pandas本地化并转换datetime列而不是datetimeindex

时间:2017-03-10 15:28:50

标签: python pandas datetime timezone

我有以下数据框,由“tz-aware”'编入索引。 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

我只有一个名为last_time的列,其中包含时间,但是作为字符串,并且在不同的时区(America/New_York)中,而不是索引中的列Europe/Paris {1}})。

我的目标是在正确的时区将此列转换为日期时间。

我尝试过以下方法:

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]

这有效地将列转换为datetime对象。

但以下失败

In [96]: pd.to_datetime(df['last_time']).tz_localize('America/New_York')

错误

TypeError: Already tz-aware, use tz_convert to convert.

我设法通过以下

获得我想要的系列
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]

然后我可以使用原始的datetimeindex重新索引它并将其重新插入数据帧。

但是我发现这个解决方案非常脏,我想知道是否有更好的方法。

1 个答案:

答案 0 :(得分:8)

你几乎就在那里 - 只需添加.dt访问者...

来源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

转换为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