From b78bf6e5216603306ac13dc3112879e4881bf432 Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Tue, 20 Jun 2023 14:17:04 +0800 Subject: [PATCH 1/2] BUG: resampling empty series loses time zone from dtype --- pandas/core/resample.py | 10 +++++++--- pandas/tests/resample/test_datetime_index.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index a9faad4cbef6a..3fc0c47dd175e 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1892,7 +1892,9 @@ def _get_time_bins(self, ax: DatetimeIndex): ) if len(ax) == 0: - binner = labels = DatetimeIndex(data=[], freq=self.freq, name=ax.name) + binner = labels = DatetimeIndex( + data=[], freq=self.freq, name=ax.name, dtype=ax.dtype + ) return binner, [], labels first, last = _get_timestamp_range_edges( @@ -2023,8 +2025,10 @@ def _get_time_period_bins(self, ax: DatetimeIndex): freq = self.freq - if not len(ax): - binner = labels = PeriodIndex(data=[], freq=freq, name=ax.name) + if len(ax) == 0: + binner = labels = PeriodIndex( + data=[], freq=freq, name=ax.name, dtype=ax.dtype + ) return binner, [], labels labels = binner = period_range(start=ax[0], end=ax[-1], freq=freq, name=ax.name) diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index 389db2d36474d..0f52f2d1c65ee 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -1968,3 +1968,19 @@ def test_long_rule_non_nano(): ) expected = Series([1.0, 3.0, 6.5, 4.0, 3.0, 6.5, 4.0, 3.0, 6.5], index=expected_idx) tm.assert_series_equal(result, expected) + + +def test_resample_empty_series_with_tz(): + # GH#53664 + df = DataFrame({"ts": [], "values": []}).astype( + {"ts": "datetime64[ns, Atlantic/Faroe]"} + ) + result = df.resample("2MS", on="ts", closed="left", label="left", origin="start")[ + "values" + ].sum() + + expected_idx = DatetimeIndex( + [], freq="2MS", name="ts", dtype="datetime64[ns, Atlantic/Faroe]" + ) + expected = Series([], index=expected_idx, name="values", dtype="float64") + tm.assert_series_equal(result, expected) From 80ff8e3f571911a49b78af7f4364ee81590ada95 Mon Sep 17 00:00:00 2001 From: Charlie-XIAO Date: Tue, 20 Jun 2023 14:19:38 +0800 Subject: [PATCH 2/2] changelog added --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 1a9eac0fa0c07..b07584514c9b4 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -454,6 +454,7 @@ Plotting Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` in incorrectly allowing non-fixed ``freq`` when resampling on a :class:`TimedeltaIndex` (:issue:`51896`) +- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` losing time zone when resampling empty data (:issue:`53664`) - Bug in :meth:`DataFrameGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmax` return wrong dtype when used on empty DataFrameGroupBy or SeriesGroupBy (:issue:`51423`) - Bug in weighted rolling aggregations when specifying ``min_periods=0`` (:issue:`51449`) - Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby`, where, when the index of the