Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Loading
from
Prev Previous commit
Next Next commit
Reverse tuple to avoid unintended dtype inference
  • Loading branch information
sanggon6107 committed Mar 13, 2025
commit d2e29c0b538d88fd22c31eb27dc15aa0383e7e2e
18 changes: 8 additions & 10 deletions 18 pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

from pandas.core.dtypes.cast import (
can_hold_element,
find_common_type,
maybe_promote,
)
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -1067,7 +1066,13 @@ def _getitem_lowerdim(self, tup: tuple):

tup = self._validate_key_length(tup)

for i, key in enumerate(tup):
# 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):
# We don't need to check for tuples here because those are
# caught by the _is_nested_tuple_indexer check above.
Expand Down Expand Up @@ -1095,14 +1100,7 @@ def _getitem_lowerdim(self, tup: tuple):
if com.is_null_slice(new_key):
return section
# This is an elided recursive call to iloc/loc
out = getattr(section, self.name)[new_key]
# Re-interpret dtype of out.values for loc/iloc[int, list-like].
# GH60600
if i == 0 and isinstance(key, int) and is_list_like(tup[1]):
dt = self.obj.dtypes.__getitem__(tup[1])
if len(dt) > 0:
out = out.astype(find_common_type(dt.tolist()))
return out
return getattr(section, self.name)[new_key]

raise IndexingError("not applicable")

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.