Skip to content

Commit d02ef6f

Browse files
discortjreback
authored andcommitted
BUG: Inconsistent return type for downsampling on resample of empty DataFrame (#15093)
closes #14692
1 parent 6401b82 commit d02ef6f

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Plotting
113113

114114
Groupby/Resample/Rolling
115115
^^^^^^^^^^^^^^^^^^^^^^^^
116+
- Bug in ``DataFrame.resample().size()`` where an empty DataFrame did not return a Series (:issue:`14962`)
116117

117118

118119

pandas/core/resample.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas.core.indexes.period import PeriodIndex, period_range
1818
import pandas.core.common as com
1919
import pandas.core.algorithms as algos
20+
from pandas.core.dtypes.generic import ABCDataFrame
2021

2122
import pandas.compat as compat
2223
from pandas.compat.numpy import function as nv
@@ -549,6 +550,15 @@ def var(self, ddof=1, *args, **kwargs):
549550
nv.validate_resampler_func('var', args, kwargs)
550551
return self._downsample('var', ddof=ddof)
551552

553+
@Appender(GroupBy.size.__doc__)
554+
def size(self):
555+
# It's a special case as higher level does return
556+
# a copy of 0-len objects. GH14962
557+
result = self._downsample('size')
558+
if not len(self.ax) and isinstance(self._selected_obj, ABCDataFrame):
559+
result = pd.Series([], index=result.index, dtype='int64')
560+
return result
561+
552562

553563
Resampler._deprecated_valids += dir(Resampler)
554564

@@ -563,8 +573,7 @@ def f(self, _method=method, *args, **kwargs):
563573
setattr(Resampler, method, f)
564574

565575
# groupby & aggregate methods
566-
for method in ['count', 'size']:
567-
576+
for method in ['count']:
568577
def f(self, _method=method):
569578
return self._downsample(_method)
570579
f.__doc__ = getattr(GroupBy, method).__doc__

pandas/tests/test_resample.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -783,15 +783,19 @@ def test_resample_empty_dataframe(self):
783783

784784
for freq in ['M', 'D', 'H']:
785785
# count retains dimensions too
786-
methods = downsample_methods + ['count']
786+
methods = downsample_methods + upsample_methods
787787
for method in methods:
788788
result = getattr(f.resample(freq), method)()
789+
if method != 'size':
790+
expected = f.copy()
791+
else:
792+
# GH14962
793+
expected = Series([])
789794

790-
expected = f.copy()
791795
expected.index = f.index._shallow_copy(freq=freq)
792796
assert_index_equal(result.index, expected.index)
793797
assert result.index.freq == expected.index.freq
794-
assert_frame_equal(result, expected, check_dtype=False)
798+
assert_almost_equal(result, expected, check_dtype=False)
795799

796800
# test size for GH13212 (currently stays as df)
797801

0 commit comments

Comments
 (0)