Skip to content

BUG: resampling empty series loses time zone from dtype #53736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,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
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)