Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
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
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 = Series([1.0, 2.0], index=[1, 2], dtype=float, name=0)
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