Skip to content

Commit dd2e721

Browse files
authored
Deprecate DataFrame indexer for iloc setitem and getitem (#39022)
1 parent 9fdb8f6 commit dd2e721

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Other enhancements
128128
- :meth:`.Rolling.sum`, :meth:`.Expanding.sum`, :meth:`.Rolling.mean`, :meth:`.Expanding.mean`, :meth:`.Rolling.median`, :meth:`.Expanding.median`, :meth:`.Rolling.max`, :meth:`.Expanding.max`, :meth:`.Rolling.min`, and :meth:`.Expanding.min` now support ``Numba`` execution with the ``engine`` keyword (:issue:`38895`)
129129
- :meth:`DataFrame.apply` can now accept NumPy unary operators as strings, e.g. ``df.apply("sqrt")``, which was already the case for :meth:`Series.apply` (:issue:`39116`)
130130
- :meth:`DataFrame.apply` can now accept non-callable DataFrame properties as strings, e.g. ``df.apply("size")``, which was already the case for :meth:`Series.apply` (:issue:`39116`)
131+
- Disallow :class:`DataFrame` indexer for ``iloc`` for :meth:`Series.__getitem__` and :meth:`DataFrame.__getitem__`, (:issue:`39004`)
131132
- :meth:`Series.apply` can now accept list-like or dictionary-like arguments that aren't lists or dictionaries, e.g. ``ser.apply(np.array(["sum", "mean"]))``, which was already the case for :meth:`DataFrame.apply` (:issue:`39140`)
132133
- :meth:`DataFrame.plot.scatter` can now accept a categorical column as the argument to ``c`` (:issue:`12380`, :issue:`31357`)
133134
- :meth:`.Styler.set_tooltips` allows on hover tooltips to be added to styled HTML dataframes (:issue:`35643`, :issue:`21266`, :issue:`39317`)
@@ -318,6 +319,7 @@ Deprecations
318319
- Deprecated comparison of :class:`Timestamp` object with ``datetime.date`` objects. Instead of e.g. ``ts <= mydate`` use ``ts <= pd.Timestamp(mydate)`` or ``ts.date() <= mydate`` (:issue:`36131`)
319320
- Deprecated :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`)
320321
- Deprecated :attr:`Rolling.is_datetimelike` (:issue:`38963`)
322+
- Deprecated :class:`DataFrame` indexer for :meth:`Series.__setitem__` and :meth:`DataFrame.__setitem__` (:issue:`39004`)
321323
- Deprecated :meth:`core.window.ewm.ExponentialMovingWindow.vol` (:issue:`39220`)
322324
- Using ``.astype`` to convert between ``datetime64[ns]`` dtype and :class:`DatetimeTZDtype` is deprecated and will raise in a future version, use ``obj.tz_localize`` or ``obj.dt.tz_localize`` instead (:issue:`38622`)
323325
- Deprecated casting ``datetime.date`` objects to ``datetime64`` when used as ``fill_value`` in :meth:`DataFrame.unstack`, :meth:`DataFrame.shift`, :meth:`Series.shift`, and :meth:`DataFrame.reindex`, pass ``pd.Timestamp(dateobj)`` instead (:issue:`39767`)

pandas/core/indexing.py

+15
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,15 @@ def _has_valid_setitem_indexer(self, indexer) -> bool:
14211421
if isinstance(indexer, dict):
14221422
raise IndexError("iloc cannot enlarge its target object")
14231423

1424+
if isinstance(indexer, ABCDataFrame):
1425+
warnings.warn(
1426+
"DataFrame indexer for .iloc is deprecated and will be removed in"
1427+
"a future version.\n"
1428+
"consider using .loc with a DataFrame indexer for automatic alignment.",
1429+
FutureWarning,
1430+
stacklevel=3,
1431+
)
1432+
14241433
if not isinstance(indexer, tuple):
14251434
indexer = _tuplify(self.ndim, indexer)
14261435

@@ -1508,6 +1517,12 @@ def _get_list_axis(self, key, axis: int):
15081517
raise IndexError("positional indexers are out-of-bounds") from err
15091518

15101519
def _getitem_axis(self, key, axis: int):
1520+
if isinstance(key, ABCDataFrame):
1521+
raise IndexError(
1522+
"DataFrame indexer is not allowed for .iloc\n"
1523+
"Consider using .loc for automatic alignment."
1524+
)
1525+
15111526
if isinstance(key, slice):
15121527
return self._get_slice_axis(key, axis=axis)
15131528

pandas/tests/indexing/test_iloc.py

+14
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,20 @@ def test_iloc_getitem_setitem_fancy_exceptions(self, float_frame):
10901090
# GH#32257 we let numpy do validation, get their exception
10911091
float_frame.iloc[:, :, :] = 1
10921092

1093+
def test_iloc_frame_indexer(self):
1094+
# GH#39004
1095+
df = DataFrame({"a": [1, 2, 3]})
1096+
indexer = DataFrame({"a": [True, False, True]})
1097+
with tm.assert_produces_warning(FutureWarning):
1098+
df.iloc[indexer] = 1
1099+
1100+
msg = (
1101+
"DataFrame indexer is not allowed for .iloc\n"
1102+
"Consider using .loc for automatic alignment."
1103+
)
1104+
with pytest.raises(IndexError, match=msg):
1105+
df.iloc[indexer]
1106+
10931107

10941108
class TestILocSetItemDuplicateColumns:
10951109
def test_iloc_setitem_scalar_duplicate_columns(self):

0 commit comments

Comments
 (0)