Skip to content

Commit ce1a7cc

Browse files
Gabriel CoronaGabriel Corona
Gabriel Corona
authored and
Gabriel Corona
committed
BUG: fix wrong dtype for size()/count() when resampling empty series (pandas-dev#28427)
1 parent 9ef67b1 commit ce1a7cc

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
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.size()` and `Resampler.count()` returning wrong `dtype` 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

+24-10
Original file line numberDiff line numberDiff line change
@@ -856,10 +856,32 @@ def size(self):
856856
# It's a special case as higher level does return
857857
# a copy of 0-len objects. GH14962
858858
result = self._downsample("size")
859-
if not len(self.ax) and isinstance(self._selected_obj, ABCDataFrame):
859+
if not len(self.ax):
860+
if isinstance(self._selected_obj, ABCDataFrame):
861+
from pandas import Series
862+
863+
result = Series([], index=result.index, dtype="int64")
864+
elif isinstance(self._selected_obj, ABCSeries):
865+
from pandas import Series
866+
867+
result = Series(
868+
[], index=result.index, dtype="int64", name=self._selected_obj.name
869+
)
870+
else:
871+
raise AssertionError("Unexpected type result")
872+
873+
return result
874+
875+
@Appender(GroupBy.count.__doc__)
876+
def count(self):
877+
result = self._downsample("count")
878+
if not len(self.ax) and isinstance(self._selected_obj, ABCSeries):
860879
from pandas import Series
861880

862-
result = Series([], index=result.index, dtype="int64")
881+
result = Series(
882+
[], index=result.index, dtype="int64", name=self._selected_obj.name
883+
)
884+
863885
return result
864886

865887
def quantile(self, q=0.5, **kwargs):
@@ -907,14 +929,6 @@ def g(self, _method=method, *args, **kwargs):
907929
g.__doc__ = getattr(GroupBy, method).__doc__
908930
setattr(Resampler, method, g)
909931

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)
918932

919933
# series only methods
920934
for method in ["nunique"]:

pandas/tests/resample/test_base.py

+14
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ 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+
expected.index = empty_series.index._shallow_copy(freq=freq)
131+
132+
assert_series_equal(result, expected)
133+
134+
121135
@all_ts
122136
@pytest.mark.parametrize("freq", ["M", "D", "H"])
123137
def test_resample_empty_dataframe(empty_frame, freq, resample_method):

0 commit comments

Comments
 (0)