Skip to content

Commit ccad304

Browse files
committed
BUG: PeriodEngine doesn't work (#29038)
1 parent f03ed62 commit ccad304

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

pandas/core/indexes/period.py

+19-25
Original file line numberDiff line numberDiff line change
@@ -704,36 +704,30 @@ def get_loc(self, key, method=None, tolerance=None):
704704
-------
705705
loc : int
706706
"""
707-
try:
708-
return self._engine.get_loc(key)
709-
except KeyError:
710-
if is_integer(key):
711-
raise
712-
713-
try:
714-
asdt, parsed, reso = parse_time_string(key, self.freq)
715-
key = asdt
716-
except TypeError:
717-
pass
718-
except DateParseError:
719-
# A string with invalid format
720-
raise KeyError("Cannot interpret '{}' as period".format(key))
707+
if is_integer(key) or (is_float(key) and not isna(key)):
708+
ordinal = key
709+
else:
710+
if isinstance(key, str):
711+
try:
712+
asdt, parsed, reso = parse_time_string(key, self.freq)
713+
key = asdt
714+
except DateParseError:
715+
# A string with invalid format
716+
raise KeyError("Cannot interpret '{}' as period".format(key))
721717

722718
try:
723719
key = Period(key, freq=self.freq)
724-
except ValueError:
725-
# we cannot construct the Period
726-
# as we have an invalid type
727-
raise KeyError(key)
728-
729-
try:
730720
ordinal = iNaT if key is NaT else key.ordinal
731-
if tolerance is not None:
732-
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
733-
return self._int64index.get_loc(ordinal, method, tolerance)
721+
except ValueError:
722+
# we cannot construct the Period as we have an invalid type
723+
raise TypeError("'{val}' is an invalid key".format(val=key))
734724

735-
except KeyError:
736-
raise KeyError(key)
725+
try:
726+
if tolerance is not None:
727+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
728+
return self._int64index.get_loc(ordinal, method, tolerance)
729+
except KeyError:
730+
raise KeyError(key)
737731

738732
def _maybe_cast_slice_bound(self, label, side, kind):
739733
"""

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_memory_usage(self, indices):
331331

332332
# RangeIndex, IntervalIndex
333333
# don't have engines
334-
if not isinstance(indices, (RangeIndex, IntervalIndex)):
334+
if not isinstance(indices, (RangeIndex, IntervalIndex, PeriodIndex)):
335335
assert result2 > result
336336

337337
if indices.inferred_type == "object":

pandas/tests/indexes/period/test_indexing.py

+1
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,4 @@ def test_period_index_indexer(self):
685685
tm.assert_frame_equal(df, df.loc[list(idx)])
686686
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]])
687687
tm.assert_frame_equal(df, df.loc[list(idx)])
688+

0 commit comments

Comments
 (0)