Skip to content

Commit b9afb3e

Browse files
TomAugspurgerPingviinituutti
authored andcommitted
Fixed PeriodIndex._shallow_copy for i8 (pandas-dev#24604)
1 parent 04bb10f commit b9afb3e

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

pandas/core/indexes/period.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,9 @@ def _shallow_copy(self, values=None, **kwargs):
322322
# this quite a bit.
323323
values = period_array(values, freq=self.freq)
324324

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

333329
attributes.update(kwargs)
334330
if not len(values) and 'dtype' not in kwargs:

pandas/core/resample.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,10 @@ def _wrap_result(self, result):
404404

405405
if isinstance(result, ABCSeries) and result.empty:
406406
obj = self.obj
407-
result.index = obj.index._shallow_copy(freq=to_offset(self.freq))
407+
if isinstance(obj.index, PeriodIndex):
408+
result.index = obj.index.asfreq(self.freq)
409+
else:
410+
result.index = obj.index._shallow_copy(freq=self.freq)
408411
result.name = getattr(obj, 'name', None)
409412

410413
return result

pandas/tests/indexes/period/test_period.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from pandas._libs.tslibs.period import IncompatibleFrequency
45
import pandas.util._test_decorators as td
56

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

118117
tm.assert_index_equal(result, expected)
119118

119+
def test_shallow_copy_i8(self):
120+
# GH-24391
121+
pi = period_range("2018-01-01", periods=3, freq="2D")
122+
result = pi._shallow_copy(pi.asi8, freq=pi.freq)
123+
tm.assert_index_equal(result, pi)
124+
125+
def test_shallow_copy_changing_freq_raises(self):
126+
pi = period_range("2018-01-01", periods=3, freq="2D")
127+
with pytest.raises(IncompatibleFrequency, match="are different"):
128+
pi._shallow_copy(pi, freq="H")
129+
120130
def test_dtype_str(self):
121131
pi = pd.PeriodIndex([], freq='M')
122132
assert pi.dtype_str == 'period[M]'

pandas/tests/resample/test_base.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ def test_resample_empty_series_all_ts(freq, empty_series, resample_method):
109109
result = getattr(s.resample(freq), resample_method)()
110110

111111
expected = s.copy()
112-
expected.index = s.index._shallow_copy(freq=freq)
112+
if isinstance(s.index, PeriodIndex):
113+
expected.index = s.index.asfreq(freq=freq)
114+
else:
115+
expected.index = s.index._shallow_copy(freq=freq)
113116
assert_index_equal(result.index, expected.index)
114117
assert result.index.freq == expected.index.freq
115118
assert_series_equal(result, expected, check_dtype=False)
@@ -127,7 +130,10 @@ def test_resample_empty_dataframe_all_ts(empty_frame, freq, resample_method):
127130
# GH14962
128131
expected = Series([])
129132

130-
expected.index = df.index._shallow_copy(freq=freq)
133+
if isinstance(df.index, PeriodIndex):
134+
expected.index = df.index.asfreq(freq=freq)
135+
else:
136+
expected.index = df.index._shallow_copy(freq=freq)
131137
assert_index_equal(result.index, expected.index)
132138
assert result.index.freq == expected.index.freq
133139
assert_almost_equal(result, expected, check_dtype=False)

0 commit comments

Comments
 (0)