Skip to content

Fixed PeriodIndex._shallow_copy for i8 #24604

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 4 commits into from
Jan 4, 2019
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
8 changes: 2 additions & 6 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,9 @@ def _shallow_copy(self, values=None, **kwargs):
# this quite a bit.
values = period_array(values, freq=self.freq)

# I don't like overloading shallow_copy with freq changes.
# See if it's used anywhere outside of test_resample_empty_dataframe
# We don't allow changing `freq` in _shallow_copy.
validate_dtype_freq(self.dtype, kwargs.get('freq'))
attributes = self._get_attributes_dict()
freq = kwargs.pop("freq", None)
if freq:
values = values.asfreq(freq)
attributes.pop("freq", None)

attributes.update(kwargs)
if not len(values) and 'dtype' not in kwargs:
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ def _wrap_result(self, result):

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

return result
Expand Down
16 changes: 13 additions & 3 deletions pandas/tests/indexes/period/test_period.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

from pandas._libs.tslibs.period import IncompatibleFrequency
import pandas.util._test_decorators as td

import pandas as pd
Expand Down Expand Up @@ -40,9 +41,7 @@ def test_where(self):
@pytest.mark.parametrize('use_numpy', [True, False])
@pytest.mark.parametrize('index', [
pd.period_range('2000-01-01', periods=3, freq='D'),
pytest.param(
pd.period_range('2001-01-01', periods=3, freq='2D'),
marks=pytest.mark.xfail(reason='GH 24391')),
pd.period_range('2001-01-01', periods=3, freq='2D'),
pd.PeriodIndex(['2001-01', 'NaT', '2003-01'], freq='M')])
def test_repeat_freqstr(self, index, use_numpy):
# GH10183
Expand Down Expand Up @@ -117,6 +116,17 @@ def test_shallow_copy_empty(self):

tm.assert_index_equal(result, expected)

def test_shallow_copy_i8(self):
# GH-24391
pi = period_range("2018-01-01", periods=3, freq="2D")
result = pi._shallow_copy(pi.asi8, freq=pi.freq)
tm.assert_index_equal(result, pi)

def test_shallow_copy_changing_freq_raises(self):
pi = period_range("2018-01-01", periods=3, freq="2D")
with pytest.raises(IncompatibleFrequency, match="are different"):
pi._shallow_copy(pi, freq="H")

def test_dtype_str(self):
pi = pd.PeriodIndex([], freq='M')
assert pi.dtype_str == 'period[M]'
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ def test_resample_empty_series_all_ts(freq, empty_series, resample_method):
result = getattr(s.resample(freq), resample_method)()

expected = s.copy()
expected.index = s.index._shallow_copy(freq=freq)
if isinstance(s.index, PeriodIndex):
expected.index = s.index.asfreq(freq=freq)
else:
expected.index = s.index._shallow_copy(freq=freq)
assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
assert_series_equal(result, expected, check_dtype=False)
Expand All @@ -127,7 +130,10 @@ def test_resample_empty_dataframe_all_ts(empty_frame, freq, resample_method):
# GH14962
expected = Series([])

expected.index = df.index._shallow_copy(freq=freq)
if isinstance(df.index, PeriodIndex):
expected.index = df.index.asfreq(freq=freq)
else:
expected.index = df.index._shallow_copy(freq=freq)
assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
assert_almost_equal(result, expected, check_dtype=False)
Expand Down