Skip to content

Commit b10e5ce

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

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ Performance improvements
688688
The improvement is not present if checking if the :class:`Categorical` is less than or less than or equal than the scalar (:issue:`29820`)
689689
- Performance improvement in :meth:`Index.equals` and :meth:`MultiIndex.equals` (:issue:`29134`)
690690
- Performance improvement in :func:`~pandas.api.types.infer_dtype` when ``skipna`` is ``True`` (:issue:`28814`)
691+
- Performance improvement in :meth:`PeriodIndex.get_loc` using input from user (:issue:`29038`)
691692

692693
.. _whatsnew_1000.bug_fixes:
693694

pandas/core/indexes/period.py

+18-24
Original file line numberDiff line numberDiff line change
@@ -642,36 +642,30 @@ def get_loc(self, key, method=None, tolerance=None):
642642
-------
643643
loc : int
644644
"""
645-
try:
646-
return self._engine.get_loc(key)
647-
except KeyError:
648-
if is_integer(key):
649-
raise
650-
651-
try:
652-
asdt, parsed, reso = parse_time_string(key, self.freq)
653-
key = asdt
654-
except TypeError:
655-
pass
656-
except DateParseError:
657-
# A string with invalid format
658-
raise KeyError(f"Cannot interpret '{key}' as period")
645+
if is_integer(key) or (is_float(key) and not isna(key)):
646+
ordinal = key
647+
else:
648+
if isinstance(key, str):
649+
try:
650+
key, parsed, reso = parse_time_string(key, self.freq)
651+
except DateParseError:
652+
# A string with invalid format
653+
raise KeyError(f"Cannot interpret '{key}' as period")
659654

660655
try:
661656
key = Period(key, freq=self.freq)
657+
ordinal = iNaT if key is NaT else key.ordinal
662658
except ValueError:
663-
# we cannot construct the Period
664-
# as we have an invalid type
665-
raise KeyError(key)
659+
# we cannot construct the Period as we have an invalid type
660+
raise TypeError(f"'{key}' is an invalid key")
666661

667-
try:
668-
ordinal = iNaT if key is NaT else key.ordinal
669-
if tolerance is not None:
670-
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
671-
return self._int64index.get_loc(ordinal, method, tolerance)
662+
if tolerance is not None:
663+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
672664

673-
except KeyError:
674-
raise KeyError(key)
665+
try:
666+
return self._int64index.get_loc(ordinal, method, tolerance)
667+
except KeyError:
668+
raise KeyError(key)
675669

676670
def _maybe_cast_slice_bound(self, label, side, kind):
677671
"""

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def test_memory_usage(self, indices):
340340
# RangeIndex, IntervalIndex
341341
# don't have engines
342342
if not isinstance(indices, (RangeIndex, IntervalIndex)):
343-
assert result2 > result
343+
assert result2 >= result
344344

345345
if indices.inferred_type == "object":
346346
assert result3 > result2

0 commit comments

Comments
 (0)