Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Other enhancements
- Add support for dict-like names in :class:`MultiIndex.set_names` and :class:`MultiIndex.rename` (:issue:`20421`)
- :func:`pandas.read_excel` can now auto detect .xlsb files (:issue:`35416`)
- :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`)
- Disallow :class:`DataFrame` indexer for ``iloc`` to make behavior consistent between :meth:`Series.__getitem__`, :meth:`DataFrame.__getitem__`, :meth:`Series.__setitem__` and :meth:`DataFrame.__setitem__` (:issue:`39004`)

.. ---------------------------------------------------------------------------

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,14 @@ def _has_valid_setitem_indexer(self, indexer) -> bool:
if isinstance(indexer, dict):
raise IndexError("iloc cannot enlarge its target object")

if isinstance(indexer, ABCDataFrame):
warnings.warn(
"DataFrame indexer for iloc is deprecated and will be removed in "
"a future version",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repeated 'a future version'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, changed

FutureWarning,
stacklevel=3,
)

if not isinstance(indexer, tuple):
indexer = _tuplify(self.ndim, indexer)

Expand Down Expand Up @@ -1480,6 +1488,9 @@ def _get_list_axis(self, key, axis: int):
raise IndexError("positional indexers are out-of-bounds") from err

def _getitem_axis(self, key, axis: int):
if isinstance(key, ABCDataFrame):
raise IndexError("DataFrame indexer is not allowed for iloc")

if isinstance(key, slice):
return self._get_slice_axis(key, axis=axis)

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,17 @@ def test_iloc_float_raises(self, series_with_simple_index, frame_or_series):
with pytest.raises(IndexError, match=_slice_iloc_msg):
obj.iloc[3.0] = 0

def test_iloc_frame_indexer(self, frame_or_series):
# GH#39004
df = DataFrame({"a": [1, 2, 3]})
indexer = DataFrame({"a": [True, False, True]})
with tm.assert_produces_warning(FutureWarning):
df.iloc[indexer] = 1

msg = "DataFrame indexer is not allowed for iloc"
with pytest.raises(IndexError, match=msg):
df.iloc[indexer]


class TestILocSetItemDuplicateColumns:
def test_iloc_setitem_scalar_duplicate_columns(self):
Expand Down