Skip to content

Commit 4b47e01

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

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
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

+21-23
Original file line numberDiff line numberDiff line change
@@ -642,36 +642,34 @@ 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-
645+
if isinstance(key, str):
651646
try:
652-
asdt, parsed, reso = parse_time_string(key, self.freq)
653-
key = asdt
654-
except TypeError:
655-
pass
647+
key, parsed, reso = parse_time_string(key, self.freq)
656648
except DateParseError:
657649
# A string with invalid format
658650
raise KeyError(f"Cannot interpret '{key}' as period")
659651

660-
try:
661-
key = Period(key, freq=self.freq)
662-
except ValueError:
663-
# we cannot construct the Period
664-
# as we have an invalid type
665-
raise KeyError(key)
652+
elif is_integer(key):
653+
# Period constructor will cast to string, which we dont want
654+
raise KeyError(key)
666655

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)
656+
try:
657+
key = Period(key, freq=self.freq)
658+
ordinal = key.ordinal if key is not NaT else key.value
659+
except ValueError:
660+
# we cannot construct the Period
661+
# as we have an invalid type
662+
if is_list_like(key):
663+
raise TypeError(f"'{key}' is an invalid key")
664+
raise KeyError(key)
665+
666+
if tolerance is not None:
667+
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
672668

673-
except KeyError:
674-
raise KeyError(key)
669+
try:
670+
return self._int64index.get_loc(ordinal, method, tolerance)
671+
except KeyError:
672+
raise KeyError(key)
675673

676674
def _maybe_cast_slice_bound(self, label, side, kind):
677675
"""

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)