Skip to content

BUG: using loc[int] with object index #31905

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 4 commits into from
Feb 22, 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
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3138,7 +3138,7 @@ def _convert_scalar_indexer(self, key, kind: str_t):
self._invalid_indexer("label", key)

elif kind == "loc" and is_integer(key):
if not self.holds_integer():
if not (is_integer_dtype(self.dtype) or is_object_dtype(self.dtype)):
self._invalid_indexer("label", key)

return key
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,11 @@ def _get_value(self, label, takeable: bool = False):
if takeable:
return self._values[label]

# Similar to Index.get_value, but we do not fall back to positional
loc = self.index.get_loc(label)
# We assume that _convert_scalar_indexer has already been called,
# with kind="loc", if necessary, by the time we get here
return self.index.get_value(self, label)
return self.index._get_values_for_loc(self, loc, label)

def __setitem__(self, key, value):
key = com.apply_if_callable(key, self)
Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ def test_loc_scalar(self):
with pytest.raises(TypeError, match=msg):
df.loc["d", "C"] = 10

msg = (
"cannot do label indexing on CategoricalIndex with these "
r"indexers \[1\] of type int"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(KeyError, match="^1$"):
df.loc[1]

def test_getitem_scalar(self):
Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestLoc(Base):
def test_loc_getitem_int(self):

# int label
self.check_result("loc", 2, typs=["labels"], fails=TypeError)
self.check_result("loc", 2, typs=["labels"], fails=KeyError)

def test_loc_getitem_label(self):

Expand All @@ -34,7 +34,7 @@ def test_loc_getitem_label_out_of_range(self):
self.check_result(
"loc", 20, typs=["ints", "uints", "mixed"], fails=KeyError,
)
self.check_result("loc", 20, typs=["labels"], fails=TypeError)
self.check_result("loc", 20, typs=["labels"], fails=KeyError)
self.check_result("loc", 20, typs=["ts"], axes=0, fails=TypeError)
self.check_result("loc", 20, typs=["floats"], axes=0, fails=KeyError)

Expand Down Expand Up @@ -967,3 +967,11 @@ def test_loc_set_dataframe_multiindex():
result = expected.copy()
result.loc[0, [(0, 1)]] = result.loc[0, [(0, 1)]]
tm.assert_frame_equal(result, expected)


def test_loc_mixed_int_float():
# GH#19456
ser = pd.Series(range(2), pd.Index([1, 2.0], dtype=object))

result = ser.loc[1]
assert result == 0
18 changes: 5 additions & 13 deletions pandas/tests/indexing/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,22 @@ def test_series_at_raises_type_error(self):
result = ser.loc["a"]
assert result == 1

msg = (
"cannot do label indexing on Index "
r"with these indexers \[0\] of type int"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(KeyError, match="^0$"):
ser.at[0]
with pytest.raises(TypeError, match=msg):
with pytest.raises(KeyError, match="^0$"):
ser.loc[0]

def test_frame_raises_type_error(self):
def test_frame_raises_key_error(self):
# GH#31724 .at should match .loc
df = DataFrame({"A": [1, 2, 3]}, index=list("abc"))
result = df.at["a", "A"]
assert result == 1
result = df.loc["a", "A"]
assert result == 1

msg = (
"cannot do label indexing on Index "
r"with these indexers \[0\] of type int"
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(KeyError, match="^0$"):
df.at["a", 0]
with pytest.raises(TypeError, match=msg):
with pytest.raises(KeyError, match="^0$"):
df.loc["a", 0]

def test_series_at_raises_key_error(self):
Expand Down