Skip to content

Commit c41d339

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

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
@@ -686,12 +686,8 @@ def _downsample(self, how, **kwargs):
686686
if not len(ax):
687687
# reset to the new freq
688688
obj = obj.copy()
689-
if how == "size" and isinstance(obj, pd.DataFrame):
690-
obj = obj.groupby(
691-
self.grouper, axis=self.axis).aggregate(how, **kwargs)
692-
693689
obj.index.freq = self.freq
694-
return obj
690+
return self._wrap_result(obj)
695691

696692
# do we have a regular frequency
697693
if ax.freq is not None or ax.inferred_freq is not None:
@@ -760,6 +756,15 @@ def _wrap_result(self, result):
760756
# convert if needed
761757
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
762758
result.index = result.index.to_period(self.freq)
759+
760+
# Make consistent type of result. GH14962
761+
if not len(self.ax):
762+
grouper = BinGrouper([], result.index)
763+
grouped = self._selected_obj.groupby(grouper)
764+
result = pd.Series([],
765+
index=result.index,
766+
name=grouped.name,
767+
dtype='int64')
763768
return result
764769

765770

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()
@@ -731,7 +730,7 @@ def test_resample_empty_series(self):
731730
assert_series_equal(result, expected, check_dtype=False,
732731
check_names=False)
733732
# this assert will break when fixed
734-
self.assertTrue(result.name is None)
733+
# self.assertTrue(result.name is None)
735734
else:
736735
assert_series_equal(result, expected, check_dtype=False)
737736

@@ -742,15 +741,15 @@ def test_resample_empty_dataframe(self):
742741

743742
for freq in ['M', 'D', 'H']:
744743
# count retains dimensions too
745-
methods = downsample_methods + ['count']
744+
methods = downsample_methods + upsample_methods
746745
for method in methods:
747746
result = getattr(f.resample(freq), method)()
748747

749-
expected = f.copy()
748+
expected = pd.Series([])
750749
expected.index = f.index._shallow_copy(freq=freq)
751750
assert_index_equal(result.index, expected.index)
752751
self.assertEqual(result.index.freq, expected.index.freq)
753-
assert_frame_equal(result, expected, check_dtype=False)
752+
assert_series_equal(result, expected, check_dtype=False)
754753

755754
# test size for GH13212 (currently stays as df)
756755

@@ -809,11 +808,13 @@ def test_resample_loffset_arg_type(self):
809808

810809
def test_resample_empty_dataframe_with_size(self):
811810
# GH 14962
812-
df1 = pd.DataFrame(dict(a=range(100)),
813-
index=pd.date_range('1/1/2000', periods=100, freq="M"))
814-
df2 = df1[df1.a < 0]
815-
result = df2.resample("Q").size()
816-
assertIsInstance(result, pd.Series)
811+
index = pd.DatetimeIndex([], freq='M')
812+
df = pd.DataFrame([], index=index)
813+
814+
for freq in ['M', 'D', 'H']:
815+
result = df.resample(freq).size()
816+
expected = pd.Series([], index=index, dtype='int64')
817+
assert_series_equal(result, expected)
817818

818819

819820
class TestDatetimeIndex(Base, tm.TestCase):

0 commit comments

Comments
 (0)