Skip to content

Commit

Permalink
BUG: resample and apply modify the index type for empty Series (#17149)
Browse files Browse the repository at this point in the history
  • Loading branch information
discort authored and jreback committed Aug 9, 2017
1 parent 64129d1 commit 3e9e947
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ Groupby/Resample/Rolling
- Bug in :func:`infer_freq` causing indices with 2-day gaps during the working week to be wrongly inferred as business daily (:issue:`16624`)
- Bug in ``.rolling(...).quantile()`` which incorrectly used different defaults than :func:`Series.quantile()` and :func:`DataFrame.quantile()` (:issue:`9413`, :issue:`16211`)
- Bug in ``groupby.transform()`` that would coerce boolean dtypes back to float (:issue:`16875`)
- Bug in ``Series.resample(...).apply()`` where an empty ``Series`` modified the source index and did not return the name of a ``Series`` (:issue:`14313`)

Sparse
^^^^^^
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pandas.core.indexes.period import PeriodIndex, period_range
import pandas.core.common as com
import pandas.core.algorithms as algos
from pandas.core.dtypes.generic import ABCDataFrame
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries

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

if isinstance(result, ABCSeries) and result.empty:
obj = self.obj
result.index = obj.index._shallow_copy(freq=to_offset(self.freq))
result.name = getattr(obj, 'name', None)

return result

def pad(self, limit=None):
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,16 @@ def test_resample_loffset_arg_type(self):
assert_frame_equal(result_agg, expected)
assert_frame_equal(result_how, expected)

def test_apply_to_empty_series(self):
# GH 14313
series = self.create_series()[:0]

for freq in ['M', 'D', 'H']:
result = series.resample(freq).apply(lambda x: 1)
expected = series.resample(freq).apply(np.sum)

assert_series_equal(result, expected, check_dtype=False)


class TestDatetimeIndex(Base):
_index_factory = lambda x: date_range
Expand Down Expand Up @@ -2794,6 +2804,14 @@ def test_evenly_divisible_with_no_extra_bins(self):
result = df.resample('7D').sum()
assert_frame_equal(result, expected)

def test_apply_to_empty_series(self):
# GH 14313
series = self.create_series()[:0]

for freq in ['M', 'D', 'H']:
with pytest.raises(TypeError):
series.resample(freq).apply(lambda x: 1)


class TestTimedeltaIndex(Base):
_index_factory = lambda x: timedelta_range
Expand Down

0 comments on commit 3e9e947

Please sign in to comment.