Skip to content

CLN: move PeriodIndex.__getitem__ to DatetimeIndexOpsMixin #13987

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
21 changes: 15 additions & 6 deletions pandas/tseries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,25 @@ def __getitem__(self, key):

attribs = self._get_attributes_dict()

freq = None
if isinstance(key, slice):
if self.freq is not None and key.step is not None:
freq = key.step * self.freq
else:
freq = self.freq
is_period = isinstance(self, ABCPeriodIndex)
if is_period:
freq = self.freq
else:
freq = None
if isinstance(key, slice):
if self.freq is not None and key.step is not None:
freq = key.step * self.freq
else:
freq = self.freq

attribs['freq'] = freq

result = getitem(key)
if result.ndim > 1:
# To support MPL which performs slicing with 2 dim
# even though it only has 1 dim by definition
if is_period:
return self._simple_new(result, **attribs)
return result

return self._simple_new(result, **attribs)
Expand Down
19 changes: 0 additions & 19 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,25 +862,6 @@ def _apply_meta(self, rawarr):
rawarr = PeriodIndex(rawarr, freq=self.freq)
return rawarr

def __getitem__(self, key):
getitem = self._data.__getitem__
if is_scalar(key):
val = getitem(key)
return Period(ordinal=val, freq=self.freq)
else:
if com.is_bool_indexer(key):
key = np.asarray(key)

result = getitem(key)
if result.ndim > 1:
# MPL kludge
# values = np.asarray(list(values), dtype=object)
# return values.reshape(result.shape)

return PeriodIndex(result, name=self.name, freq=self.freq)

return PeriodIndex(result, name=self.name, freq=self.freq)

def _format_native_types(self, na_rep=u('NaT'), date_format=None,
**kwargs):

Expand Down
17 changes: 16 additions & 1 deletion pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -2060,8 +2060,23 @@ def test_getitem_ndim2(self):
idx = period_range('2007-01', periods=3, freq='M')

result = idx[:, None]
# MPL kludge
# MPL kludge, internally has incorrect shape
tm.assertIsInstance(result, PeriodIndex)
self.assertEqual(result.shape, (len(idx), 1))

def test_getitem_index(self):
idx = period_range('2007-01', periods=10, freq='M', name='x')

result = idx[[1, 3, 5]]
exp = pd.PeriodIndex(['2007-02', '2007-04', '2007-06'],
freq='M', name='x')
tm.assert_index_equal(result, exp)

result = idx[[True, True, False, False, False,
True, True, False, False, False]]
exp = pd.PeriodIndex(['2007-01', '2007-02', '2007-06', '2007-07'],
freq='M', name='x')
tm.assert_index_equal(result, exp)

def test_getitem_partial(self):
rng = period_range('2007-01', periods=50, freq='M')
Expand Down