我想在python中的dataframe中转换dateTime列的时区。这就是我的做法。
df.dtypes
Out[11]:
x1 object
x2 object
event_time datetime64[ns]
x3 float64
dtype: object
type(df.event_time)
Out[12]: pandas.core.series.Series
但结果看起来像这样
df['event_time'].dt.tz_localize('Asia/Kolkata')
Out[2]:
0 2017-12-06 13:07:00+05:30
1 2017-12-06 13:06:00+05:30
2 2017-12-06 13:05:00+05:30
3 2017-12-06 13:04:00+05:30
4 2017-12-06 13:03:00+05:30
5 2017-12-06 13:02:00+05:30
6 2017-12-06 13:01:00+05:30
7 2017-12-06 13:00:00+05:30
8 2017-12-06 12:59:00+05:30
9 2017-12-06 12:58:00+05:30
10 2017-12-06 12:57:00+05:30
11 2017-12-06 12:56:00+05:30
12 2017-12-06 12:55:00+05:30
有什么方法可以显示如下
的日期时间 0 2017-12-06 18:37:00
1 2017-12-06 18:36:00
另外,我有兴趣通过使用+05:30
更改时区,而不是使用区域名称'Asia/Kolkata'
EDIT :
df = pd.DataFrame({'event_time': [pd.Timestamp('2017-12-06 13:07:00'),pd.Timestamp('2017-12-06 13:06:00')]})
df['event_time'] = df['event_time'].dt.tz_localize('Asia/Kolkata')
print (df)
event_time
0 2017-12-06 13:07:00+05:30
1 2017-12-06 13:06:00+05:30
df['event_time'] = df['event_time'].dt.tz_localize('Asia/Kolkata').dt.tz_convert('UTC').dt.tz_localize(None)
File "<ipython-input-24-ee54cb3c6b25>", line 1
df['event_time'] = df['event_time'].dt.tz_localize('Asia/Kolkata').dt.tz_convert('UTC').dt.tz_localize(None)
^
SyntaxError: invalid character in identifier
我相信您需要:
df = pd.DataFrame({'event_time': [pd.Timestamp('2017-12-06 13:07:00'),
pd.Timestamp('2017-12-06 13:06:00')]})
df['event_time'] = df['event_time'].dt.tz_localize('Asia/Kolkata')
print (df)
event_time
0 2017-12-06 13:07:00+05:30
1 2017-12-06 13:06:00+05:30
df['event_time'] = df['event_time'].dt.tz_convert('UTC').dt.tz_localize(None)
print (df)
event_time
0 2017-12-06 07:37:00
1 2017-12-06 07:36:00