Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: resample and apply modify the index type for empty Series #17149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -299,6 +299,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