[英]Converting column in Dataframe to datetime
I've been trying to use the to_datetime function to convert values in my column to datetime:我一直在尝试使用 to_datetime 函数将列中的值转换为日期时间:
df['date'] = pd.to_datetime(df['date'],errors='coerce',format='%Y-%m-%d %H:%M:%S %z %Z')
After that, I received only NaT values.之后,我只收到了 NaT 值。
Example: Value Format in Column: '1979-01-01 00:00:00 +0000 UTC'示例:列中的值格式:'1979-01-01 00:00:00 +0000 UTC'
I think you can't parse utc offset (+0000) and timeszone information at the sime time.我认为您无法同时解析 utc 偏移量(+0000)和时区信息。
You might want to remove the UTC at the end and only parse the offset.您可能希望在末尾删除 UTC 并仅解析偏移量。
df['date'] = df.date.str[:-4]
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d %H:%M:%S %z')
Pandas can't manage both %z
and %Z
as you can see here . Pandas 不能同时管理
%z
和%Z
正如你在这里看到的。 Note that Python's strptime
can handle this, but doesn't deal with %Z
.请注意,Python 的
strptime
可以处理这个问题,但不处理%Z
。
In your case you might want to just peel off the last bit with ser.str
and consider opening a feature request.在您的情况下,您可能只想使用
ser.str
剥离最后一点并考虑打开功能请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.