Skip to content

Commit 427234f

Browse files
committed
added test for size() method
1 parent b2c416b commit 427234f

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1562,5 +1562,4 @@ Bug Fixes
15621562
- Bugs in ``stack``, ``get_dummies``, ``make_axis_dummies`` which don't preserve categorical dtypes in (multi)indexes (:issue:`13854`)
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`)
1565-
- 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`)
1565+
- Bug in ``Index.copy()`` where ``name`` parameter was ignored (:issue:`14302`)

doc/source/whatsnew/v0.20.0.txt

+2-5
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,8 @@ Bug Fixes
457457
- Bug in ``pd.read_csv()`` with ``float_precision='round_trip'`` which caused a segfault when a text entry is parsed (:issue:`15140`)
458458

459459

460+
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
460461

461462

462463

463-
464-
465-
466-
467-
- Bug in ``DataFrame.boxplot`` where ``fontsize`` was not applied to the tick labels on both axes (:issue:`15108`)
464+
- Bug in ``resample().size()``. Inconsistent return type on resample of empty DataFrame (:issue:`14962`)

pandas/tseries/resample.py

+19-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
@@ -444,6 +445,17 @@ def _wrap_result(self, result):
444445

445446
return result
446447

448+
def _assure_downsample(self, _method):
449+
"""Assure that downsample is done with the correct type"""
450+
result = self._downsample(_method)
451+
452+
# Make consistent type of result. GH14962
453+
if not len(self.ax) and isinstance(result, ABCDataFrame) and \
454+
isinstance(result.index, (DatetimeIndex, TimedeltaIndex)):
455+
result = pd.Series([], index=result.index, dtype='int64')
456+
457+
return result
458+
447459
def pad(self, limit=None):
448460
"""
449461
Forward fill the values
@@ -567,8 +579,13 @@ def f(self, _method=method, *args, **kwargs):
567579
# groupby & aggregate methods
568580
for method in ['count', 'size']:
569581

570-
def f(self, _method=method):
571-
return self._downsample(_method)
582+
if method == "size":
583+
def f(self, _method=method):
584+
return self._assure_downsample(_method)
585+
else:
586+
def f(self, _method=method):
587+
return self._downsample(_method)
588+
572589
f.__doc__ = getattr(GroupBy, method).__doc__
573590
setattr(Resampler, method, f)
574591

@@ -772,14 +789,6 @@ def _wrap_result(self, result):
772789
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
773790
result.index = result.index.to_period(self.freq)
774791

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')
783792
return result
784793

785794

pandas/tseries/tests/test_resample.py

+24-15
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

@@ -759,18 +759,37 @@ def test_resample_empty_dataframe(self):
759759

760760
for freq in ['M', 'D', 'H']:
761761
# count retains dimensions too
762-
methods = downsample_methods + upsample_methods
762+
methods = downsample_methods + ['count']
763763
for method in methods:
764764
result = getattr(f.resample(freq), method)()
765-
766-
expected = pd.Series([])
765+
expected = f.copy()
767766
expected.index = f.index._shallow_copy(freq=freq)
768767
assert_index_equal(result.index, expected.index)
769768
self.assertEqual(result.index.freq, expected.index.freq)
770-
assert_series_equal(result, expected, check_dtype=False)
769+
assert_frame_equal(result, expected, check_dtype=False)
771770

772771
# test size for GH13212 (currently stays as df)
773772

773+
def test_resample_size_empty_dataframe(self):
774+
# GH14962
775+
index = self.create_series().index[:0]
776+
f = DataFrame(index=index)
777+
778+
for freq in ['M', 'D', 'H']:
779+
result = f.resample(freq).size()
780+
781+
if isinstance(result.index, PeriodIndex) and freq == 'H':
782+
expected = f.copy()
783+
assert_equal = assert_frame_equal
784+
else:
785+
expected = Series([])
786+
assert_equal = assert_series_equal
787+
788+
expected.index = f.index._shallow_copy(freq=freq)
789+
assert_index_equal(result.index, expected.index)
790+
self.assertEqual(result.index.freq, expected.index.freq)
791+
assert_equal(result, expected, check_dtype=False)
792+
774793
def test_resample_empty_dtypes(self):
775794

776795
# Empty series were sometimes causing a segfault (for the functions
@@ -824,16 +843,6 @@ def test_resample_loffset_arg_type(self):
824843
assert_frame_equal(result_agg, expected)
825844
assert_frame_equal(result_how, expected)
826845

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-
837846

838847
class TestDatetimeIndex(Base, tm.TestCase):
839848
_multiprocess_can_split_ = True

0 commit comments

Comments
 (0)