Pandas to_datetime changes year unexpectedly

安稳与你 提交于 2020-04-16 03:08:12

问题


I have a date column which after using pandas read_csv is represented as the object type. I'm trying to convert it to pandas datetime object but when using pd.to_datetime() it returns incorrect datetime object.

for example, I have dates in this format 01-06-68, where 01 is the day, 06 is the month and 68 is the year. Applying pandas to_datetime() to this string returns 2068-06-01 but should return 1968-06-01, where 06 is a month and 01 is the day.

I tried every possible solution using pandas to_datetime(), python's datetime, pendulum library but still getting an error. How can I solve this problem?


回答1:


Use:

df['date'] = pd.to_datetime(df['date'].str[:-2] + '19' + df['date'].str[-2:])

Another solution with replace:

df['date'] = pd.to_datetime(df['date'].str.replace(r'-(\d+)$', r'-19\1'))

Sample:

print (df)
       date
0  01-06-70
1  01-06-69
2  01-06-68
3  01-06-67

df['date'] = pd.to_datetime(df['date'].str.replace(r'-(\d+)$', r'-19\1'))
print (df)
        date
0 1970-01-06
1 1969-01-06
2 1968-01-06
3 1967-01-06



回答2:


You have a format= parameter in pd.to_datetime
You can probably try df['my_col'] = pd.to_datetime(df['my_col'], format='%d-%m-%Y')

See other way to do it here: Convert Pandas Column to DateTime



来源:https://stackoverflow.com/questions/55684075/pandas-to-datetime-changes-year-unexpectedly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!