Skip to content

Commit 328e79d

Browse files
authored
BUG: Handle nonexistent end dates when resampling (#59471)
1 parent 13c4069 commit 328e79d

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ Groupby/resample/rolling
650650
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
651651
- Bug in :meth:`DataFrameGroupby.transform` and :meth:`SeriesGroupby.transform` with a reducer and ``observed=False`` that coerces dtype to float when there are unobserved categories. (:issue:`55326`)
652652
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
653+
- Bug in :meth:`Series.resample` could raise when the the date range ended shortly before a non-existent time. (:issue:`58380`)
653654

654655
Reshaping
655656
^^^^^^^^^

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ def _get_timestamp_range_edges(
24662466
)
24672467
if isinstance(freq, Day):
24682468
first = first.tz_localize(index_tz)
2469-
last = last.tz_localize(index_tz)
2469+
last = last.tz_localize(index_tz, nonexistent="shift_forward")
24702470
else:
24712471
first = first.normalize()
24722472
last = last.normalize()

pandas/tests/resample/test_datetime_index.py

+13
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,19 @@ def _create_series(values, timestamps, freq="D"):
958958
tm.assert_series_equal(result, expected)
959959

960960

961+
def test_resample_dst_midnight_last_nonexistent():
962+
# GH 58380
963+
ts = Series(
964+
1,
965+
date_range("2024-04-19", "2024-04-20", tz="Africa/Cairo", freq="15min"),
966+
)
967+
968+
expected = Series([len(ts)], index=DatetimeIndex([ts.index[0]], freq="7D"))
969+
970+
result = ts.resample("7D").sum()
971+
tm.assert_series_equal(result, expected)
972+
973+
961974
def test_resample_daily_anchored(unit):
962975
rng = date_range("1/1/2000 0:00:00", periods=10000, freq="min").as_unit(unit)
963976
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)

0 commit comments

Comments
 (0)