Skip to content

Commit 766a77f

Browse files
committed
added explicit 'size' method and defined logic there
1 parent c1098da commit 766a77f

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
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-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ Bug Fixes
556556
- Bug in ``pd.merge_asof()`` where ``left_index``/``right_index`` together caused a failure when ``tolerance`` was specified (:issue:`15135`)
557557

558558

559-
559+
- Bug in ``resample().size()``. Inconsistent return type on resample of empty DataFrame (:issue:`14962`)
560560

561561

562562
- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)

pandas/tests/tseries/test_resample.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ def test_resample_empty_series(self):
760760
assert_series_equal(result, expected, check_dtype=False,
761761
check_names=False)
762762
# this assert will break when fixed
763-
# self.assertTrue(result.name is None)
763+
self.assertTrue(result.name is None)
764764
else:
765765
assert_series_equal(result, expected, check_dtype=False)
766766

@@ -774,15 +774,22 @@ def test_resample_empty_dataframe(self):
774774
methods = downsample_methods + upsample_methods
775775
for method in methods:
776776
result = getattr(f.resample(freq), method)()
777-
778-
expected = pd.Series([])
777+
if method != 'size':
778+
expected = f.copy()
779+
assert_equal = assert_frame_equal
780+
else:
781+
# GH14962
782+
expected = Series([])
783+
assert_equal = assert_series_equal
784+
779785
expected.index = f.index._shallow_copy(freq=freq)
780786
assert_index_equal(result.index, expected.index)
781787
self.assertEqual(result.index.freq, expected.index.freq)
782-
assert_series_equal(result, expected, check_dtype=False)
788+
assert_equal(result, expected, check_dtype=False)
783789

784790
# test size for GH13212 (currently stays as df)
785791

792+
786793
def test_resample_empty_dtypes(self):
787794

788795
# Empty series were sometimes causing a segfault (for the functions
@@ -836,16 +843,6 @@ def test_resample_loffset_arg_type(self):
836843
assert_frame_equal(result_agg, expected)
837844
assert_frame_equal(result_how, expected)
838845

839-
def test_resample_empty_dataframe_with_size(self):
840-
# GH 14962
841-
index = pd.DatetimeIndex([], freq='M')
842-
df = pd.DataFrame([], index=index)
843-
844-
for freq in ['M', 'D', 'H']:
845-
result = df.resample(freq).size()
846-
expected = pd.Series([], index=index, dtype='int64')
847-
assert_series_equal(result, expected)
848-
849846

850847
class TestDatetimeIndex(Base, tm.TestCase):
851848
_index_factory = lambda x: date_range

pandas/tseries/resample.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pandas.tseries.period import PeriodIndex, period_range
1717
import pandas.core.common as com
1818
import pandas.core.algorithms as algos
19+
from pandas.types.generic import ABCDataFrame
1920

2021
import pandas.compat as compat
2122
from pandas.compat.numpy import function as nv
@@ -552,6 +553,14 @@ def var(self, ddof=1, *args, **kwargs):
552553
"""
553554
nv.validate_resampler_func('var', args, kwargs)
554555
return self._downsample('var', ddof=ddof)
556+
557+
@Appender(GroupBy.size.__doc__)
558+
def size(self):
559+
# It 'seems' special and needs extra handling. GH14962
560+
result = self._downsample('size')
561+
if not len(self.ax) and isinstance(self._selected_obj, ABCDataFrame):
562+
result = pd.Series([], index=result.index, dtype='int64')
563+
return result
555564
Resampler._deprecated_valids += dir(Resampler)
556565

557566
# downsample methods
@@ -565,8 +574,7 @@ def f(self, _method=method, *args, **kwargs):
565574
setattr(Resampler, method, f)
566575

567576
# groupby & aggregate methods
568-
for method in ['count', 'size']:
569-
577+
for method in ['count']:
570578
def f(self, _method=method):
571579
return self._downsample(_method)
572580
f.__doc__ = getattr(GroupBy, method).__doc__
@@ -772,14 +780,6 @@ def _wrap_result(self, result):
772780
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
773781
result.index = result.index.to_period(self.freq)
774782

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

785785

0 commit comments

Comments
 (0)