Skip to content

Commit 63ee6bd

Browse files
author
Gabriel Corona
committed
BUG: fix size()/count() when resamping empty series (pandas-dev#28427)
1 parent 9ef67b1 commit 63ee6bd

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ Groupby/resample/rolling
210210
^^^^^^^^^^^^^^^^^^^^^^^^
211211

212212
-
213+
- Bug in :meth:`Resampler.use` and `Resampler.count()` when used with an empty `Series` (:issue:`28427`)
213214
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`)
214215
- Bug in :meth:`DataFrame.groupby` not offering selection by column name when ``axis=1`` (:issue:`27614`)
215216
- Bug in :meth:`DataFrameGroupby.agg` not able to use lambda function with named aggregation (:issue:`27519`)

pandas/core/resample.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,25 @@ def size(self):
860860
from pandas import Series
861861

862862
result = Series([], index=result.index, dtype="int64")
863+
elif not len(self.ax) and isinstance(self._selected_obj, ABCSeries):
864+
from pandas import Series
865+
866+
result = Series(
867+
[], index=result.index, dtype="int64", name=self._selected_obj.name
868+
)
869+
870+
return result
871+
872+
@Appender(GroupBy.size.__doc__)
873+
def count(self):
874+
result = self._downsample("count")
875+
if not len(self.ax) and isinstance(self._selected_obj, ABCSeries):
876+
from pandas import Series
877+
878+
result = Series(
879+
[], index=result.index, dtype="int64", name=self._selected_obj.name
880+
)
881+
863882
return result
864883

865884
def quantile(self, q=0.5, **kwargs):
@@ -907,14 +926,6 @@ def g(self, _method=method, *args, **kwargs):
907926
g.__doc__ = getattr(GroupBy, method).__doc__
908927
setattr(Resampler, method, g)
909928

910-
# groupby & aggregate methods
911-
for method in ["count"]:
912-
913-
def h(self, _method=method):
914-
return self._downsample(_method)
915-
916-
h.__doc__ = getattr(GroupBy, method).__doc__
917-
setattr(Resampler, method, h)
918929

919930
# series only methods
920931
for method in ["nunique"]:

pandas/tests/resample/test_base.py

+19
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ def test_resample_empty_series(freq, empty_series, resample_method):
118118
assert_series_equal(result, expected, check_dtype=False)
119119

120120

121+
@pytest.mark.parametrize("freq", ["M", "D", "H"])
122+
@pytest.mark.parametrize("resample_method", ["count", "size"])
123+
def test_resample_count_empty_series(freq, resample_method):
124+
# GH28427
125+
126+
empty_series = pd.Series([], dtype="datetime64[ns]", index=pd.DatetimeIndex([]))
127+
result = getattr(empty_series.resample(freq), resample_method)()
128+
129+
expected = pd.Series([], dtype="int64", index=pd.DatetimeIndex([]))
130+
if isinstance(empty_series.index, PeriodIndex):
131+
expected.index = empty_series.index.asfreq(freq=freq)
132+
else:
133+
expected.index = empty_series.index._shallow_copy(freq=freq)
134+
135+
assert_index_equal(result.index, expected.index)
136+
assert result.index.freq == expected.index.freq
137+
assert_series_equal(result, expected, check_dtype=True)
138+
139+
121140
@all_ts
122141
@pytest.mark.parametrize("freq", ["M", "D", "H"])
123142
def test_resample_empty_dataframe(empty_frame, freq, resample_method):

0 commit comments

Comments
 (0)