Skip to content

Commit 231d652

Browse files
mroeschkeWillAyd
andauthored
CLN: callable to Series.iloc returning a tuple (#58626)
* CLN: callable to Series.iloc returning a tuple * undo other change * concat string * Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: William Ayd <[email protected]> --------- Co-authored-by: William Ayd <[email protected]>
1 parent d642a87 commit 231d652

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ Removal of prior version deprecations/changes
223223
- All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`)
224224
- All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`)
225225
- Changed behavior of :meth:`Series.__getitem__` and :meth:`Series.__setitem__` to always treat integer keys as labels, never as positional, consistent with :class:`DataFrame` behavior (:issue:`50617`)
226+
- Disallow a callable argument to :meth:`Series.iloc` to return a ``tuple`` (:issue:`53769`)
226227
- Disallow allowing logical operations (``||``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``); wrap the objects in :class:`Series`, :class:`Index`, or ``np.array`` first instead (:issue:`52264`)
227228
- Disallow automatic casting to object in :class:`Series` logical operations (``&``, ``^``, ``||``) between series with mismatched indexes and dtypes other than ``object`` or ``bool`` (:issue:`52538`)
228229
- Disallow calling :meth:`Series.replace` or :meth:`DataFrame.replace` without a ``value`` and with non-dict-like ``to_replace`` (:issue:`33302`)

pandas/core/indexing.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def iloc(self) -> _iLocIndexer:
159159
"""
160160
Purely integer-location based indexing for selection by position.
161161
162-
.. deprecated:: 2.2.0
162+
.. versionchanged:: 3.0
163163
164164
Returning a tuple from a callable is deprecated.
165165
@@ -905,7 +905,7 @@ def __setitem__(self, key, value) -> None:
905905
key = tuple(com.apply_if_callable(x, self.obj) for x in key)
906906
else:
907907
maybe_callable = com.apply_if_callable(key, self.obj)
908-
key = self._check_deprecated_callable_usage(key, maybe_callable)
908+
key = self._raise_callable_usage(key, maybe_callable)
909909
indexer = self._get_setitem_indexer(key)
910910
self._has_valid_setitem_indexer(key)
911911

@@ -1164,14 +1164,11 @@ def _contains_slice(x: object) -> bool:
11641164
def _convert_to_indexer(self, key, axis: AxisInt):
11651165
raise AbstractMethodError(self)
11661166

1167-
def _check_deprecated_callable_usage(self, key: Any, maybe_callable: T) -> T:
1167+
def _raise_callable_usage(self, key: Any, maybe_callable: T) -> T:
11681168
# GH53533
11691169
if self.name == "iloc" and callable(key) and isinstance(maybe_callable, tuple):
1170-
warnings.warn(
1171-
"Returning a tuple from a callable with iloc "
1172-
"is deprecated and will be removed in a future version",
1173-
FutureWarning,
1174-
stacklevel=find_stack_level(),
1170+
raise ValueError(
1171+
"Returning a tuple from a callable with iloc is not allowed.",
11751172
)
11761173
return maybe_callable
11771174

@@ -1189,7 +1186,7 @@ def __getitem__(self, key):
11891186
axis = self.axis or 0
11901187

11911188
maybe_callable = com.apply_if_callable(key, self.obj)
1192-
maybe_callable = self._check_deprecated_callable_usage(key, maybe_callable)
1189+
maybe_callable = self._raise_callable_usage(key, maybe_callable)
11931190
return self._getitem_axis(maybe_callable, axis=axis)
11941191

11951192
def _is_scalar_access(self, key: tuple):

pandas/tests/frame/indexing/test_indexing.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1019,13 +1019,13 @@ def test_single_element_ix_dont_upcast(self, float_frame):
10191019
result = df.loc[[0], "b"]
10201020
tm.assert_series_equal(result, expected)
10211021

1022-
def test_iloc_callable_tuple_return_value(self):
1023-
# GH53769
1022+
def test_iloc_callable_tuple_return_value_raises(self):
1023+
# GH53769: Enforced pandas 3.0
10241024
df = DataFrame(np.arange(40).reshape(10, 4), index=range(0, 20, 2))
1025-
msg = "callable with iloc"
1026-
with tm.assert_produces_warning(FutureWarning, match=msg):
1025+
msg = "Returning a tuple from"
1026+
with pytest.raises(ValueError, match=msg):
10271027
df.iloc[lambda _: (0,)]
1028-
with tm.assert_produces_warning(FutureWarning, match=msg):
1028+
with pytest.raises(ValueError, match=msg):
10291029
df.iloc[lambda _: (0,)] = 1
10301030

10311031
def test_iloc_row(self):

0 commit comments

Comments
 (0)