4
4
import sys
5
5
from typing import (
6
6
TYPE_CHECKING ,
7
+ Any ,
7
8
Hashable ,
8
9
Sequence ,
10
+ TypeVar ,
9
11
cast ,
10
12
final ,
11
13
)
87
89
Series ,
88
90
)
89
91
92
+ T = TypeVar ("T" )
90
93
# "null slice"
91
94
_NS = slice (None , None )
92
95
_one_ellipsis_message = "indexer may only contain one '...' entry"
@@ -164,7 +167,12 @@ def iloc(self) -> _iLocIndexer:
164
167
DataFrame) and that returns valid output for indexing (one of the above).
165
168
This is useful in method chains, when you don't have a reference to the
166
169
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
+
168
176
- A tuple of row and column indexes. The tuple elements consist of one of the
169
177
above inputs, e.g. ``(0, 1)``.
170
178
@@ -872,18 +880,7 @@ def __setitem__(self, key, value) -> None:
872
880
key = tuple (com .apply_if_callable (x , self .obj ) for x in key )
873
881
else :
874
882
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 )
887
884
indexer = self ._get_setitem_indexer (key )
888
885
self ._has_valid_setitem_indexer (key )
889
886
@@ -1130,6 +1127,17 @@ def _getitem_nested_tuple(self, tup: tuple):
1130
1127
def _convert_to_indexer (self , key , axis : AxisInt ):
1131
1128
raise AbstractMethodError (self )
1132
1129
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
+
1133
1141
@final
1134
1142
def __getitem__ (self , key ):
1135
1143
check_dict_or_set_indexers (key )
@@ -1144,17 +1152,7 @@ def __getitem__(self, key):
1144
1152
axis = self .axis or 0
1145
1153
1146
1154
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 )
1158
1156
return self ._getitem_axis (maybe_callable , axis = axis )
1159
1157
1160
1158
def _is_scalar_access (self , key : tuple ):
@@ -1207,6 +1205,14 @@ def _validate_key(self, key, axis: Axis):
1207
1205
f"{ key } : boolean label can not be used without a boolean index"
1208
1206
)
1209
1207
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
+ )
1210
1216
if isinstance (key , slice ) and (
1211
1217
isinstance (key .start , bool ) or isinstance (key .stop , bool )
1212
1218
):
0 commit comments