Skip to content

Commit 986f7c2

Browse files
committed
added explicit 'size' method and defined logic there
1 parent 6dc0961 commit 986f7c2

File tree

4 files changed

+22
-33
lines changed

4 files changed

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

448448

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

451451

452452
- Bug in ``Series`` constructor when both ``copy=True`` and ``dtype`` arguments are provided (:issue:`15125`)
@@ -460,11 +460,4 @@ Bug Fixes
460460
- Bug in ``pd.read_csv()`` with ``float_precision='round_trip'`` which caused a segfault when a text entry is parsed (:issue:`15140`)
461461

462462

463-
464-
465-
466-
467-
468-
469-
470463
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)

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

pandas/tseries/tests/test_resample.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ def test_resample_empty_series(self):
748748
assert_series_equal(result, expected, check_dtype=False,
749749
check_names=False)
750750
# this assert will break when fixed
751-
# self.assertTrue(result.name is None)
751+
self.assertTrue(result.name is None)
752752
else:
753753
assert_series_equal(result, expected, check_dtype=False)
754754

@@ -762,15 +762,22 @@ def test_resample_empty_dataframe(self):
762762
methods = downsample_methods + upsample_methods
763763
for method in methods:
764764
result = getattr(f.resample(freq), method)()
765-
766-
expected = pd.Series([])
765+
if method != 'size':
766+
expected = f.copy()
767+
assert_equal = assert_frame_equal
768+
else:
769+
# GH14962
770+
expected = Series([])
771+
assert_equal = assert_series_equal
772+
767773
expected.index = f.index._shallow_copy(freq=freq)
768774
assert_index_equal(result.index, expected.index)
769775
self.assertEqual(result.index.freq, expected.index.freq)
770-
assert_series_equal(result, expected, check_dtype=False)
776+
assert_equal(result, expected, check_dtype=False)
771777

772778
# test size for GH13212 (currently stays as df)
773779

780+
774781
def test_resample_empty_dtypes(self):
775782

776783
# Empty series were sometimes causing a segfault (for the functions
@@ -824,16 +831,6 @@ def test_resample_loffset_arg_type(self):
824831
assert_frame_equal(result_agg, expected)
825832
assert_frame_equal(result_how, expected)
826833

827-
def test_resample_empty_dataframe_with_size(self):
828-
# GH 14962
829-
index = pd.DatetimeIndex([], freq='M')
830-
df = pd.DataFrame([], index=index)
831-
832-
for freq in ['M', 'D', 'H']:
833-
result = df.resample(freq).size()
834-
expected = pd.Series([], index=index, dtype='int64')
835-
assert_series_equal(result, expected)
836-
837834

838835
class TestDatetimeIndex(Base, tm.TestCase):
839836
_multiprocess_can_split_ = True

0 commit comments

Comments
 (0)