我有一个数据框 df
。我试图将它转换为 DateTime 索引,但还没有得到确切的解决方案。
India China
date
2018M06 2 1
2018M05 1 2
2018M04 3 1
. . .
. . .
. . .
这里是数据框类型“pandas.core.indexes.base.Index”的索引中的日期。
最佳答案
在索引上使用 pd.to_datetime
:
df.index = pd.to_datetime(df.index, format='%Y%m')
>>> df
India China
date
2018-06-01 2 1
2018-05-01 1 2
2018-04-01 3 1
[编辑:为您编辑的输入数据帧]:
df.index = pd.to_datetime(df.index, format='%YM%m')
>>> df
India China
date
2018-06-01 2 1
2018-05-01 1 2
2018-04-01 3 1
关于python - 将格式为 YYYYMM 且类型为 'pandas.core.indexes.base.Index' 的数据帧索引转换为日期时间索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52208306/