Skip to content

Commit 1db092b

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

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

pandas/core/indexes/period.py

+18-24
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+
key, parsed, reso = parse_time_string(key, self.freq)
713+
except DateParseError:
714+
# A string with invalid format
715+
raise KeyError(f"Cannot interpret '{key}' as period")
721716

722717
try:
723718
key = Period(key, freq=self.freq)
719+
ordinal = iNaT if key is NaT else key.ordinal
724720
except ValueError:
725-
# we cannot construct the Period
726-
# as we have an invalid type
727-
raise KeyError(key)
721+
# we cannot construct the Period as we have an invalid type
722+
raise TypeError(f"'{key}' is an invalid key")
728723

729-
try:
730-
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)
724+
if tolerance is not None:
725+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
734726

735-
except KeyError:
736-
raise KeyError(key)
727+
try:
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":

0 commit comments

Comments
 (0)