简体   繁体   English

Pandas:转换为带有时区名称的日期时间

[英]Pandas: converting to datetime with timezone name

I am trying to convert to datetime in pandas dataframe, where name of timezone is included.我正在尝试在 pandas dataframe 中转换为日期时间,其中包含时区名称。 I have tried the following but it yields an error:我尝试了以下方法,但会产生错误:

d = {'col1': ['Sun Dec 31 00:00:00 EAT 1899', 'Mon Mar 02 00:00:00 EAT 2020']}
x = pd.DataFrame(data=d)
pd.to_datetime(x['col1'], format='%a %b %d %H:%M:%S %Z %Y', errors='coerce')

0   NaT
1   NaT
Name: col1, dtype: datetime64[ns]

Is there any way to convert to datetime this way?有没有办法以这种方式转换为日期时间? If not, how to ignore the timezone name?如果没有,如何忽略时区名称?

you can use dateutil :你可以使用dateutil

import dateutil
print (x['col1'].apply(dateutil.parser.parse))
0   1899-12-31
1   2020-03-02
Name: col1, dtype: datetime64[ns]

or to ignore the part with the timezoen, something like:或忽略带有 timezoen 的部分,例如:

pd.to_datetime(x['col1'].str[:20] + x['col1'].str[24:])

but not sure if it is interesting但不确定是否有趣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM