Skip to content

PERF: PeriodEngine.get_loc accept narrow data type #29234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Deprecations
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~

-
- Performance improvement in :meth:`PeriodIndex.get_loc` using input from user (:issue:`29038`)
-

.. ---------------------------------------------------------------------------
Expand Down
20 changes: 7 additions & 13 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,9 @@ def get_loc(self, key, method=None, tolerance=None):
TypeError
If key is listlike or otherwise not hashable.
"""

if isinstance(key, str):
try:
asdt, parsed, reso = parse_time_string(key, self.freq)
key = asdt
key, parsed, reso = parse_time_string(key, self.freq)
except DateParseError:
# A string with invalid format
raise KeyError(f"Cannot interpret '{key}' as period")
Expand All @@ -613,25 +611,21 @@ def get_loc(self, key, method=None, tolerance=None):

try:
key = Period(key, freq=self.freq)
ordinal = key.ordinal if key is not NaT else key.value
except ValueError:
# we cannot construct the Period
# as we have an invalid type
if is_list_like(key):
raise TypeError(f"'{key}' is an invalid key")
raise KeyError(key)

ordinal = key.ordinal if key is not NaT else key.value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this got affected by #31021. unless there a compelling reason to move this line up to L614, lets keep it here

if tolerance is not None:
tolerance = self._convert_tolerance(tolerance, np.asarray(key))

try:
return self._engine.get_loc(ordinal)
return self._int64index.get_loc(ordinal, method, tolerance)
except KeyError:

try:
if tolerance is not None:
tolerance = self._convert_tolerance(tolerance, np.asarray(key))
return self._int64index.get_loc(ordinal, method, tolerance)

except KeyError:
raise KeyError(key)
raise KeyError(key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going directly to _int64index does simplify the code here which is nice, but i imagine involves a perf penalty for by-far-the-most-common case with method=tolerance=None. Am I wrong about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel
Yes, you are right. I pointed out the wrong key. Like #31021, cleaning up codes is best choice.
Is it okay if i close PR and issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure


def _maybe_cast_slice_bound(self, label, side, kind):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def test_memory_usage(self, indices):
# RangeIndex, IntervalIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you update / change the comment to more reflect what is going on (i would remove the references to RI and II here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indices = PeriodIndex(['2000-01-03', '2000-01-04', '2000-01-05', '2000-01-06',
             '2000-01-07', '2000-01-10', '2000-01-11', '2000-01-12',
             '2000-01-13', '2000-01-14'],
            dtype='period[D]', freq='D')
result = indices.memory_usage()
indices.get_loc(indices[0])
result2 = indices.memory_usage()

"result" and "result2" same as 80

# don't have engines
if not isinstance(indices, (RangeIndex, IntervalIndex)):
assert result2 > result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it just PeriodIndex for which this inequality is no longer strict?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel
Yes. for PeriodIndex

assert result2 >= result

if indices.inferred_type == "object":
assert result3 > result2
Expand Down