Skip to content

Commit 06b3f5d

Browse files
authored
Resample fix dst transition (#36264)
1 parent 4dc5887 commit 06b3f5d

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ Groupby/resample/rolling
308308

309309
- Bug in :meth:`DataFrameGroupBy.count` and :meth:`SeriesGroupBy.sum` returning ``NaN`` for missing categories when grouped on multiple ``Categoricals``. Now returning ``0`` (:issue:`35028`)
310310
- Bug in :meth:`DataFrameGroupBy.apply` that would some times throw an erroneous ``ValueError`` if the grouping axis had duplicate entries (:issue:`16646`)
311+
- Bug in :meth:`DataFrame.resample(...)` that would throw a ``ValueError`` when resampling from "D" to "24H" over a transition into daylight savings time (DST) (:issue:`35219`)
311312
- Bug when combining methods :meth:`DataFrame.groupby` with :meth:`DataFrame.resample` and :meth:`DataFrame.interpolate` raising an ``TypeError`` (:issue:`35325`)
312313
- Bug in :meth:`DataFrameGroupBy.apply` where a non-nuisance grouping column would be dropped from the output columns if another groupby method was called before ``.apply()`` (:issue:`34656`)
313314
- Bug in :meth:`DataFrameGroupby.apply` would drop a :class:`CategoricalIndex` when grouped on. (:issue:`35792`)

pandas/core/resample.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,11 @@ def _upsample(self, method, limit=None, fill_value=None):
10871087
res_index = self._adjust_binner_for_upsample(binner)
10881088

10891089
# if we have the same frequency as our axis, then we are equal sampling
1090-
if limit is None and to_offset(ax.inferred_freq) == self.freq:
1090+
if (
1091+
limit is None
1092+
and to_offset(ax.inferred_freq) == self.freq
1093+
and len(obj) == len(res_index)
1094+
):
10911095
result = obj.copy()
10921096
result.index = res_index
10931097
else:

pandas/tests/resample/test_datetime_index.py

+47
Original file line numberDiff line numberDiff line change
@@ -1740,3 +1740,50 @@ def test_resample_apply_product():
17401740
columns=["A", "B"],
17411741
)
17421742
tm.assert_frame_equal(result, expected)
1743+
1744+
1745+
@pytest.mark.parametrize(
1746+
"first,last,freq_in,freq_out,exp_last",
1747+
[
1748+
(
1749+
"2020-03-28",
1750+
"2020-03-31",
1751+
"D",
1752+
"24H",
1753+
"2020-03-30 01:00",
1754+
), # includes transition into DST
1755+
(
1756+
"2020-03-28",
1757+
"2020-10-27",
1758+
"D",
1759+
"24H",
1760+
"2020-10-27 00:00",
1761+
), # includes transition into and out of DST
1762+
(
1763+
"2020-10-25",
1764+
"2020-10-27",
1765+
"D",
1766+
"24H",
1767+
"2020-10-26 23:00",
1768+
), # includes transition out of DST
1769+
(
1770+
"2020-03-28",
1771+
"2020-03-31",
1772+
"24H",
1773+
"D",
1774+
"2020-03-30 00:00",
1775+
), # same as above, but from 24H to D
1776+
("2020-03-28", "2020-10-27", "24H", "D", "2020-10-27 00:00"),
1777+
("2020-10-25", "2020-10-27", "24H", "D", "2020-10-26 00:00"),
1778+
],
1779+
)
1780+
def test_resample_calendar_day_with_dst(
1781+
first: str, last: str, freq_in: str, freq_out: str, exp_last: str
1782+
):
1783+
# GH 35219
1784+
ts = pd.Series(1.0, pd.date_range(first, last, freq=freq_in, tz="Europe/Amsterdam"))
1785+
result = ts.resample(freq_out).pad()
1786+
expected = pd.Series(
1787+
1.0, pd.date_range(first, exp_last, freq=freq_out, tz="Europe/Amsterdam")
1788+
)
1789+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)