pandas 熊猫本地化和转换日期时间列而不是日期时间索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42722018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas localize and convert datetime column instead of the datetimeindex
提问by valentin
I have the following dataframe, which is indexed by a 'tz-aware' Datetimeindex
.
我有以下数据框,它由 '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
I only have one column called last_time
which also contains time but as stringsand 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.
这有效地将列转换为日期时间对象。
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.
然后我可以使用原始日期时间索引重新索引它并将其插入到数据帧中。
However I find this solution quite dirty and I'm wondering if there's a better way to do it.
但是我发现这个解决方案很脏,我想知道是否有更好的方法来做到这一点。
回答by MaxU
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:
转换为日期时间 + 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