Skip to content

Commit d57a3f0

Browse files
committed
Refactor check into method
1 parent 268f5e5 commit d57a3f0

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

pandas/core/indexing.py

+30-24
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import sys
55
from typing import (
66
TYPE_CHECKING,
7+
Any,
78
Hashable,
89
Sequence,
10+
TypeVar,
911
cast,
1012
final,
1113
)
@@ -87,6 +89,7 @@
8789
Series,
8890
)
8991

92+
T = TypeVar("T")
9093
# "null slice"
9194
_NS = slice(None, None)
9295
_one_ellipsis_message = "indexer may only contain one '...' entry"
@@ -164,7 +167,12 @@ def iloc(self) -> _iLocIndexer:
164167
DataFrame) and that returns valid output for indexing (one of the above).
165168
This is useful in method chains, when you don't have a reference to the
166169
calling object, but would like to base your selection on
167-
some value. Note that returning a tuple from a callable is deprecated.
170+
some value.
171+
172+
.. deprecated:: 2.1.0
173+
174+
Returning a tuple from a callable is deprecated.
175+
168176
- A tuple of row and column indexes. The tuple elements consist of one of the
169177
above inputs, e.g. ``(0, 1)``.
170178
@@ -872,18 +880,7 @@ def __setitem__(self, key, value) -> None:
872880
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
873881
else:
874882
maybe_callable = com.apply_if_callable(key, self.obj)
875-
if (
876-
self.name == "iloc"
877-
and callable(key)
878-
and isinstance(maybe_callable, tuple)
879-
):
880-
warnings.warn(
881-
"Returning a tuple from a callable in iLocation indexing "
882-
"is deprecated and will be removed in a future version",
883-
FutureWarning,
884-
stacklevel=find_stack_level(),
885-
)
886-
key = maybe_callable
883+
key = self._check_deprecated_callable_usage(key, maybe_callable)
887884
indexer = self._get_setitem_indexer(key)
888885
self._has_valid_setitem_indexer(key)
889886

@@ -1130,6 +1127,17 @@ def _getitem_nested_tuple(self, tup: tuple):
11301127
def _convert_to_indexer(self, key, axis: AxisInt):
11311128
raise AbstractMethodError(self)
11321129

1130+
def _check_deprecated_callable_usage(self, key: Any, maybe_callable: T) -> T:
1131+
# GH53533
1132+
if self.name == "iloc" and callable(key) and isinstance(maybe_callable, tuple):
1133+
warnings.warn(
1134+
"Returning a tuple from a callable in iLocation indexing "
1135+
"is deprecated and will be removed in a future version",
1136+
FutureWarning,
1137+
stacklevel=find_stack_level(),
1138+
)
1139+
return maybe_callable
1140+
11331141
@final
11341142
def __getitem__(self, key):
11351143
check_dict_or_set_indexers(key)
@@ -1144,17 +1152,7 @@ def __getitem__(self, key):
11441152
axis = self.axis or 0
11451153

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

11601158
def _is_scalar_access(self, key: tuple):
@@ -1207,6 +1205,14 @@ def _validate_key(self, key, axis: Axis):
12071205
f"{key}: boolean label can not be used without a boolean index"
12081206
)
12091207

1208+
if isinstance(key, tuple) and not isinstance(ax, MultiIndex):
1209+
warnings.warn(
1210+
"Treating tuple keys as list-like for non-multiindices, "
1211+
"rather than a single label is deprecated and will be "
1212+
"removed in a future version",
1213+
FutureWarning,
1214+
stacklevel=find_stack_level(),
1215+
)
12101216
if isinstance(key, slice) and (
12111217
isinstance(key.start, bool) or isinstance(key.stop, bool)
12121218
):

0 commit comments

Comments
 (0)