Skip to content

Commit 6cc7135

Browse files
sinhrksjreback
authored andcommitted
CLN: move PeriodIndex.__getitem__ to DatetimeIndexOpsMixin
related to #6469 Author: sinhrks <[email protected]> Closes #13987 from sinhrks/period_getitem and squashes the following commits: 48d187d [sinhrks] CLN: move PeriodIndex.__getitem__ to DatetimeOpsMixin
1 parent 6d8044c commit 6cc7135

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

pandas/tseries/base.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,25 @@ def __getitem__(self, key):
236236

237237
attribs = self._get_attributes_dict()
238238

239-
freq = None
240-
if isinstance(key, slice):
241-
if self.freq is not None and key.step is not None:
242-
freq = key.step * self.freq
243-
else:
244-
freq = self.freq
239+
is_period = isinstance(self, ABCPeriodIndex)
240+
if is_period:
241+
freq = self.freq
242+
else:
243+
freq = None
244+
if isinstance(key, slice):
245+
if self.freq is not None and key.step is not None:
246+
freq = key.step * self.freq
247+
else:
248+
freq = self.freq
249+
245250
attribs['freq'] = freq
246251

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

251260
return self._simple_new(result, **attribs)

pandas/tseries/period.py

-19
Original file line numberDiff line numberDiff line change
@@ -886,25 +886,6 @@ def _apply_meta(self, rawarr):
886886
rawarr = PeriodIndex(rawarr, freq=self.freq)
887887
return rawarr
888888

889-
def __getitem__(self, key):
890-
getitem = self._data.__getitem__
891-
if is_scalar(key):
892-
val = getitem(key)
893-
return Period(ordinal=val, freq=self.freq)
894-
else:
895-
if com.is_bool_indexer(key):
896-
key = np.asarray(key)
897-
898-
result = getitem(key)
899-
if result.ndim > 1:
900-
# MPL kludge
901-
# values = np.asarray(list(values), dtype=object)
902-
# return values.reshape(result.shape)
903-
904-
return PeriodIndex(result, name=self.name, freq=self.freq)
905-
906-
return PeriodIndex(result, name=self.name, freq=self.freq)
907-
908889
def _format_native_types(self, na_rep=u('NaT'), date_format=None,
909890
**kwargs):
910891

pandas/tseries/tests/test_period.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -2098,8 +2098,23 @@ def test_getitem_ndim2(self):
20982098
idx = period_range('2007-01', periods=3, freq='M')
20992099

21002100
result = idx[:, None]
2101-
# MPL kludge
2101+
# MPL kludge, internally has incorrect shape
21022102
tm.assertIsInstance(result, PeriodIndex)
2103+
self.assertEqual(result.shape, (len(idx), 1))
2104+
2105+
def test_getitem_index(self):
2106+
idx = period_range('2007-01', periods=10, freq='M', name='x')
2107+
2108+
result = idx[[1, 3, 5]]
2109+
exp = pd.PeriodIndex(['2007-02', '2007-04', '2007-06'],
2110+
freq='M', name='x')
2111+
tm.assert_index_equal(result, exp)
2112+
2113+
result = idx[[True, True, False, False, False,
2114+
True, True, False, False, False]]
2115+
exp = pd.PeriodIndex(['2007-01', '2007-02', '2007-06', '2007-07'],
2116+
freq='M', name='x')
2117+
tm.assert_index_equal(result, exp)
21032118

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

0 commit comments

Comments
 (0)