Skip to content

Fix regression in iloc with boolean list #37432

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

Merged
merged 8 commits into from
Oct 28, 2020
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.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Fixed regressions
- Fixed regression in :class:`PeriodDtype` comparing both equal and unequal to its string representation (:issue:`37265`)
- Fixed regression in certain offsets (:meth:`pd.offsets.Day() <pandas.tseries.offsets.Day>` and below) no longer being hashable (:issue:`37267`)
- Fixed regression in :class:`StataReader` which required ``chunksize`` to be manually set when using an iterator to read a dataset (:issue:`37280`)
- Fixed regression in :meth:`DataFrame.iloc.__setitem__` which raised error when trying to set a value after filtering with a boolean list (:issue:`36741`)

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

Expand Down
16 changes: 8 additions & 8 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,6 @@ def _setitem_with_indexer(self, indexer, value):
"length than the value"
)

pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer

# we need an iterable, with a ndim of at least 1
# eg. don't pass through np.array(0)
if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0:
Expand All @@ -1698,7 +1696,7 @@ def _setitem_with_indexer(self, indexer, value):
else:
v = np.nan

self._setitem_single_column(loc, v, pi)
self._setitem_single_column(loc, v, plane_indexer)

elif not unique_cols:
raise ValueError(
Expand All @@ -1716,7 +1714,7 @@ def _setitem_with_indexer(self, indexer, value):
else:
v = np.nan

self._setitem_single_column(loc, v, pi)
self._setitem_single_column(loc, v, plane_indexer)

# we have an equal len ndarray/convertible to our labels
# hasattr first, to avoid coercing to ndarray without reason.
Expand All @@ -1735,7 +1733,9 @@ def _setitem_with_indexer(self, indexer, value):

for i, loc in enumerate(ilocs):
# setting with a list, re-coerces
self._setitem_single_column(loc, value[:, i].tolist(), pi)
self._setitem_single_column(
loc, value[:, i].tolist(), plane_indexer
)

elif (
len(labels) == 1
Expand All @@ -1744,7 +1744,7 @@ def _setitem_with_indexer(self, indexer, value):
):
# we have an equal len list/ndarray
# We only get here with len(labels) == len(ilocs) == 1
self._setitem_single_column(ilocs[0], value, pi)
self._setitem_single_column(ilocs[0], value, plane_indexer)

elif lplane_indexer == 0 and len(value) == len(self.obj.index):
# We get here in one case via .loc with a all-False mask
Expand All @@ -1759,12 +1759,12 @@ def _setitem_with_indexer(self, indexer, value):
)

for loc, v in zip(ilocs, value):
self._setitem_single_column(loc, v, pi)
self._setitem_single_column(loc, v, plane_indexer)
else:

# scalar value
for loc in ilocs:
self._setitem_single_column(loc, value, pi)
self._setitem_single_column(loc, value, plane_indexer)

else:
self._setitem_single_block(indexer, value)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,11 @@ def test_iloc_setitem_series_duplicate_columns(self):
)
df.iloc[:, 0] = df.iloc[:, 0].astype(np.float64)
assert df.dtypes.iloc[2] == np.int64


def test_iloc_setitem_bool_array():
Copy link
Member

Choose a reason for hiding this comment

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

could also be put somewhere in pandas\tests\frame\indexing\

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's the file I looked for there :) Thanks

# GH: 36741
df = DataFrame({"flag": ["x", "y", "z"], "value": [1, 3, 4]})
df.iloc[[True, False, False], 1] = df.iloc[[True, False, False], 1] * 2
expected = DataFrame({"flag": ["x", "y", "z"], "value": [2, 3, 4]})
tm.assert_frame_equal(df, expected)