[英]Converting Python dataframe column to date format
I have a column in a dataframe that I want to convert to a date.我在数据框中有一列要转换为日期。 The values of the column are either
DDMONYYY
or DD Month YYYY 00:00:00.000 GMT
.该列的值为
DDMONYYY
或DD Month YYYY 00:00:00.000 GMT
。 For example, one row in the dataframe could have the value 31DEC2002
and the next row could have 31 December 2015 00:00:00.000 GMT
.例如,数据
31DEC2002
的一行可能具有值31DEC2002
,下一行可能具有31 December 2015 00:00:00.000 GMT
。 I think this is why I get an error when trying to convert the column to a date using pd.to_datetime or datetime.strptime to convert.我认为这就是为什么我在尝试使用 pd.to_datetime 或 datetime.strptime 将列转换为日期时出现错误的原因。
Anyone got any ideas?有人有任何想法吗? I'd be very grateful for any help/pointers.
我将非常感谢任何帮助/指示。
For me working to_datetime
with utc=True
for converting all values to UTC
and errors='coerce'
for convert not parseable values to NaT
(missing datetime):对我
to_datetime
使用utc=True
使用to_datetime
将所有值转换为UTC
而使用errors='coerce'
将不可解析的值NaT
为NaT
(缺少日期时间):
df = pd.DataFrame({'date':['31DEC2002','31 December 2015 00:00:00.000 GMT','.']})
df['date'] = pd.to_datetime(df['date'], utc=True, errors='coerce')
print (df)
date
0 2002-12-31 00:00:00+00:00
1 2015-12-31 00:00:00+00:00
2 NaT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.