首页 > 解决方案 > How to convert Python DataFrame column with Excel 5 digit date to yyyy-mm-dd

问题描述

I'm trying to convert an entire column containing a 5 digit date code (EX: 43390, 43599) to a normal date format. This is just to make data analysis easier, it doesn't matter which way it's formatted. In a series, the DATE column looks like this:

1         43390
2         43599
3         43605
4         43329
5         43330
          ...  
264832    43533
264833    43325
264834    43410
264835    43461
264836    43365

I don't understand previous submissions with this question, and when I tried code such as

date_col = df.iloc[:,0]
print((datetime.utcfromtimestamp(0) + timedelta(date_col)).strftime("%Y-%m-%d")) 

I get this error

unsupported type for timedelta days component: Series

Thanks, sorry if this is a basic question.

标签: pythonexcelpandasdate

解决方案


您正在调用数据框并将其分配给date_col. 例如,如果要获取第一行的值,请使用date_col = df.iloc[0]. 这将返回值。Timedelta 采用整数值,而不是 Series。


推荐阅读