Skip to content

Bug in loc raised for numeric label even when label is in Index #37675

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

Merged
merged 3 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ Indexing
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`MultiIndex` with a level named "0" (:issue:`37194`)
- Bug in :meth:`Series.__getitem__` when using an unsigned integer array as an indexer giving incorrect results or segfaulting instead of raising ``KeyError`` (:issue:`37218`)
- Bug in :meth:`Index.where` incorrectly casting numeric values to strings (:issue:`37591`)
- Bug in :meth:`Series.loc` and :meth:`DataFrame.loc` raises when numeric label was given for object :class:`Index` although label was in :class:`Index` (:issue:`26491`)

Missing
^^^^^^^
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5200,13 +5200,14 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind):
# We are a plain index here (sub-class override this method if they
# wish to have special treatment for floats/ints, e.g. Float64Index and
# datetimelike Indexes
# reject them
if is_float(label):
# reject them, if index does not contain label
if is_float(label) and label not in self.values:
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like these can be the same case now
if (is_float(label) or is_integer(label)) and label not in self.values

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, thx

self._invalid_indexer("slice", label)

# we are trying to find integer bounds on a non-integer based index
# this is rejected (generally .loc gets you here)
elif is_integer(label):
# this is rejected (generally .loc gets you here) if label is not in
# index
elif is_integer(label) and label not in self.values:
self._invalid_indexer("slice", label)

return label
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,3 +1579,13 @@ def test_loc_setitem_dt64tz_values(self):
s2["a"] = expected
result = s2["a"]
assert result == expected


@pytest.mark.parametrize("klass", [DataFrame, Series])
Copy link
Contributor

Choose a reason for hiding this comment

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

@jbrockmendel

IIRC this test should be in pandas/tests/frame/indexing/test_loc.py per our new org.

Copy link
Member Author

Choose a reason for hiding this comment

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

Was not quite sure where to place them, because of tests for DataFrame and Series. Would be happy to move them, if this place is better.

Copy link
Member

Choose a reason for hiding this comment

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

This is the correct file, should probably be inside TestLabelSlicing class.

use frame_or_series fixture instead of klass here

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no class TestLabelSlicing or similar to this?

@pytest.mark.parametrize("value", [1, 1.5])
def test_loc_int_in_object_index(klass, value):
# GH: 26491
obj = klass(range(4), index=[value, "first", 2, "third"])
result = obj.loc[value:"third"]
expected = klass(range(4), index=[value, "first", 2, "third"])
tm.assert_equal(result, expected)