Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Fixed regressions
- Fixed regression where slicing :class:`DatetimeIndex` raised :exc:`AssertionError` on irregular time series with ``pd.NaT`` or on unsorted indices (:issue:`36953` and :issue:`35509`)
- 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`)
- Fixed regression in :attr:`MultiIndex.is_monotonic_increasing` returning wrong results with ``NaN`` in at least one of the levels (:issue:`37220`)

.. ---------------------------------------------------------------------------
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
9 changes: 9 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,12 @@ def test_setitem_periodindex(self):
rs = df.reset_index().set_index("index")
assert isinstance(rs.index, PeriodIndex)
tm.assert_index_equal(rs.index, rng)

@pytest.mark.parametrize("klass", [list, np.array])
def test_iloc_setitem_bool_indexer(self, klass):
# GH: 36741
df = DataFrame({"flag": ["x", "y", "z"], "value": [1, 3, 4]})
indexer = klass([True, False, False])
df.iloc[indexer, 1] = df.iloc[indexer, 1] * 2
expected = DataFrame({"flag": ["x", "y", "z"], "value": [2, 3, 4]})
tm.assert_frame_equal(df, expected)