Skip to content

Commit 6dc0961

Browse files
committed
BUG: added series type to wrap_result for empty DataFrame
1 parent 2fc473c commit 6dc0961

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

pandas/tseries/tests/test_resample.py

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

3332
bday = BDay()
@@ -749,7 +748,7 @@ def test_resample_empty_series(self):
749748
assert_series_equal(result, expected, check_dtype=False,
750749
check_names=False)
751750
# this assert will break when fixed
752-
self.assertTrue(result.name is None)
751+
# self.assertTrue(result.name is None)
753752
else:
754753
assert_series_equal(result, expected, check_dtype=False)
755754

@@ -760,15 +759,15 @@ def test_resample_empty_dataframe(self):
760759

761760
for freq in ['M', 'D', 'H']:
762761
# count retains dimensions too
763-
methods = downsample_methods + ['count']
762+
methods = downsample_methods + upsample_methods
764763
for method in methods:
765764
result = getattr(f.resample(freq), method)()
766765

767-
expected = f.copy()
766+
expected = pd.Series([])
768767
expected.index = f.index._shallow_copy(freq=freq)
769768
assert_index_equal(result.index, expected.index)
770769
self.assertEqual(result.index.freq, expected.index.freq)
771-
assert_frame_equal(result, expected, check_dtype=False)
770+
assert_series_equal(result, expected, check_dtype=False)
772771

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

@@ -827,11 +826,13 @@ def test_resample_loffset_arg_type(self):
827826

828827
def test_resample_empty_dataframe_with_size(self):
829828
# GH 14962
830-
df1 = pd.DataFrame(dict(a=range(100)),
831-
index=pd.date_range('1/1/2000', periods=100, freq="M"))
832-
df2 = df1[df1.a < 0]
833-
result = df2.resample("Q").size()
834-
assertIsInstance(result, pd.Series)
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)
835836

836837

837838
class TestDatetimeIndex(Base, tm.TestCase):

0 commit comments

Comments
 (0)