Skip to content

Commit

Permalink
BUG: resampling empty series loses time zone from dtype (pandas-dev#5…
Browse files Browse the repository at this point in the history
…3736)

* BUG: resampling empty series loses time zone from dtype

* changelog added
  • Loading branch information
Charlie-XIAO authored and root committed Jun 23, 2023
1 parent 525ece3 commit 8b12efd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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 @@ -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
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)

0 comments on commit 8b12efd

Please sign in to comment.