首页 > 解决方案 > Pandas DatetimeIndex 到数据框

问题描述

如何将 DatetimeIndex 更改为这样的简单数据框:

        month
0  2013-07-31
1  2013-08-31
2  2013-09-30
3  2013-10-31

这是日期时间索引:

DatetimeIndex(['2013-07-31', '2013-08-31', '2013-09-30', '2013-10-31',
       '2013-11-30', '2013-12-31', '2014-01-31', '2014-02-28',
       '2014-03-31', '2014-04-30', '2014-05-31', '2014-06-30'],
        dtype='datetime64[ns]', freq='M')

谢谢你。

标签: pandas

解决方案


使用DataFrame构造函数:

idx = pd.DatetimeIndex(['2013-07-31', '2013-08-31', '2013-09-30', '2013-10-31',
       '2013-11-30', '2013-12-31', '2014-01-31', '2014-02-28',
       '2014-03-31', '2014-04-30', '2014-05-31', '2014-06-30'],
        dtype='datetime64[ns]', freq='M')

df = pd.DataFrame({'month':idx})
#alternative 
#df = pd.DataFrame({'month':df1.index})
print (df)
        month
0  2013-07-31
1  2013-08-31
2  2013-09-30
3  2013-10-31
4  2013-11-30
5  2013-12-31
6  2014-01-31
7  2014-02-28
8  2014-03-31
9  2014-04-30
10 2014-05-31
11 2014-06-30

推荐阅读