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

标签 python datetime python-datetime

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

相关文章:

python - 如何在 python 中捕获 finally 异常子句的消息?

JavaScript - 日期不相等

Python:从时间戳中添加或删除时区小时并获取实际时间

python - 获取python中两个日期之间日期的星期数

python - 在新的virtualenv中安装wheel软件包时,导入旧django项目模型的任何方法

python - 在运行时获取 HybridBlock 层形状

python - 取一个数字并对其数字求和

c# - 为什么将 DateTime 格式化为字符串会截断而不是舍入毫秒?

ruby-on-rails - 如果将日期时间列转换为日期 (rails),则不在失败的地方

python - 在 matplotlib 中将日期时间对象列表绘制为折线图