Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法将PeriodIndex保留为一系列具有reset_index()的时段?_Python_Pandas - Fatal编程技术网

Python 有没有办法将PeriodIndex保留为一系列具有reset_index()的时段?

Python 有没有办法将PeriodIndex保留为一系列具有reset_index()的时段?,python,pandas,Python,Pandas,我注意到,对于具有PeriodIndex的DataFrame,在reset\u index()时,月份会恢复为其本机Int64类型,并在过程中丢失其freq属性。有没有办法将其保持为系列的周期s 例如: In [42]: monthly Out[42]: qunits expend month store upc 1992-1

我注意到,对于具有
PeriodIndex
DataFrame
,在
reset\u index()
时,月份会恢复为其本机
Int64
类型,并在过程中丢失其
freq
属性。有没有办法将其保持为
系列
周期
s

例如:

In [42]: monthly
Out[42]: 
                    qunits  expend     
month   store upc                                                         
1992-12 1     21        83  248.17  
              72         3   13.95  
              78         2    6.28  
              79         1    5.82  
              85         5   28.10  
              87         1    1.87  
              88         6   11.76  
...
1994-12 151   857       12   81.48  
              858       23  116.15  
              880        7   44.73  
              881       13   25.05  
              883       21   67.25  
              884       44  190.56  
              885       13   83.57  
              887        1    4.55  
变成:

In [43]: monthly.reset_index()
Out[43]: 
        month  store   upc  qunits  expend     
0         275      1    21      83  248.17  
1         275      1    72       3   13.95  
2         275      1    78       2    6.28  
3         275      1    79       1    5.82  
4         275      1    85       5   28.10  
5         275      1    87       1    1.87  
6         275      1    88       6   11.76  
7         275      1    89      21   41.16  
...
500099    299    151   857      12   81.48  
500100    299    151   858      23  116.15  
500101    299    151   880       7   44.73  
500102    299    151   881      13   25.05  
500103    299    151   883      21   67.25  
500104    299    151   884      44  190.56  
500105    299    151   885      13   83.57  
500106    299    151   887       1    4.55  
更新日期2014年6月13日 它工作得很好,但我需要的最终结果是将
PeriodIndex
值传递到分组的
DataFrame
。我让它工作,但在我看来,它可以做得更紧凑。即,我的代码是:

periods_index = monthly.index.get_level_values('month')
monthly.reset_index(inplace=True)
monthly.month = periods_index
grouped=monthly.groupby('month')
moments=pd.DataFrame(monthly.month.unique(),columns=['month'])
for month,group in grouped:
  moments.loc[moments.month==month,'meanNo0']=wmean(group[group.relative!=1].avExpend,np.log(group[group.relative!=1].relative))
还有什么建议吗?

这个怎么样:

periods_index = monthly.index.get_level_values('month')