Skip to content

BUG: Period resample with length=0 doesn't set freq #13067

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ Performance Improvements

Bug Fixes
~~~~~~~~~

- Bug in ``.resample`` empty data with ``PeriodIndex`` doesn't change ``freq`` (:issue:`13067`)
5 changes: 4 additions & 1 deletion pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,10 @@ def _downsample(self, how, **kwargs):

new_index = self._get_new_index()
if len(new_index) == 0:
return self._wrap_result(self._selected_obj.reindex(new_index))
result = self._selected_obj
if isinstance(self._selected_obj.index, PeriodIndex):
result = result.asfreq(self.freq, how=self.convention)
return self._wrap_result(result.reindex(new_index))

# Start vs. end of period
memb = ax.asfreq(self.freq, how=self.convention)
Expand Down
13 changes: 7 additions & 6 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2043,17 +2043,18 @@ def test_resample_basic(self):
assert_series_equal(result2, expected)

def test_resample_empty(self):

# GH12771 & GH12868
index = PeriodIndex(start='2000', periods=0, freq='D', name='idx')
s = Series(index=index)

expected_index = PeriodIndex([], name='idx', freq='M')
expected = Series(index=expected_index)
for freq in ['M', 'D', 'H']:
expected_index = PeriodIndex([], name='idx', freq=freq)
expected = Series(index=expected_index)

for method in resample_methods:
result = getattr(s.resample('M'), method)()
assert_series_equal(result, expected)
for method in resample_methods:
result = getattr(s.resample(freq), method)()
assert_series_equal(result, expected)
self.assertEqual(result.index.freq, freq)

def test_resample_count(self):

Expand Down