[英]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我有一个带有浮点值的日期列,并想转换为 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.我已经尝试了其他线程的一些方法,但仍然无法解决。
- df['Date'] = pd.to_datetime(df['Date'],format='%Y/%m/%d').dt.strftime('%Y%m%d')
df['Date'] = pd.to_datetime(df['Date'],format='%Y/%m/%d').dt.strftime('%Y%m%d')
This changes the year to 1970.
这会将年份更改为 1970 年。
- df['Modified'] = pd.to_datetime(df['Modified'], unit='s')
df['Modified'] = pd.to_datetime(df['Modified'], unit='s')
This changes the year to 1970.
这会将年份更改为 1970 年。
- df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d.0')
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).
我收到一条错误消息:时间数据“43411”与格式“%Y%m%d.0”不匹配(匹配)。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.