Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate DataFrame indexer for iloc setitem and getitem #39022

Merged
merged 14 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,9 @@ def _has_valid_setitem_indexer(self, indexer) -> bool:
if isinstance(indexer, dict):
raise IndexError("iloc cannot enlarge its target object")

if isinstance(indexer, ABCDataFrame):
raise IndexError("DataFrame indexer is not allowed for iloc")

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

Expand Down Expand Up @@ -1480,6 +1483,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")
phofl marked this conversation as resolved.
Show resolved Hide resolved

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
obj = frame_or_series([1, 2, 3])
indexer = DataFrame([True, True, False])
msg = "DataFrame indexer is not allowed for iloc"
with pytest.raises(IndexError, match=msg):
obj.iloc[indexer] = 1

with pytest.raises(IndexError, match=msg):
obj.iloc[indexer]


class TestILocSetItemDuplicateColumns:
def test_iloc_setitem_scalar_duplicate_columns(self):
Expand Down