2016-07-28 131 views
2

我有一个小时频率的日期时间索引数据框。我想创建一个groupby对象 - 按季节分组。到了季节,我的意思是春天是3,4,5,夏天是6,7,8等等。我希望每个赛季都有一个独特的组合。有没有办法做到这一点与自定义DateOffset?它需要一个子类来完成吗?还是我生产一个季节专栏,然后做:grouper = df.groupby([df['season'], df.index.year])Python Pandas:创建季节性DateOffset对象?

当前的代码是丑陋:

def group_season(df): 
    """ 
    This uses the meteorological seasons 
    """ 
    df['month'] = df.index.month 
    spring = df['month'].isin([3,4,5]) 
    spring[spring] = 'spring' 
    summer = df['month'].isin([6,7,8]) 
    summer[summer] = 'summer' 
    fall = df['month'].isin([9,10,11]) 
    fall[fall] = 'fall' 
    winter = df['month'].isin([12,1,2]) 
    winter[winter] = 'winter' 
    df['season'] = pd.concat([winter[winter != False], spring[spring != False],\ 
    fall[fall != False], summer[summer != False]], axis=0) 

    return df.groupby([df['season'], df.index.year]) 

回答

2

对于那种你想要做的分组,使用anchored quarterly offsets

import numpy as np 
import pandas as pd 

dates = pd.date_range('2016-01', freq='MS', periods=12) 
df = pd.DataFrame({'num': np.arange(12)}, index=dates) 
print(df) 

#    num 
# 2016-01-01 0 
# 2016-02-01 1 
# 2016-03-01 2 
# 2016-04-01 3 
# 2016-05-01 4 
# 2016-06-01 5 
# 2016-07-01 6 
# 2016-08-01 7 
# 2016-09-01 8 
# 2016-10-01 9 
# 2016-11-01 10 
# 2016-12-01 11 

by_season = df.resample('QS-MAR').sum() 
print(by_season) 

#    num 
# 2015-12-01 1 
# 2016-03-01 9 
# 2016-06-01 18 
# 2016-09-01 27 
# 2016-12-01 11 

您也可以在索引更好,更具描述性的标签:

SEASONS = { 
    'winter': [12, 1, 2], 
    'spring': [3, 4, 5], 
    'summer': [6, 7, 8], 
    'fall': [9, 10, 11] 
} 
MONTHS = {month: season for season in SEASONS.keys() 
         for month in SEASONS[season]} 

by_season.index = (pd.Series(by_season.index.month).map(MONTHS) + 
        ' ' + by_season.index.year.astype(str)) 
print(by_season) 

#    num 
# winter 2015 1 
# spring 2016 9 
# summer 2016 18 
# fall 2016  27 
# winter 2016 11