Tom*_*101 5 python numpy time-series pandas
我有一个DataFrame
时间序列:
rng = pd.date_range('2016-06-01', periods=24*7, freq='H')
ones = pd.Series([1]*24*7, rng)
rdf = pd.DataFrame({'a': ones})
Run Code Online (Sandbox Code Playgroud)
最后一个条目是2016-06-07 23:00:00
.我现在想把这个分组,比如两天,基本上是这样的:
rdf.groupby(pd.TimeGrouper('2D')).sum()
Run Code Online (Sandbox Code Playgroud)
但是,我想从最后一个数据点向后分组,所以不要得到这个结果:
a
2016-06-01 48
2016-06-03 48
2016-06-05 48
2016-06-07 24
Run Code Online (Sandbox Code Playgroud)
我更期待这个:
a
2016-06-01 24
2016-06-03 48
2016-06-05 48
2016-06-07 48
Run Code Online (Sandbox Code Playgroud)
分组时'3D'
:
a
2016-06-01 24
2016-06-04 72
2016-06-07 72
Run Code Online (Sandbox Code Playgroud)
分组时的预期结果'4D'
是:
a
2016-06-03 72
2016-06-07 96
Run Code Online (Sandbox Code Playgroud)
我不能用的每个组合得到这个closed
,label
等我能想到的.
我怎样才能做到这一点?
由于我主要想按 7 天(即一周)进行分组,因此我现在使用此方法来找到所需的垃圾箱:
from pandas.tseries.offsets import Week
# Let's not make full weeks
hours = 24*6*4
rng = pd.date_range('2016-06-01', periods=hours, freq='H')
# Set week start to whatever the last weekday of the range is
print("Last day is %s" % rng[-1])
freq = Week(weekday=rng[-1].weekday())
ones = pd.Series([1]*hours, rng)
rdf = pd.DataFrame({'a': ones})
rdf.groupby(pd.TimeGrouper(freq=freq, closed='right', label='right')).sum()
Run Code Online (Sandbox Code Playgroud)
这给了我想要的输出
2016-06-25 96
2016-07-02 168
2016-07-09 168
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
411 次 |
最近记录: |