[英]Convert multiple date formats to datetime in pandas
I have a row of messy data where date formats are different and I want them to be coherent as datetime in pandas我有一行凌乱的数据,其中日期格式不同,我希望它们与 Pandas 中的日期时间一致
df:
Date
0 1/05/2015
1 15 Jul 2009
2 1-Feb-15
3 12/08/2019
When I run this part:当我运行这部分时:
df['date'] = pd.to_datetime(df['date'], format='%d %b %Y', errors='coerce')
I get我得到
Date
0 NaT
1 2009-07-15
2 NaT
3 NaT
How do I convert it all to date time in pandas?如何将其全部转换为熊猫中的日期时间?
pd.to_datetime
is capabale of handling multiple date formats in the same column. pd.to_datetime
能够处理同一列中的多种日期格式。 Specifying a format
will hinder its ability to dynamically determine the format, so if there are multiple types do not specify the format
:指定
format
会妨碍它动态确定格式的能力,所以如果有多种类型不要指定format
:
import pandas as pd
df = pd.DataFrame({
'Date': ['1/05/2015', '15 Jul 2009', '1-Feb-15', '12/08/2019']
})
df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
print(df)
Date
0 2015-01-05
1 2009-07-15
2 2015-02-01
3 2019-12-08
*There are limitations to the ability to handle multiple date times. *处理多个日期时间的能力存在限制。 Mixed timezone aware and timezone unaware datetimes will not process correctly.
混合时区感知和时区不感知日期时间将无法正确处理。 Likewise mixed dayfirst and monthfirst notations will not always parse correctly.
同样,混合的 dayfirst 和 monthfirst 符号不会总是正确解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.