Groupby 与 TimeGrouper '向后'

Groupby with TimeGrouper #39;backwards#39;(Groupby 与 TimeGrouper 向后)
本文介绍了Groupby 与 TimeGrouper '向后'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个包含时间序列的 DataFrame:

I have a DataFrame containing a time series:

rng = pd.date_range('2016-06-01', periods=24*7, freq='H')
ones = pd.Series([1]*24*7, rng)
rdf = pd.DataFrame({'a': ones})

最后一个条目是 2016-06-07 23:00:00.我现在想按两天分组,基本上是这样的:

Last entry is 2016-06-07 23:00:00. I now want to group this by, say two days, basically like so:

rdf.groupby(pd.TimeGrouper('2D')).sum()

但是,我想从最后一个数据点开始向后分组,所以不要得到这个结果:

However, I want to group starting from my last data point backwards, so instead of getting this result:

            a
2016-06-01  48
2016-06-03  48
2016-06-05  48
2016-06-07  24

我更希望这样:

            a
2016-06-01  24
2016-06-03  48
2016-06-05  48
2016-06-07  48

当按'3D'分组时:

            a
2016-06-01  24
2016-06-04  72
2016-06-07  72

'4D'分组时的预期结果是:

Expected outcome when grouping by '4D' is:

            a
2016-06-03  72
2016-06-07  96

我无法通过我能想到的 closedlabel 等的每种组合来实现这一点.

I am not able to get this with every combination of closed, label etc. I could think of.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

由于我主要想按 7 天(也就是一周)分组,所以我现在使用这种方法来找到所需的垃圾箱:

Since I primarily want to group by 7 days, aka one week, I am using this method now to come to the desired bins:

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()

这给了我想要的输出

2016-06-25  96
2016-07-02  168
2016-07-09  168

这篇关于Groupby 与 TimeGrouper '向后'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)