简体   繁体   中英

Pandas dataframe change date from int64 to date in datetime

I have a dataframe where the date column type is int64. I want to convert that column and its values to datetime.

df['datdadat'] = pd.to_datetime(df['datadate'].astype('str'), errors='coerce')

在最后一栏中使用我的代码进行转换

As you can see, my code fails when encountering the second row without leading zeroes for the months/days. I tried adding a format already, but when I add it in, even the first one with zeroes fails.

My question would be how to add in the leading zeroes before conversion, or if there is a good way to add a format in while converting it? Thx in advance

Try this:

df['datdadat'] = pd.to_datetime(df['datdadat'], format='%d%m%y')
print(df)

       datdadat
0 1984-01-30
1 1983-12-30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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