Skip to content

Commit cf9ec78

Browse files
mroeschkeMatt Roeschke
and
Matt Roeschke
authored
BUG: DataFrame.resample raised AmbiguousTimeError at a midnight DST transition (#33137)
* BUG: DataFrame.resample raised AmbiguousTimeError at a midnight DST transition * whatsnew grammar Co-authored-by: Matt Roeschke <[email protected]>
1 parent 02a134b commit cf9ec78

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ Groupby/resample/rolling
423423
- Bug in :meth:`GroupBy.apply` raises ``ValueError`` when the ``by`` axis is not sorted and has duplicates and the applied ``func`` does not mutate passed in objects (:issue:`30667`)
424424
- Bug in :meth:`DataFrameGroupby.transform` produces incorrect result with transformation functions (:issue:`30918`)
425425
- Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` produces inconsistent type when aggregating Boolean series (:issue:`32894`)
426-
426+
- Bug in :meth:`DataFrame.resample` where an ``AmbiguousTimeError`` would be raised when the resulting timezone aware :class:`DatetimeIndex` had a DST transition at midnight (:issue:`25758`)
427427

428428
Reshaping
429429
^^^^^^^^^

pandas/core/resample.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1422,13 +1422,15 @@ def _get_time_bins(self, ax):
14221422
# because replace() will swallow the nanosecond part
14231423
# thus last bin maybe slightly before the end if the end contains
14241424
# nanosecond part and lead to `Values falls after last bin` error
1425+
# GH 25758: If DST lands at midnight (e.g. 'America/Havana'), user feedback
1426+
# has noted that ambiguous=True provides the most sensible result
14251427
binner = labels = date_range(
14261428
freq=self.freq,
14271429
start=first,
14281430
end=last,
14291431
tz=ax.tz,
14301432
name=ax.name,
1431-
ambiguous="infer",
1433+
ambiguous=True,
14321434
nonexistent="shift_forward",
14331435
)
14341436

pandas/tests/resample/test_datetime_index.py

+18
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,24 @@ def test_downsample_across_dst_weekly():
14401440
tm.assert_series_equal(result, expected)
14411441

14421442

1443+
def test_downsample_dst_at_midnight():
1444+
# GH 25758
1445+
start = datetime(2018, 11, 3, 12)
1446+
end = datetime(2018, 11, 5, 12)
1447+
index = pd.date_range(start, end, freq="1H")
1448+
index = index.tz_localize("UTC").tz_convert("America/Havana")
1449+
data = list(range(len(index)))
1450+
dataframe = pd.DataFrame(data, index=index)
1451+
result = dataframe.groupby(pd.Grouper(freq="1D")).mean()
1452+
expected = DataFrame(
1453+
[7.5, 28.0, 44.5],
1454+
index=date_range("2018-11-03", periods=3).tz_localize(
1455+
"America/Havana", ambiguous=True
1456+
),
1457+
)
1458+
tm.assert_frame_equal(result, expected)
1459+
1460+
14431461
def test_resample_with_nat():
14441462
# GH 13020
14451463
index = DatetimeIndex(

0 commit comments

Comments
 (0)