简体   繁体   English

熊猫优雅地处理具有不同日期时间格式的列

[英]pandas handle column with different date time formats gracefully

I have a column with a birthdate. 我有一列生日。 Some are NA, some 01.01.2016 but some contain 01.01.2016 01:01:01 Filtering the NA values works fine. 有些是NA,有些是01.01.2016但有些包含01.01.2016 01:01:01过滤NA值就可以了。 But handling the different date formats seems clumsy. 但是处理不同的日期格式似乎很笨拙。 Is it possible to have pandas handle these gracefully and eg for a birthdate only interpret the date and not fail? 是否可以让熊猫优雅地处理这些问题,例如对于生日,只解释日期而不失败?

pd.to_datetime() will handle multiple formats pd.to_datetime()将处理多种格式

>>> ser = pd.Series(['NaT', '01.01.2016', '01.01.2016 01:01:01'])
>>> pd.to_datetime(ser)
0                   NaT
1   2016-01-01 00:00:00
2   2016-01-01 01:01:01
dtype: datetime64[ns]

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

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