Skip to content

Commit f4845eb

Browse files
Gabriel CoronaGabriel Corona
Gabriel Corona
authored and
Gabriel Corona
committed
BUG: fix dtype for .resample().size()/count() of empty series/dataframe (#28427)
1 parent 29be383 commit f4845eb

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ Groupby/resample/rolling
424424

425425
-
426426
- Bug in :meth:`DataFrame.groupby` with multiple groups where an ``IndexError`` would be raised if any group contained all NA values (:issue:`20519`)
427+
- Bug in :meth:`pandas.core.resample.Resampler.size` and :meth:`pandas.core.resample.Resampler.count` returning wrong dtype when used with an empty series or dataframe (:issue:`28427`)
427428
- Bug in :meth:`DataFrame.rolling` not allowing for rolling over datetimes when ``axis=1`` (:issue: `28192`)
428429
- Bug in :meth:`DataFrame.rolling` not allowing rolling over multi-index levels (:issue: `15584`).
429430
- Bug in :meth:`DataFrame.rolling` not allowing rolling on monotonic decreasing time indexes (:issue: `19248`).

pandas/core/resample.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -869,13 +869,32 @@ def var(self, ddof=1, *args, **kwargs):
869869

870870
@Appender(GroupBy.size.__doc__)
871871
def size(self):
872-
# It's a special case as higher level does return
873-
# a copy of 0-len objects. GH14962
874872
result = self._downsample("size")
875-
if not len(self.ax) and isinstance(self._selected_obj, ABCDataFrame):
873+
if not len(self.ax):
876874
from pandas import Series
877875

878-
result = Series([], index=result.index, dtype="int64")
876+
if self._selected_obj.ndim == 1:
877+
name = self._selected_obj.name
878+
else:
879+
name = None
880+
result = Series([], index=result.index, dtype="int64", name=name)
881+
return result
882+
883+
@Appender(GroupBy.count.__doc__)
884+
def count(self):
885+
result = self._downsample("count")
886+
if not len(self.ax):
887+
if self._selected_obj.ndim == 1:
888+
result = self._selected_obj.__class__(
889+
[], index=result.index, dtype="int64", name=self._selected_obj.name
890+
)
891+
else:
892+
from pandas import DataFrame
893+
894+
result = DataFrame(
895+
[], index=result.index, columns=result.columns, dtype="int64"
896+
)
897+
879898
return result
880899

881900
def quantile(self, q=0.5, **kwargs):
@@ -923,14 +942,6 @@ def g(self, _method=method, *args, **kwargs):
923942
g.__doc__ = getattr(GroupBy, method).__doc__
924943
setattr(Resampler, method, g)
925944

926-
# groupby & aggregate methods
927-
for method in ["count"]:
928-
929-
def h(self, _method=method):
930-
return self._downsample(_method)
931-
932-
h.__doc__ = getattr(GroupBy, method).__doc__
933-
setattr(Resampler, method, h)
934945

935946
# series only methods
936947
for method in ["nunique"]:

pandas/tests/resample/test_base.py

+43
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ def test_resample_empty_series(freq, empty_series, resample_method):
112112
tm.assert_series_equal(result, expected, check_dtype=False)
113113

114114

115+
@pytest.mark.parametrize("freq", ["M", "D", "H"])
116+
@pytest.mark.parametrize("resample_method", ["count", "size"])
117+
def test_resample_count_empty_series(freq, resample_method):
118+
# GH28427
119+
120+
empty_series = pd.Series([], dtype="datetime64[ns]", index=pd.DatetimeIndex([]))
121+
result = getattr(empty_series.resample(freq), resample_method)()
122+
123+
expected = pd.Series([], dtype="int64", index=pd.DatetimeIndex([], freq=freq))
124+
125+
tm.assert_series_equal(result, expected)
126+
127+
115128
@all_ts
116129
@pytest.mark.parametrize("freq", ["M", "D", "H"])
117130
def test_resample_empty_dataframe(empty_frame, freq, resample_method):
@@ -136,6 +149,36 @@ def test_resample_empty_dataframe(empty_frame, freq, resample_method):
136149
# test size for GH13212 (currently stays as df)
137150

138151

152+
@pytest.mark.parametrize("freq", ["M", "D", "H"])
153+
def test_resample_count_empty_dataframe(freq):
154+
# GH28427
155+
156+
empty_dataframe = pd.DataFrame(
157+
{"a": []}, dtype="datetime64[ns]", index=pd.DatetimeIndex([])
158+
)
159+
result = empty_dataframe.resample(freq).count()
160+
161+
expected = pd.DataFrame(
162+
{"a": []}, dtype="int64", index=pd.DatetimeIndex([], freq=freq)
163+
)
164+
165+
tm.assert_frame_equal(result, expected)
166+
167+
168+
@pytest.mark.parametrize("freq", ["M", "D", "H"])
169+
def test_resample_size_empty_dataframe(freq):
170+
# GH28427
171+
172+
empty_dataframe = pd.DataFrame(
173+
{"a": []}, dtype="datetime64[ns]", index=pd.DatetimeIndex([])
174+
)
175+
result = empty_dataframe.resample(freq).size()
176+
177+
expected = pd.Series([], dtype="int64", index=pd.DatetimeIndex([], freq=freq))
178+
179+
tm.assert_series_equal(result, expected)
180+
181+
139182
@pytest.mark.parametrize("index", tm.all_timeseries_index_generator(0))
140183
@pytest.mark.parametrize("dtype", [np.float, np.int, np.object, "datetime64[ns]"])
141184
def test_resample_empty_dtypes(index, dtype, resample_method):

0 commit comments

Comments
 (0)