Skip to content

Commit 48d187d

Browse files
committed
CLN: move PeriodIndex.__getitem__ to DatetimeOpsMixin
1 parent a0d05db commit 48d187d

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
@@ -862,25 +862,6 @@ def _apply_meta(self, rawarr):
862862
rawarr = PeriodIndex(rawarr, freq=self.freq)
863863
return rawarr
864864

865-
def __getitem__(self, key):
866-
getitem = self._data.__getitem__
867-
if is_scalar(key):
868-
val = getitem(key)
869-
return Period(ordinal=val, freq=self.freq)
870-
else:
871-
if com.is_bool_indexer(key):
872-
key = np.asarray(key)
873-
874-
result = getitem(key)
875-
if result.ndim > 1:
876-
# MPL kludge
877-
# values = np.asarray(list(values), dtype=object)
878-
# return values.reshape(result.shape)
879-
880-
return PeriodIndex(result, name=self.name, freq=self.freq)
881-
882-
return PeriodIndex(result, name=self.name, freq=self.freq)
883-
884865
def _format_native_types(self, na_rep=u('NaT'), date_format=None,
885866
**kwargs):
886867

pandas/tseries/tests/test_period.py

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

20622062
result = idx[:, None]
2063-
# MPL kludge
2063+
# MPL kludge, internally has incorrect shape
20642064
tm.assertIsInstance(result, PeriodIndex)
2065+
self.assertEqual(result.shape, (len(idx), 1))
2066+
2067+
def test_getitem_index(self):
2068+
idx = period_range('2007-01', periods=10, freq='M', name='x')
2069+
2070+
result = idx[[1, 3, 5]]
2071+
exp = pd.PeriodIndex(['2007-02', '2007-04', '2007-06'],
2072+
freq='M', name='x')
2073+
tm.assert_index_equal(result, exp)
2074+
2075+
result = idx[[True, True, False, False, False,
2076+
True, True, False, False, False]]
2077+
exp = pd.PeriodIndex(['2007-01', '2007-02', '2007-06', '2007-07'],
2078+
freq='M', name='x')
2079+
tm.assert_index_equal(result, exp)
20652080

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

0 commit comments

Comments
 (0)