Skip to content

Commit 77fc4a3

Browse files
committed
doc added & tests fix
1 parent 7d8d67a commit 77fc4a3

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

doc/source/user_guide/timeseries.rst

+44
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,50 @@ Those two examples are equivalent for this time series:
18971897
18981898
Note the use of ``'start'`` for ``origin`` on the last example. In that case, ``origin`` will be set to the first value of the timeseries.
18991899

1900+
Backward resample
1901+
~~~~~~~~~~~~~~~~~
1902+
1903+
.. versionadded:: 1.2.0
1904+
1905+
``origin`` can not only make a foreward resample, namely grouping from the starting point with the given ``freq`` , but is also able to implement the backward resample. This method allows users to control bins of the grouping from the given origin with a backward direction. (:issue:`37804`)
1906+
1907+
.. ipython:: python
1908+
1909+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
1910+
rng = date_range(start, end, freq="7min")
1911+
ts = Series(np.arange(len(rng)) * 3, index=rng)
1912+
1913+
Setting ``offset='end'`` means using the max ``Timestamp`` as the ``origin`` with ``backward=True`` .
1914+
1915+
ts.index.max()
1916+
ts.resample("17min", origin="end").sum()
1917+
1918+
Setting ``offset='end'`` means using the ceiling midnight of the max ``Timestamp`` as the ``origin`` with ``backward=True`` .
1919+
1920+
.. ipython:: python
1921+
1922+
ts.resample("17min", origin="end").sum()
1923+
1924+
If you want to make the backward resample from a Timestamp-like ``origin`` , ``backward=True`` should be set.
1925+
1926+
.. ipython:: python
1927+
1928+
ts.resample("17min", origin="2000-10-02 00:40:00", backward=True).sum()
1929+
1930+
You can implement ``offset='end_day'`` in the following method equivalently.
1931+
1932+
.. ipython:: python
1933+
1934+
end_day_origin = ts.index.max().ceil("D")
1935+
end_day_origin
1936+
ts.resample("17min", origin=end_day_origin, backward=True).sum()
1937+
1938+
By defualt, backward resample uses ``closed=right`` while ``closed=left`` is also available.
1939+
1940+
.. ipython:: python
1941+
1942+
ts.resample("17min", closed="left", origin="end").sum()
1943+
19001944
.. _timeseries.periods:
19011945

19021946
Time span representation

doc/source/whatsnew/v1.2.0.rst

+44
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,50 @@ example where the index name is preserved:
203203
The same is true for :class:`MultiIndex`, but the logic is applied separately on a
204204
level-by-level basis.
205205

206+
.. _whatsnew_120.backward_resample:
207+
208+
Backward resample
209+
^^^^^^^^^^^^^^^^^
210+
211+
:class:`Grouper` and :meth:`DataFrame.resample` now support the argument ``backward`` . ``'end'`` and ``'end_day'`` are available in argument ``offset`` . Backward resample allows users to control bins of the grouping from the given origin with a backward direction. (:issue:`37804`)
212+
213+
.. ipython:: python
214+
215+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
216+
rng = date_range(start, end, freq="7min")
217+
ts = Series(np.arange(len(rng)) * 3, index=rng)
218+
219+
Setting ``offset='end'`` means using the max ``Timestamp`` as the ``origin`` with ``backward=True`` .
220+
221+
ts.index.max()
222+
ts.resample("17min", origin="end").sum()
223+
224+
Setting ``offset='end'`` means using the ceiling midnight of the max ``Timestamp`` as the ``origin`` with ``backward=True`` .
225+
226+
.. ipython:: python
227+
228+
ts.resample("17min", origin="end").sum()
229+
230+
If you want to make the backward resample from a Timestamp-like ``origin`` , ``backward=True`` should be set.
231+
232+
.. ipython:: python
233+
234+
ts.resample("17min", origin="2000-10-02 00:40:00", backward=True).sum()
235+
236+
You can implement ``offset='end_day'`` in the following method equivalently.
237+
238+
.. ipython:: python
239+
240+
end_day_origin = ts.index.max().ceil("D")
241+
end_day_origin
242+
ts.resample("17min", origin=end_day_origin, backward=True).sum()
243+
244+
By defualt, backward resample uses ``closed=right`` while ``closed=left`` is also available.
245+
246+
.. ipython:: python
247+
248+
ts.resample("17min", closed="left", origin="end").sum()
249+
206250
.. _whatsnew_120.enhancements.other:
207251

208252
Other enhancements

pandas/tests/resample/test_resample_api.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,12 @@ def test_resample_agg_readonly():
613613
tm.assert_series_equal(result, expected)
614614

615615

616-
# test data for backward resample GH#37804
617-
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
618-
rng = date_range(start, end, freq="7min")
619-
ts = Series(np.arange(len(rng)) * 3, index=rng)
620-
621-
622616
def test_backward_origin_consistency():
623617

618+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
619+
rng = date_range(start, end, freq="7min")
620+
ts = Series(np.arange(len(rng)) * 3, index=rng)
621+
624622
msg = "`start` or `start_day` origin isn't allowed when `backward` is True"
625623
with pytest.raises(ValueError, match=msg):
626624
ts.resample("1min", origin="start", backward=True)
@@ -631,6 +629,10 @@ def test_backward_origin_consistency():
631629

632630
def test_end_origin():
633631

632+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
633+
rng = date_range(start, end, freq="7min")
634+
ts = Series(np.arange(len(rng)) * 3, index=rng)
635+
634636
res = ts.resample("17min", origin="end").sum().astype("int64")
635637
data = [0, 18, 27, 63]
636638
expected = Series(
@@ -659,6 +661,10 @@ def test_end_origin():
659661

660662
def test_end_day_origin():
661663

664+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
665+
rng = date_range(start, end, freq="7min")
666+
ts = Series(np.arange(len(rng)) * 3, index=rng)
667+
662668
# 12 == 24 * 60 - 84 * 17 <= 26 (last value) <= 24 * 60 - 83 * 17 == 29
663669
res = ts.resample("17min", origin="end_day").sum().astype("int64")
664670
data = [3, 15, 45, 45]
@@ -676,6 +682,10 @@ def test_end_day_origin():
676682

677683
def test_backward_resample_with_datetime_origin():
678684

685+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
686+
rng = date_range(start, end, freq="7min")
687+
ts = Series(np.arange(len(rng)) * 3, index=rng)
688+
679689
res = (
680690
ts.resample(
681691
"17min",
@@ -721,6 +731,10 @@ def test_backward_resample_with_datetime_origin():
721731

722732
def test_left_and_right_close_in_backward_resample():
723733

734+
start, end = "2000-10-01 23:30:00", "2000-10-02 00:26:00"
735+
rng = date_range(start, end, freq="7min")
736+
ts = Series(np.arange(len(rng)) * 3, index=rng)
737+
724738
res = (
725739
ts.resample(
726740
"17min",

0 commit comments

Comments
 (0)