Skip to content

Commit 3088bcc

Browse files
committed
Improved tests & addressed comments
1 parent 12aba35 commit 3088bcc

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

pandas/core/resample.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pandas.core.indexes.period import PeriodIndex, period_range
1818
import pandas.core.common as com
1919
import pandas.core.algorithms as algos
20-
from pandas.core.dtypes.generic import ABCDataFrame
20+
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
2121

2222
import pandas.compat as compat
2323
from pandas.compat.numpy import function as nv
@@ -439,6 +439,11 @@ def _wrap_result(self, result):
439439
if isinstance(result, com.ABCSeries) and self._selection is not None:
440440
result.name = self._selection
441441

442+
if isinstance(result, ABCSeries) and result.empty:
443+
obj = self.obj
444+
result.index = obj.index._shallow_copy(freq=to_offset(self.freq))
445+
result.name = getattr(obj, 'name', None)
446+
442447
return result
443448

444449
def pad(self, limit=None):
@@ -778,11 +783,6 @@ def _wrap_result(self, result):
778783
# convert if needed
779784
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
780785
result.index = result.index.to_period(self.freq)
781-
782-
if isinstance(result, com.ABCSeries) and result.empty:
783-
obj = self.obj
784-
result.index = obj.index._shallow_copy(freq=to_offset(self.freq))
785-
result.name = getattr(obj, 'name', None)
786786
return result
787787

788788

pandas/tests/test_resample.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,16 @@ def test_resample_loffset_arg_type(self):
852852
assert_frame_equal(result_agg, expected)
853853
assert_frame_equal(result_how, expected)
854854

855+
def test_apply_to_empty_series(self):
856+
# GH 14313
857+
series = self.create_series()[:0]
858+
859+
for freq in ['M', 'D', 'H']:
860+
result = series.resample(freq).apply(lambda x: 1)
861+
expected = series.resample(freq).apply(np.sum)
862+
863+
assert_series_equal(result, expected, check_dtype=False)
864+
855865

856866
class TestDatetimeIndex(Base):
857867
_index_factory = lambda x: date_range
@@ -2192,17 +2202,6 @@ def test_resample_datetime_values(self):
21922202
res = df['timestamp'].resample('2D').first()
21932203
tm.assert_series_equal(res, exp)
21942204

2195-
def test_apply_to_empty_series(self):
2196-
# GH 14313
2197-
series = self.create_series()[:0]
2198-
2199-
for freq in ['M', 'D', 'H']:
2200-
result = series.resample(freq).apply(lambda x: 1)
2201-
expected = series.resample(freq).apply(np.sum)
2202-
2203-
assert result.name == expected.name
2204-
assert_series_equal(result, expected, check_dtype=False)
2205-
22062205

22072206
class TestPeriodIndex(Base):
22082207
_index_factory = lambda x: period_range
@@ -2805,6 +2804,14 @@ def test_evenly_divisible_with_no_extra_bins(self):
28052804
result = df.resample('7D').sum()
28062805
assert_frame_equal(result, expected)
28072806

2807+
def test_apply_to_empty_series(self):
2808+
# GH 14313
2809+
series = self.create_series()[:0]
2810+
2811+
for freq in ['M', 'D', 'H']:
2812+
with pytest.raises(TypeError):
2813+
series.resample(freq).apply(lambda x: 1)
2814+
28082815

28092816
class TestTimedeltaIndex(Base):
28102817
_index_factory = lambda x: timedelta_range

0 commit comments

Comments
 (0)