Skip to content

Commit 30e0146

Browse files
committed
Handle nonexistent end dates for resampling
1 parent 0cdc6a4 commit 30e0146

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ Groupby/resample/rolling
615615
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
616616
- 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`)
617617
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
618+
- Bug in :meth:`Series.resample` could raise when the the date range ended shortly before DST. (:issue:`58380`)
618619

619620
Reshaping
620621
^^^^^^^^^

pandas/core/resample.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,20 @@ 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+
print(f"{result=}")
972+
tm.assert_series_equal(result, expected)
973+
974+
961975
def test_resample_daily_anchored(unit):
962976
rng = date_range("1/1/2000 0:00:00", periods=10000, freq="min").as_unit(unit)
963977
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)

0 commit comments

Comments
 (0)