将格式为 YYYYMM 且类型为 'pandas.core.indexes.base.Index' 的数据帧索引转换为日期时间索引
Convert dataframe index with format YYYYMM and type 'pandas.core.indexes.base.Index' to datetime index
我有一个数据框 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
我有一个数据框 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