ENH: Add 'end' option in resample's origin · Issue #37804 · pandas-dev/pandas (original) (raw)
In some cases I need to calculate the time group from the end of time series instead of the beginning.
For example, if user uses s.resample('7min', origin='end', closed='right')
, the following expected groups are (09:23:45, 09:30:45], (09:16:45, 09:23:45], (09:09:45, 09:16:45], ... , (08:20:45, 08:27:45]. And under normal cases, closed
should be right
when origin
is end
since last value should serve as the end value of the last group.
idx = pd.date_range('20200101 8:26:35', '20200101 9:31:58', freq='77s') idx DatetimeIndex(['2020-01-01 08:26:35', '2020-01-01 08:27:52', '2020-01-01 08:29:09', '2020-01-01 08:30:26', '2020-01-01 08:31:43', '2020-01-01 08:33:00', ... ... '2020-01-01 09:25:37', '2020-01-01 09:26:54', '2020-01-01 09:28:11', '2020-01-01 09:29:28', '2020-01-01 09:30:45'], dtype='datetime64[ns]', freq='77S') data = np.ones(len(idx)) s = pd.Series(data,index=idx) s 2020-01-01 08:26:35 1.0 2020-01-01 08:27:52 1.0 2020-01-01 08:29:09 1.0 .... ... 2020-01-01 09:23:03 1.0 2020-01-01 09:24:20 1.0 2020-01-01 09:25:37 1.0 2020-01-01 09:26:54 1.0 2020-01-01 09:28:11 1.0 2020-01-01 09:29:28 1.0 2020-01-01 09:30:45 1.0 Freq: 77S, dtype: float64
Thus the sum operation on this resampler shows:
s.resample('7min', origin='end', closed='right').sum() 2020-01-01 08:27:45 1.0 2020-01-01 08:34:45 6.0 2020-01-01 08:41:45 5.0 2020-01-01 08:48:45 6.0 2020-01-01 08:55:45 5.0 2020-01-01 09:02:45 6.0 2020-01-01 09:09:45 5.0 2020-01-01 09:16:45 6.0 2020-01-01 09:23:45 5.0 2020-01-01 09:30:45 6.0 Freq: 7T, dtype: float64