From 8b12efd43bf24bf5bb87cbef14a80a82523f7520 Mon Sep 17 00:00:00 2001 From: Yao Xiao <108576690+Charlie-XIAO@users.noreply.github.com> Date: Wed, 21 Jun 2023 01:15:34 +0800 Subject: [PATCH] BUG: resampling empty series loses time zone from dtype (#53736) * BUG: resampling empty series loses time zone from dtype * changelog added --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/resample.py | 10 +++++++--- pandas/tests/resample/test_datetime_index.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index d0d98e08998f1..ac4fb6838b9ce 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -459,6 +459,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 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)