Skip to content

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

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Improved tests & addressed comments
  • Loading branch information
discort committed Aug 2, 2017
commit 3088bcc6038db16f50860ec9735d1cc101bdcadb
12 changes: 6 additions & 6 deletions 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 Expand Up @@ -778,11 +783,6 @@ def _wrap_result(self, result):
# convert if needed
if self.kind == 'period' and not isinstance(result.index, PeriodIndex):
result.index = result.index.to_period(self.freq)

if isinstance(result, com.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


Expand Down
29 changes: 18 additions & 11 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 @@ -2192,17 +2202,6 @@ def test_resample_datetime_values(self):
res = df['timestamp'].resample('2D').first()
tm.assert_series_equal(res, exp)

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 result.name == expected.name
assert_series_equal(result, expected, check_dtype=False)


class TestPeriodIndex(Base):
_index_factory = lambda x: period_range
Expand Down Expand Up @@ -2805,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