简体   繁体   English

Pandas 一列中的混合日期格式值

[英]Pandas Mixed Date Format Values in One Column

df = pd.Series('''18-04-2022
2016-10-05'''.split('\n') , name='date'
).to_frame()

df['post_date'] = pd.to_datetime(df['date'])
print (df)

         date  post_date
0  18-04-2022 2022-04-18
1  2016-10-05 2016-10-05

When trying to align the date column into one consistent format, I get an error such as above.尝试将日期列对齐为一种一致的格式时,出现如上所示的错误。
The error is that values have mixed date formats dd-mm-yyyy (18-04-2022) and yyyy-dd-mm (2016-10-05).错误是值具有混合日期格式 dd-mm-yyyy (18-04-2022) 和 yyyy-dd-mm (2016-10-05)。

What I want to have is below (yyyy-mm-dd) for both of the above inconsistent formats:对于上述两种不一致的格式,我想要的是下面的 (yyyy-mm-dd):

         date  post_date
0  18-04-2022 2022-04-18
1  2016-10-05 2016-05-10

Appreciate it in advance.提前欣赏。

You can be explicit and parse the two possible formats one after the other:您可以明确并依次解析两种可能的格式:

df['post_date'] = (
 pd.to_datetime(df['date'], format='%d-%m-%Y', errors='coerce')
   .fillna(
 pd.to_datetime(df['date'], format='%Y-%d-%m', errors='coerce')
    )
 )

Output: Output:

         date  post_date
0  18-04-2022 2022-04-18
1  2016-10-05 2016-05-10

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

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