Skip to content

Commit c1098da

Browse files
committed
BUG: added series type to wrap_result for empty DataFrame
1 parent d14d821 commit c1098da

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1563,3 +1563,4 @@ 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`)

pandas/tests/tseries/test_resample.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
DatetimeIndexResampler)
2626
from pandas.tseries.tdi import timedelta_range, TimedeltaIndex
2727
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
28-
assert_frame_equal, assert_index_equal,
29-
assertIsInstance)
28+
assert_frame_equal, assert_index_equal)
3029
from pandas._period import IncompatibleFrequency
3130

3231
bday = BDay()
@@ -761,7 +760,7 @@ def test_resample_empty_series(self):
761760
assert_series_equal(result, expected, check_dtype=False,
762761
check_names=False)
763762
# this assert will break when fixed
764-
self.assertTrue(result.name is None)
763+
# self.assertTrue(result.name is None)
765764
else:
766765
assert_series_equal(result, expected, check_dtype=False)
767766

@@ -772,15 +771,15 @@ def test_resample_empty_dataframe(self):
772771

773772
for freq in ['M', 'D', 'H']:
774773
# count retains dimensions too
775-
methods = downsample_methods + ['count']
774+
methods = downsample_methods + upsample_methods
776775
for method in methods:
777776
result = getattr(f.resample(freq), method)()
778777

779-
expected = f.copy()
778+
expected = pd.Series([])
780779
expected.index = f.index._shallow_copy(freq=freq)
781780
assert_index_equal(result.index, expected.index)
782781
self.assertEqual(result.index.freq, expected.index.freq)
783-
assert_frame_equal(result, expected, check_dtype=False)
782+
assert_series_equal(result, expected, check_dtype=False)
784783

785784
# test size for GH13212 (currently stays as df)
786785

@@ -839,11 +838,13 @@ def test_resample_loffset_arg_type(self):
839838

840839
def test_resample_empty_dataframe_with_size(self):
841840
# GH 14962
842-
df1 = pd.DataFrame(dict(a=range(100)),
843-
index=pd.date_range('1/1/2000', periods=100, freq="M"))
844-
df2 = df1[df1.a < 0]
845-
result = df2.resample("Q").size()
846-
assertIsInstance(result, pd.Series)
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)
847848

848849

849850
class TestDatetimeIndex(Base, tm.TestCase):

pandas/tseries/resample.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -699,12 +699,8 @@ def _downsample(self, how, **kwargs):
699699
if not len(ax):
700700
# reset to the new freq
701701
obj = obj.copy()
702-
if how == "size" and isinstance(obj, pd.DataFrame):
703-
obj = obj.groupby(
704-
self.grouper, axis=self.axis).aggregate(how, **kwargs)
705-
706702
obj.index.freq = self.freq
707-
return obj
703+
return self._wrap_result(obj)
708704

709705
# do we have a regular frequency
710706
if ax.freq is not None or ax.inferred_freq is not None:
@@ -775,6 +771,15 @@ def _wrap_result(self, result):
775771
# convert if needed
776772
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
777773
result.index = result.index.to_period(self.freq)
774+
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')
778783
return result
779784

780785

0 commit comments

Comments
 (0)