Skip to content

Commit fef8aa1

Browse files
committed
DEPR: deprecate returning a tuple from a callable in iloc indexing
The current semantics are that tuple-destructuring of the key is performed before unwinding any callables. As such, if a callable returns a tuple for iloc, it will be handled incorrectly. To avoid this, explicitly deprecate support for this behaviour. Closes #53533.
1 parent 310b376 commit fef8aa1

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

pandas/core/indexing.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
_chained_assignment_msg,
2828
)
2929
from pandas.util._decorators import doc
30+
from pandas.util._exceptions import find_stack_level
3031

3132
from pandas.core.dtypes.cast import (
3233
can_hold_element,
@@ -869,7 +870,19 @@ def __setitem__(self, key, value) -> None:
869870
key = tuple(list(x) if is_iterator(x) else x for x in key)
870871
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
871872
else:
872-
key = com.apply_if_callable(key, self.obj)
873+
maybe_callable = com.apply_if_callable(key, self.obj)
874+
if (
875+
self.name == "iloc"
876+
and callable(key)
877+
and isinstance(maybe_callable, tuple)
878+
):
879+
warnings.warn(
880+
"Returning a tuple from a callable in iLocation indexing "
881+
"is deprecated and will be removed in a future version",
882+
FutureWarning,
883+
stacklevel=find_stack_level(),
884+
)
885+
key = maybe_callable
873886
indexer = self._get_setitem_indexer(key)
874887
self._has_valid_setitem_indexer(key)
875888

@@ -1130,6 +1143,17 @@ def __getitem__(self, key):
11301143
axis = self.axis or 0
11311144

11321145
maybe_callable = com.apply_if_callable(key, self.obj)
1146+
if (
1147+
self.name == "iloc"
1148+
and callable(key)
1149+
and isinstance(maybe_callable, tuple)
1150+
):
1151+
warnings.warn(
1152+
"Returning a tuple from a callable in iLocation indexing "
1153+
"is deprecated and will be removed in a future version",
1154+
FutureWarning,
1155+
stacklevel=find_stack_level(),
1156+
)
11331157
return self._getitem_axis(maybe_callable, axis=axis)
11341158

11351159
def _is_scalar_access(self, key: tuple):

0 commit comments

Comments
 (0)