Skip to content

Commit 37ba820

Browse files
committed
added explicit 'size' method and defined logic there
1 parent 8f7b437 commit 37ba820

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

doc/source/whatsnew/v0.19.0.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1563,4 +1563,3 @@ Bug Fixes
15631563
- ``PeriodIndex`` can now accept ``list`` and ``array`` which contains ``pd.NaT`` (:issue:`13430`)
15641564
- Bug in ``df.groupby`` where ``.median()`` returns arbitrary values if grouped dataframe contains empty bins (:issue:`13629`)
15651565
- Bug in ``Index.copy()`` where ``name`` parameter was ignored (:issue:`14302`)
1566-
- Bug in ``_downsample()``. Inconsistent return type on resample of empty DataFrame (:issue:`14962`)

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ Groupby/Resample/Rolling
16851685
- Bug in ``.rolling()`` where ``pd.Timedelta`` or ``datetime.timedelta`` was not accepted as a ``window`` argument (:issue:`15440`)
16861686
- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)
16871687
- Bug in ``DataFrame.resample().median()`` if duplicate column names are present (:issue:`14233`)
1688+
- Bug in ``resample().size()``. Inconsistent return type on resample of empty DataFrame (:issue:`14962`)
16881689

16891690
Sparse
16901691
^^^^^^

pandas/core/resample.py

+9-11
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.types.generic import ABCDataFrame
2021

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

552-
553+
@Appender(GroupBy.size.__doc__)
554+
def size(self):
555+
# It 'seems' special and needs extra handling. GH14962
556+
result = self._downsample('size')
557+
if not len(self.ax) and isinstance(self._selected_obj, ABCDataFrame):
558+
result = pd.Series([], index=result.index, dtype='int64')
559+
return result
553560
Resampler._deprecated_valids += dir(Resampler)
554561

555562
# downsample methods
@@ -563,8 +570,7 @@ def f(self, _method=method, *args, **kwargs):
563570
setattr(Resampler, method, f)
564571

565572
# groupby & aggregate methods
566-
for method in ['count', 'size']:
567-
573+
for method in ['count']:
568574
def f(self, _method=method):
569575
return self._downsample(_method)
570576
f.__doc__ = getattr(GroupBy, method).__doc__
@@ -770,14 +776,6 @@ def _wrap_result(self, result):
770776
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
771777
result.index = result.index.to_period(self.freq)
772778

773-
# Make consistent type of result. GH14962
774-
if not len(self.ax):
775-
grouper = BinGrouper([], result.index)
776-
grouped = self._selected_obj.groupby(grouper)
777-
result = pd.Series([],
778-
index=result.index,
779-
name=grouped.name,
780-
dtype='int64')
781779
return result
782780

783781

pandas/tests/test_resample.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,22 @@ def test_resample_empty_dataframe(self):
796796
methods = downsample_methods + upsample_methods
797797
for method in methods:
798798
result = getattr(f.resample(freq), method)()
799-
800-
expected = pd.Series([])
799+
if method != 'size':
800+
expected = f.copy()
801+
assert_equal = assert_frame_equal
802+
else:
803+
# GH14962
804+
expected = Series([])
805+
assert_equal = assert_series_equal
806+
801807
expected.index = f.index._shallow_copy(freq=freq)
802808
assert_index_equal(result.index, expected.index)
803809
assert result.index.freq == expected.index.freq
804810
assert_frame_equal(result, expected, check_dtype=False)
805811

806812
# test size for GH13212 (currently stays as df)
807813

814+
808815
def test_resample_empty_dtypes(self):
809816

810817
# Empty series were sometimes causing a segfault (for the functions
@@ -858,16 +865,6 @@ def test_resample_loffset_arg_type(self):
858865
assert_frame_equal(result_agg, expected)
859866
assert_frame_equal(result_how, expected)
860867

861-
def test_resample_empty_dataframe_with_size(self):
862-
# GH 14962
863-
index = pd.DatetimeIndex([], freq='M')
864-
df = pd.DataFrame([], index=index)
865-
866-
for freq in ['M', 'D', 'H']:
867-
result = df.resample(freq).size()
868-
expected = pd.Series([], index=index, dtype='int64')
869-
assert_series_equal(result, expected)
870-
871868

872869
class TestDatetimeIndex(Base):
873870
_index_factory = lambda x: date_range

0 commit comments

Comments
 (0)