Skip to content

Commit 3e9e947

Browse files
discortjreback
authored andcommitted
BUG: resample and apply modify the index type for empty Series (#17149)
1 parent 64129d1 commit 3e9e947

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ Groupby/Resample/Rolling
348348
- Bug in :func:`infer_freq` causing indices with 2-day gaps during the working week to be wrongly inferred as business daily (:issue:`16624`)
349349
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
350350
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
351+
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)
351352

352353
Sparse
353354
^^^^^^

pandas/core/resample.py

+6-1
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):

pandas/tests/test_resample.py

+18
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
@@ -2794,6 +2804,14 @@ def test_evenly_divisible_with_no_extra_bins(self):
27942804
result = df.resample('7D').sum()
27952805
assert_frame_equal(result, expected)
27962806

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+
27972815

27982816
class TestTimedeltaIndex(Base):
27992817
_index_factory = lambda x: timedelta_range

0 commit comments

Comments
 (0)