import pandas as pd
from numpy.random import randn
rng = pd.date_range('1/1/2011', periods=25, freq='M')
ts = pd.Series(randn(len(rng)), index=rng)
def truncYears(ts, month):
starts = ts[ts.index.month==month].index # Fix if multiple entries per month.
groups = {}
if starts[0] > ts.index[0]:
groups[ts.index[0]] = ts[ts.index < starts[0]]
for start in starts:
end = '%d-%d'%(start.year+1, start.month-1)
print(start, end)
groups[start] = ts[start:end]
return groups
groups = truncYears(ts, 3)
for k in groups:
print(groups[k])
不雅,但我不认为groupby有这样的论据:
结果(请注意dict键未排序,因此年份不按顺序排列):
受@cphlewis的启发,这里是我的groupBy方法,它每年分组,但从给定的月份开始:
相关问题 更多 >
编程相关推荐