Skip to content

Commit 222ef8d

Browse files
committed
add doc-string
1 parent d096ccd commit 222ef8d

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

pandas/core/generic.py

+41-3
Original file line numberDiff line numberDiff line change
@@ -7973,8 +7973,9 @@ def resample(
79737973
`DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`.
79747974
closed : {'right', 'left'}, default None
79757975
Which side of bin interval is closed. The default is 'left'
7976-
for all frequency offsets except for 'M', 'A', 'Q', 'BM',
7977-
'BA', 'BQ', and 'W' which all have a default of 'right'.
7976+
for all frequency offsets with forward resampling except for 'M',
7977+
'A', 'Q', 'BM', 'BA', 'BQ', and 'W' which all have a default of
7978+
'right'. When `Backward` set to be True, default is 'right'.
79787979
label : {'right', 'left'}, default None
79797980
Which bin edge label to label bucket with. The default is 'left'
79807981
for all frequency offsets except for 'M', 'A', 'Q', 'BM',
@@ -8007,7 +8008,7 @@ def resample(
80078008
level : str or int, optional
80088009
For a MultiIndex, level (name or number) to use for
80098010
resampling. `level` must be datetime-like.
8010-
origin : {'epoch', 'start', 'start_day'}, Timestamp or str, default 'start_day'
8011+
origin : {'epoch', 'start', 'start_day', 'end', 'end_day'}, Timestamp or str, default 'start_day'
80118012
The timestamp on which to adjust the grouping. The timezone of origin
80128013
must match the timezone of the index.
80138014
If a timestamp is not used, these values are also supported:
@@ -8018,6 +8019,21 @@ def resample(
80188019
80198020
.. versionadded:: 1.1.0
80208021
8022+
- 'end': `origin` is the last value of the timeseries
8023+
- 'end_day': `origin` is the ceiling midnight of the last day
8024+
8025+
.. versionadded:: 1.2.0
8026+
8027+
backward : bool, default is None
8028+
Resample on the given `origin` from a backward direction. True when
8029+
`origin` is 'end' or 'end_day'. False when `origin` is 'start' or
8030+
'start_day'. Optional when using datetime `origin` , and default
8031+
False. The resample result for a specified datetime stands for the
8032+
group from time substract the given `freq` to time with a right
8033+
`closed` setting by default.
8034+
8035+
.. versionadded:: 1.2.0
8036+
80218037
offset : Timedelta or str, default is None
80228038
An offset timedelta added to the origin.
80238039
@@ -8297,6 +8313,28 @@ def resample(
82978313
2000-10-02 00:21:00 24
82988314
Freq: 17T, dtype: int64
82998315
8316+
If you want to take the last timestamp as `origin` with a backward resample:
8317+
8318+
>>> ts.index.max()
8319+
Timestamp('2000-10-02 00:26:00', freq='7T')
8320+
>>> ts.groupby(pd.Grouper(freq='17min', origin='end')).sum()
8321+
2000-10-01 23:35:00 0
8322+
2000-10-01 23:52:00 18
8323+
2000-10-02 00:09:00 27
8324+
2000-10-02 00:26:00 63
8325+
Freq: 17T, dtype: int32
8326+
8327+
You can also specify the backward origin:
8328+
8329+
>>> ts.groupby(pd.Grouper(freq='17min',
8330+
origin='2000-10-02 00:30:00',
8331+
backward=True)).sum()
8332+
2000-10-01 23:39:00 3
8333+
2000-10-01 23:56:00 15
8334+
2000-10-02 00:13:00 45
8335+
2000-10-02 00:30:00 45
8336+
Freq: 17T, dtype: int32
8337+
83008338
To replace the use of the deprecated `base` argument, you can now use `offset`,
83018339
in this example it is equivalent to have `base=2`:
83028340

pandas/core/groupby/grouper.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ class Grouper:
8383
However, loffset is also deprecated for ``.resample(...)``
8484
See: :class:`DataFrame.resample`
8585
86-
origin : {'epoch', 'start', 'start_day'}, Timestamp or str, default 'start_day'
87-
The timestamp on which to adjust the grouping. The timezone of origin must
88-
match the timezone of the index.
86+
origin : {'epoch', 'start', 'start_day', 'end', 'end_day'}, Timestamp or str, default 'start_day'
87+
The timestamp on which to adjust the grouping. The timezone of origin
88+
must match the timezone of the index.
8989
If a timestamp is not used, these values are also supported:
9090
9191
- 'epoch': `origin` is 1970-01-01
@@ -94,6 +94,21 @@ class Grouper:
9494
9595
.. versionadded:: 1.1.0
9696
97+
- 'end': `origin` is the last value of the timeseries
98+
- 'end_day': `origin` is the ceiling midnight of the last day
99+
100+
.. versionadded:: 1.2.0
101+
102+
backward : bool, default is None
103+
Resample on the given `origin` from a backward direction. True when
104+
`origin` is 'end' or 'end_day'. False when `origin` is 'start' or
105+
'start_day'. Optional when using datetime `origin` , and default
106+
False. The resample result for a specified datetime stands for the
107+
group from time substract the given `freq` to time with a right
108+
`closed` setting by default.
109+
110+
.. versionadded:: 1.2.0
111+
97112
offset : Timedelta or str, default is None
98113
An offset timedelta added to the origin.
99114
@@ -200,6 +215,15 @@ class Grouper:
200215
2000-10-02 00:15:00 45
201216
Freq: 17T, dtype: int64
202217
218+
If you want to take the last timestamp as `origin` with a backward resample:
219+
220+
>>> ts.groupby(pd.Grouper(freq='17min', origin='end')).sum()
221+
2000-10-01 23:39:00 0
222+
2000-10-01 23:56:00 0
223+
2000-10-02 00:13:00 3
224+
2000-10-02 00:30:00 6
225+
Freq: 17T, dtype: int32
226+
203227
If you want to adjust the start of the bins with an `offset` Timedelta, the two
204228
following lines are equivalent:
205229

pandas/core/resample.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -1430,11 +1430,15 @@ def __init__(
14301430
self.backward = False
14311431
elif backward:
14321432
if origin in ("start", "start_day"):
1433-
raise ValueError(f"`start` or `start_day` origin isn't allowed when `backward` is True")
1433+
raise ValueError(
1434+
f"`start` or `start_day` origin isn't allowed when `backward` is True"
1435+
)
14341436
self.backward = backward
14351437
else:
14361438
if origin in ("end", "end_day"):
1437-
raise ValueError(f"`end` or `end_day` origin isn't allowed when `backward` is False")
1439+
raise ValueError(
1440+
f"`end` or `end_day` origin isn't allowed when `backward` is False"
1441+
)
14381442
self.backward = backward
14391443

14401444
try:
@@ -1785,7 +1789,13 @@ def _get_timestamp_range_edges(
17851789
origin = origin.tz_localize(None)
17861790

17871791
first, last = _adjust_dates_anchored(
1788-
first, last, freq, closed=closed, origin=origin, backward=backward, offset=offset
1792+
first,
1793+
last,
1794+
freq,
1795+
closed=closed,
1796+
origin=origin,
1797+
backward=backward,
1798+
offset=offset,
17891799
)
17901800
if isinstance(freq, Day):
17911801
first = first.tz_localize(index_tz)
@@ -1847,7 +1857,13 @@ def _get_period_range_edges(
18471857
adjust_last = freq.is_on_offset(last)
18481858

18491859
first, last = _get_timestamp_range_edges(
1850-
first, last, freq, closed=closed, origin=origin, backward=backward, offset=offset
1860+
first,
1861+
last,
1862+
freq,
1863+
closed=closed,
1864+
origin=origin,
1865+
backward=backward,
1866+
offset=offset,
18511867
)
18521868

18531869
first = (first + int(adjust_first) * freq).to_period(freq)
@@ -1873,7 +1889,7 @@ def _adjust_dates_anchored(
18731889
if origin == "end":
18741890
origin = last
18751891
elif origin == "end_day":
1876-
origin = last.ceil('D')
1892+
origin = last.ceil("D")
18771893
sub_freq_times = (origin.value - first.value) // freq.nanos
18781894
if closed == "left":
18791895
sub_freq_times += 1

0 commit comments

Comments
 (0)