Skip to content

Commit d2e29c0

Browse files
committed
Reverse tuple to avoid unintended dtype inference
1 parent ec9fd4f commit d2e29c0

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

pandas/core/indexing.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
from pandas.core.dtypes.cast import (
3030
can_hold_element,
31-
find_common_type,
3231
maybe_promote,
3332
)
3433
from pandas.core.dtypes.common import (
@@ -1067,7 +1066,13 @@ def _getitem_lowerdim(self, tup: tuple):
10671066

10681067
tup = self._validate_key_length(tup)
10691068

1070-
for i, key in enumerate(tup):
1069+
# Reverse tuple so that we are indexing along columns before rows
1070+
# and avoid unintended dtype inference. # GH60600
1071+
if any(isinstance(ax, MultiIndex) for ax in self.obj.axes):
1072+
enum = enumerate(tup)
1073+
else:
1074+
enum = zip(range(len(tup) - 1, -1, -1), reversed(tup))
1075+
for i, key in enum:
10711076
if is_label_like(key):
10721077
# We don't need to check for tuples here because those are
10731078
# caught by the _is_nested_tuple_indexer check above.
@@ -1095,14 +1100,7 @@ def _getitem_lowerdim(self, tup: tuple):
10951100
if com.is_null_slice(new_key):
10961101
return section
10971102
# This is an elided recursive call to iloc/loc
1098-
out = getattr(section, self.name)[new_key]
1099-
# Re-interpret dtype of out.values for loc/iloc[int, list-like].
1100-
# GH60600
1101-
if i == 0 and isinstance(key, int) and is_list_like(tup[1]):
1102-
dt = self.obj.dtypes.__getitem__(tup[1])
1103-
if len(dt) > 0:
1104-
out = out.astype(find_common_type(dt.tolist()))
1105-
return out
1103+
return getattr(section, self.name)[new_key]
11061104

11071105
raise IndexingError("not applicable")
11081106

0 commit comments

Comments
 (0)