我想在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
答案 0 :(得分:0)
我相信你需要:
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