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
11 changes: 10 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,16 @@ def _getitem_lowerdim(self, tup: tuple):
if com.is_null_slice(new_key):
return section
# This is an elided recursive call to iloc/loc
return getattr(section, self.name)[new_key]
out = getattr(section, self.name)[new_key]
# Re-interpret dtype of out.values for loc/iloc[int, list/slice].
# GH60600
if (
i == 0
and isinstance(key, int)
and isinstance(new_key, (list, slice))
):
out = out.infer_objects()
Copy link
Member

Choose a reason for hiding this comment

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

This will infer non-object dtype even on data that is object-dtype, no? I do not think this is the right change.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @rhshadrach !
I've also confirmed what you've mentioned.

>>>import pandas as pd
>>> df = pd.DataFrame({1: [11, 22], 2: [33, 44], "a": [55, 66]}, dtype=object)

>>>df.loc[0, [1, 2]]
1	11
2	33
Name: 0, dtype: int64

>>>df[[1, 2]].loc[0]
1	11
2	33
Name: 0, dtype: object

I'll do some further investigation to fix this.

return out

raise IndexingError("not applicable")

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]
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 @@ -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