Skip to content
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

BUG: Fix return type of loc/iloc #61054

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,8 +1066,14 @@ 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
if any(isinstance(ax, MultiIndex) for ax in self.obj.axes):
enum = enumerate(tup)
else:
enum = zip(range(len(tup) - 1, -1, -1), reversed(tup))
for i, key in enum:
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
10 changes: 9 additions & 1 deletion 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]
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 @@ -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