-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: loc raising KeyError for string slices in list-like indexer and DatetimeIndex #38153
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 |
---|---|---|
|
@@ -819,6 +819,25 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None): | |
|
||
# -------------------------------------------------------------------- | ||
|
||
def _convert_listlike_indexer(self, key): | ||
if not isinstance(key, list): | ||
# There are no slices, so we can dispatch back | ||
return super()._convert_listlike_indexer(key) | ||
|
||
new_indexer = [] | ||
positions = list(range(len(self))) | ||
try: | ||
for k in key: | ||
# Convert slices to list of integers | ||
indexer = positions[self.get_loc(k)] | ||
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 use .get_indexer() here? (and still then iterate over the results) 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. We discussed not supporting this. Has this changed? 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. yah i think we want to close this |
||
if not isinstance(indexer, list): | ||
indexer = [indexer] | ||
new_indexer.extend(indexer) | ||
except KeyError: | ||
# Dispatch to base method for handling of KeyErrors | ||
return super()._convert_listlike_indexer(key) | ||
return np.array(new_indexer), key | ||
|
||
@property | ||
def inferred_type(self) -> str: | ||
# b/c datetime is represented as microseconds since the epoch, make | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,3 +231,21 @@ def test_loc_setitem_with_existing_dst(self): | |
dtype=object, | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"indexer", [["2001-01", "2001-01-30"], ["2001-01", Timestamp("2001-01-30")]] | ||
) | ||
def test_loc_getitem_partial_strings_in_list(self, indexer): | ||
# GH#27180 | ||
ser = Series(1, index=date_range("2001-01-29", periods=60)) | ||
result = ser.loc[indexer] | ||
expected = Series( | ||
1, | ||
index=[ | ||
Timestamp("2001-01-29"), | ||
Timestamp("2001-01-30"), | ||
Timestamp("2001-01-31"), | ||
Timestamp("2001-01-30"), | ||
], | ||
) | ||
Comment on lines
+242
to
+250
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. nitpick, but this might look a bit better if you define 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. +1 |
||
tm.assert_series_equal(result, expected) |
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.
actually you can short-circust if we don't have any slices right?