简体   繁体   中英

Convert Float Date to YYYY-MM-DD Format

I have a Date column with float values and would like to convert to YYYY-MM-DD

Date
43411.74786 
43381.63381 
43339.3885

I've tried a few methods from the other threads but still can't solve it.

  1. df['Date'] = pd.to_datetime(df['Date'],format='%Y/%m/%d').dt.strftime('%Y%m%d')

This changes the year to 1970.

  1. df['Modified'] = pd.to_datetime(df['Modified'], unit='s')

This changes the year to 1970.

  1. df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d.0')

I get an error message: time data '43411' does not match format '%Y%m%d.0' (match).

Check with

pd.to_datetime(df.Date,unit='d',origin='1900-01-01')
Out[364]: 
0   2018-11-09 17:56:55.104
1   2018-10-10 15:12:41.184
2   2018-08-29 09:19:26.400
Name: Date, dtype: datetime64[ns]

This is working for me, let me know if this works for you.

x['Date_new']=pd.to_datetime(x.Date, unit='d', origin='1900-01-01').dt.strftime('%Y-%m-%d')
x

output

       Date      Date_new
0   43411.74786  2018-11-09
1   43381.63381  2018-10-10
2   43339.38850  2018-08-29

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