Skip to content

Commit 13d968b

Browse files
authored
BUG: resampling empty series loses time zone from dtype (#53736)
* BUG: resampling empty series loses time zone from dtype * changelog added
1 parent 54af661 commit 13d968b

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ Plotting
459459
Groupby/resample/rolling
460460
^^^^^^^^^^^^^^^^^^^^^^^^
461461
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` in incorrectly allowing non-fixed ``freq`` when resampling on a :class:`TimedeltaIndex` (:issue:`51896`)
462+
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` losing time zone when resampling empty data (:issue:`53664`)
462463
- 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`)
463464
- Bug in weighted rolling aggregations when specifying ``min_periods=0`` (:issue:`51449`)
464465
- Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby`, where, when the index of the

pandas/core/resample.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,9 @@ def _get_time_bins(self, ax: DatetimeIndex):
18921892
)
18931893

18941894
if len(ax) == 0:
1895-
binner = labels = DatetimeIndex(data=[], freq=self.freq, name=ax.name)
1895+
binner = labels = DatetimeIndex(
1896+
data=[], freq=self.freq, name=ax.name, dtype=ax.dtype
1897+
)
18961898
return binner, [], labels
18971899

18981900
first, last = _get_timestamp_range_edges(
@@ -2023,8 +2025,10 @@ def _get_time_period_bins(self, ax: DatetimeIndex):
20232025

20242026
freq = self.freq
20252027

2026-
if not len(ax):
2027-
binner = labels = PeriodIndex(data=[], freq=freq, name=ax.name)
2028+
if len(ax) == 0:
2029+
binner = labels = PeriodIndex(
2030+
data=[], freq=freq, name=ax.name, dtype=ax.dtype
2031+
)
20282032
return binner, [], labels
20292033

20302034
labels = binner = period_range(start=ax[0], end=ax[-1], freq=freq, name=ax.name)

pandas/tests/resample/test_datetime_index.py

+16
Original file line numberDiff line numberDiff line change
@@ -1968,3 +1968,19 @@ def test_long_rule_non_nano():
19681968
)
19691969
expected = Series([1.0, 3.0, 6.5, 4.0, 3.0, 6.5, 4.0, 3.0, 6.5], index=expected_idx)
19701970
tm.assert_series_equal(result, expected)
1971+
1972+
1973+
def test_resample_empty_series_with_tz():
1974+
# GH#53664
1975+
df = DataFrame({"ts": [], "values": []}).astype(
1976+
{"ts": "datetime64[ns, Atlantic/Faroe]"}
1977+
)
1978+
result = df.resample("2MS", on="ts", closed="left", label="left", origin="start")[
1979+
"values"
1980+
].sum()
1981+
1982+
expected_idx = DatetimeIndex(
1983+
[], freq="2MS", name="ts", dtype="datetime64[ns, Atlantic/Faroe]"
1984+
)
1985+
expected = Series([], index=expected_idx, name="values", dtype="float64")
1986+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)