-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
@@ -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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbrockmendel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
|
||
def _maybe_cast_slice_bound(self, label, side, kind): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -340,7 +340,7 @@ def test_memory_usage(self, indices): | |
# RangeIndex, IntervalIndex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
"result" and "result2" same as 80 |
||
# don't have engines | ||
if not isinstance(indices, (RangeIndex, IntervalIndex)): | ||
assert result2 > result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it just PeriodIndex for which this inequality is no longer strict? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbrockmendel |
||
assert result2 >= result | ||
|
||
if indices.inferred_type == "object": | ||
assert result3 > result2 | ||
|
There was a problem hiding this comment.
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