Pandas Time Grouper:自定义范围
Pandas Time Grouper: Custom Ranges
以 datetime
索引为条件,pd.TimeGrouper("AS")
按日历年对我的数据进行分组。 pandas
附带 variety of useful offsets - 但如果我想要自己的呢?
例如,如果我想按 2 年或 16 个月分组怎么办?我将如何处理?
看起来在 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
以 datetime
索引为条件,pd.TimeGrouper("AS")
按日历年对我的数据进行分组。 pandas
附带 variety of useful offsets - 但如果我想要自己的呢?
例如,如果我想按 2 年或 16 个月分组怎么办?我将如何处理?
看起来在 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