[英]pandas localize and convert datetime column instead of the datetimeindex
I have the following dataframe, which is indexed by a 'tz-aware' Datetimeindex
. 我有以下的数据帧,这是由“TZ-意识”索引
Datetimeindex
。
In [92]: df
Out[92]:
last_time
ts_recv
2017-02-13 07:00:01.103036+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:03.065284+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:13.244515+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:17.562202+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:17.917565+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:21.985626+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:28.096251+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:32.087421+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:33.386040+01:00 2017-02-13 16:03:23.626000
2017-02-13 07:00:43.923534+01:00 2017-02-13 16:03:23.626000
I only have one column called last_time
which also contains time but as strings and in a different timezone ( America/New_York
) than the one in the index (which is Europe/Paris
). 我只有一个名为
last_time
列,它也包含时间,但是作为字符串,并且在不同的时区( America/New_York
)中,而不是索引中的那个( Europe/Paris
)。
My goal is to convert this column to a datetime, in the right timezone. 我的目标是在正确的时区将此列转换为日期时间。
I've tried the following: 我尝试过以下方法:
In [94]: pd.to_datetime(df['last_time'])
Out[94]:
ts_recv
2017-02-13 07:00:01.103036+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:03.065284+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:13.244515+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:17.562202+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:17.917565+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:21.985626+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:28.096251+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:32.087421+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:33.386040+01:00 2017-02-13 16:03:23.626
2017-02-13 07:00:43.923534+01:00 2017-02-13 16:03:23.626
Name: last_time, dtype: datetime64[ns]
This effectively converts the column to datetime objects. 这有效地将列转换为datetime对象。
But the following fails 但是以下失败了
In [96]: pd.to_datetime(df['last_time']).tz_localize('America/New_York')
with the error 有错误
TypeError: Already tz-aware, use tz_convert to convert.
I manage to get the Series I want with the following 我设法通过以下方式获得我想要的系列
In [104]: pd.Series(pd.DatetimeIndex(df['last_time'].values)
.tz_localize('America/New_York').tz_convert('Europe/Paris'))
Out[104]:
0 2017-02-13 22:03:23.626000+01:00
1 2017-02-13 22:03:23.626000+01:00
2 2017-02-13 22:03:23.626000+01:00
3 2017-02-13 22:03:23.626000+01:00
4 2017-02-13 22:03:23.626000+01:00
5 2017-02-13 22:03:23.626000+01:00
6 2017-02-13 22:03:23.626000+01:00
7 2017-02-13 22:03:23.626000+01:00
8 2017-02-13 22:03:23.626000+01:00
9 2017-02-13 22:03:23.626000+01:00
dtype: datetime64[ns, Europe/Paris]
I can then reindex it using the original datetimeindex and plug it back to the dataframe. 然后我可以使用原始datetimeindex重新索引它并将其重新插入数据帧。
However I find this solution quite dirty and I'm wondering if there's a better way to do it. 但是我发现这个解决方案非常脏,我想知道是否有更好的方法来做到这一点。
You were almost there - just add .dt
accessor... 你几乎就在那里 - 只需添加
.dt
访问器......
Source DF: 来源DF:
In [86]: df
Out[86]:
last_time
ts_recv
2017-02-13 06:00:01.103036 2017-02-13 16:03:23.626000
2017-02-13 06:00:03.065284 2017-02-13 16:03:23.626000
2017-02-13 06:00:13.244515 2017-02-13 16:03:23.626000
2017-02-13 06:00:17.562202 2017-02-13 16:03:23.626000
2017-02-13 06:00:17.917565 2017-02-13 16:03:23.626000
2017-02-13 06:00:21.985626 2017-02-13 16:03:23.626000
2017-02-13 06:00:28.096251 2017-02-13 16:03:23.626000
2017-02-13 06:00:32.087421 2017-02-13 16:03:23.626000
2017-02-13 06:00:33.386040 2017-02-13 16:03:23.626000
2017-02-13 06:00:43.923534 2017-02-13 16:03:23.626000
In [87]: df.dtypes
Out[87]:
last_time object
dtype: object
Converting to datetime + TZ: 转换为datetime + TZ:
In [88]: df['last_time'] = pd.to_datetime(df['last_time']) \
.dt.tz_localize('Europe/Paris') \
.dt.tz_convert('America/New_York')
In [89]: df
Out[89]:
last_time
ts_recv
2017-02-13 06:00:01.103036 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:03.065284 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:13.244515 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:17.562202 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:17.917565 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:21.985626 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:28.096251 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:32.087421 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:33.386040 2017-02-13 10:03:23.626000-05:00
2017-02-13 06:00:43.923534 2017-02-13 10:03:23.626000-05:00
In [90]: df.dtypes
Out[90]:
last_time datetime64[ns, America/New_York]
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.