Skip to main content
 首页 » 编程设计

python之使用 Python 将一组数字转换为日期格式

2025年05月04日22TianFang

我有一个名为“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]