如何将DatetimeIndex更改为像这样的简单数据框:
month
0 2013-07-31
1 2013-08-31
2 2013-09-30
3 2013-10-31
这是DatetimeIndex:
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')
谢谢。
答案 0 :(得分:1)
使用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
答案 1 :(得分:1)
下面的代码应该可以工作:
# Import libraries
import pandas as pd
import numpy as np
import datetime as dt
# Create dataframe
df = pd.DataFrame(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'), columns=['month'])
df.head(2)