Skip to content

BUG: Inconsistent return type for downsampling on resample of empty DataFrame #15093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Plotting

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



Expand Down
13 changes: 11 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pandas.core.indexes.period import PeriodIndex, period_range
import pandas.core.common as com
import pandas.core.algorithms as algos
from pandas.core.dtypes.generic import ABCDataFrame

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

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


Resampler._deprecated_valids += dir(Resampler)

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

# groupby & aggregate methods
for method in ['count', 'size']:

for method in ['count']:
def f(self, _method=method):
return self._downsample(_method)
f.__doc__ = getattr(GroupBy, method).__doc__
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,15 +783,19 @@ def test_resample_empty_dataframe(self):

for freq in ['M', 'D', 'H']:
# count retains dimensions too
methods = downsample_methods + ['count']
methods = downsample_methods + upsample_methods
for method in methods:
result = getattr(f.resample(freq), method)()
if method != 'size':
expected = f.copy()
else:
# GH14962
expected = Series([])

expected = f.copy()
expected.index = f.index._shallow_copy(freq=freq)
assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
assert_frame_equal(result, expected, check_dtype=False)
assert_almost_equal(result, expected, check_dtype=False)

# test size for GH13212 (currently stays as df)

Expand Down