Skip to content

BUG: Fix return type of loc/iloc #61054

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
6 changes: 4 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,10 @@ def _getitem_lowerdim(self, tup: tuple):

tup = self._validate_key_length(tup)

for i, key in enumerate(tup):
if is_label_like(key):
# Reverse tuple so that we are indexing along columns before rows
# and avoid unintended dtype inference. # GH60600
for i, key in zip(range(len(tup) - 1, -1, -1), reversed(tup)):
if is_label_like(key) or is_list_like(key):
# We don't need to check for tuples here because those are
# caught by the _is_nested_tuple_indexer check above.
section = self._getitem_axis(key, axis=i)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ def test_missing_keys_raises_keyerror(self):
df = DataFrame(np.arange(12).reshape(4, 3), columns=["A", "B", "C"])
df2 = df.set_index(["A", "B"])

with pytest.raises(KeyError, match="1"):
with pytest.raises(KeyError, match="6"):
df2.loc[(1, 6)]

def test_missing_key_raises_keyerror2(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 @@ -60,6 +60,14 @@ def test_not_change_nan_loc(series, new_series, expected_ser):
tm.assert_frame_equal(df.notna(), ~expected)


def test_loc_dtype():
# GH 60600
df = DataFrame([["a", 1.0, 2.0], ["b", 3.0, 4.0]])
result = df.loc[0, [1, 2]]
expected = df[[1, 2]].loc[0]
Copy link
Member

Choose a reason for hiding this comment

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

Can you provide expected explicitly here: expected = DataFrame(...)

Copy link
Author

Choose a reason for hiding this comment

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

Made the code change. Thanks for the suggestion!

tm.assert_series_equal(result, expected)


class TestLoc:
def test_none_values_on_string_columns(self, using_infer_string):
# Issue #32218
Expand Down Expand Up @@ -441,7 +449,7 @@ def test_loc_to_fail(self):

msg = (
rf"\"None of \[Index\(\[1, 2\], dtype='{np.dtype(int)}'\)\] are "
r"in the \[index\]\""
r"in the \[columns\]\""
)
with pytest.raises(KeyError, match=msg):
df.loc[[1, 2], [1, 2]]
Expand Down Expand Up @@ -807,7 +815,7 @@ def test_loc_setitem_frame_mixed_labels(self):

result = df.loc[0, [1, 2]]
expected = Series(
[1, 3], index=Index([1, 2], dtype=object), dtype=object, name=0
[1, 3], index=Index([1, 2], dtype=object), dtype="int64", name=0
)
tm.assert_series_equal(result, expected)

Expand Down
Loading