我有一个名为“train”的数据框,其列 ID 以一种非常不寻常的方式表示“日期”。例如ID 中的某些条目:
For example, the value of ID 2013043002 represents the date 30/04/2013
02:00:00
前4位代表年,后2位分别代表月和日。最后两位数字代表时间。
所以我想把它转换成合适的日期时间格式来进行时间序列分析。
最佳答案
使用to_datetime
使用参数 format
- 检查 http://strftime.org/ :
df = pd.DataFrame({'ID':[2013043002,2013043002]})
df['ID'] = pd.to_datetime(df['ID'], format='%Y%m%d%H')
print(df)
ID
0 2013-04-30 02:00:00
1 2013-04-30 02:00:00
print(df['ID'].dtype)
datetime64[ns]
关于python - 使用 Python 将一组数字转换为日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755489/