2014-02-19 104 views
1

不確定這是否可以使用熊貓。不過,我想按如下方式製作一個DataFrame。
除了我只想在沒有年份的指數中有幾個月和幾天。大熊貓DataFrame索引按月嗎?

import pandas as pd 
import numpy as np 
df2 = pd.DataFrame(np.random.randn(12, 4), index=pd.date_range('1-1', periods=12, freq='M'), columns=['2007', '2008', '2009', '2010']) 

只是給一點點信息。我做了以下。

df = pd.Series(np.random.randn(72), index=pd.date_range('1/1/2000', periods=72, freq='M')) 

那麼我可以用grouby如下:

groupYear_Month = df.groupby(lambda x: (x.year, x.month)).sum() 

其中產量:

groupYear_Month.head() 
Out[9]: 
(2000, 1) 1.077949 
(2000, 2) -0.563224 
(2000, 3) -2.016833 
(2000, 4) -0.140693 
(2000, 5) 2.113549 
dtype: float64 

現在我可以:

groupYear_Month.index = pd.MultiIndex.from_tuples(groupYear_Month.index) 

然而,這殺死日期格式。例如,我沒有得到兩個月的01,02 ... 12.
我現在可以取消它並獲得專欄級別的年限。

groupYear_Month.unstack(0) 

這可以工作,但它不再是日期索引。

感謝

+0

我不知道你能做到這一點。如果您檢查'df.index.month.dtype'(或'.year'),它是'int32'。我猜日期格式已經消失了。但是如果你只需要兩位數的月份,你只需要'groupYear_Month = df.groupby(lambda x:(x.year,str(x.month).zfill(2)))。sum()'。無論如何,日期索引必須重新生成。 –

+0

謝謝。 zfill是一個很好的建議。我希望我可以沒有一年的日期,但在某些方面看起來很奇怪。 – user3055920

回答

0

一個可能的解決辦法是寫一個小類:

class Month: 
    __slots__ = ['month', 'year'] 
    def __init__(self, date): 
     self.month, self.year = date.month, date.year 

    def __repr__(self): 
     return '{}-{:0>2}'.format(self.year, self.month) 

    def __lt__(self, other): 
     return self.year < other.year or self.year == other.year and self.month < other.month 

則:

>>> df.groupby(Month).sum() 
2000-01 -1.66 
2000-02 0.37 
2000-03 0.85 
... 
2005-11 -0.30 
2005-12 -0.93 
Length: 72, dtype: float64