熊猫时间石斑鱼:自定义范围

时间:2015-03-04 21:43:15

标签: python pandas

有条件的datetime索引,pd.TimeGrouper("AS")按日历年对我的数据进行分组。 pandas附带了variety of useful offsets - 但如果我想要自己的那个怎么办?

例如,如果我想分组2年或16个月,该怎么办?我该如何处理?

1 个答案:

答案 0 :(得分:3)

combining offsets的下一部分中看起来就是这样,例如:

In [40]: rng = date_range('1/1/2015', periods=365, freq='D')
In [41]: d = Series(range(0, len(rng)), index=rng)

逐月分组

In [42]: d.groupby(TimeGrouper('MS')).first()
Out[42]:
2015-01-01      1
2015-02-01     32
2015-03-01     60
2015-04-01     91
2015-05-01    121
2015-06-01    152
2015-07-01    182
2015-08-01    213
2015-09-01    244
2015-10-01    274
2015-11-01    305
2015-12-01    335
Freq: MS, dtype: int64

3个月:

In [43]: d.groupby(TimeGrouper('3MS')).first()
Out[43]:
2015-01-01      1
2015-04-01     91
2015-07-01    182
2015-10-01    274
Freq: 3MS, dtype: int64

所以两年可能是2AS,16个月可能是16MS

相关问题