Skip to content

Commit fdf14f4

Browse files
committed
Fix regression in iloc with boolean list (pandas-dev#37432)
(cherry picked from commit 5292533)
1 parent dc39ee2 commit fdf14f4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Fixed regressions
2727
- 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`)
2828
- Fixed regression in certain offsets (:meth:`pd.offsets.Day() <pandas.tseries.offsets.Day>` and below) no longer being hashable (:issue:`37267`)
2929
- Fixed regression in :class:`StataReader` which required ``chunksize`` to be manually set when using an iterator to read a dataset (:issue:`37280`)
30+
- Fixed regression in setitem with :meth:`DataFrame.iloc` which raised error when trying to set a value while filtering with a boolean list (:issue:`36741`)
3031
- Fixed regression in :attr:`MultiIndex.is_monotonic_increasing` returning wrong results with ``NaN`` in at least one of the levels (:issue:`37220`)
3132

3233
.. ---------------------------------------------------------------------------

pandas/core/indexing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1669,8 +1669,6 @@ def _setitem_with_indexer(self, indexer, value):
16691669
"length than the value"
16701670
)
16711671

1672-
pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer
1673-
16741672
def isetter(loc, v):
16751673
# positional setting on column loc
16761674
ser = self.obj._ixs(loc, axis=1)
@@ -1680,15 +1678,15 @@ def isetter(loc, v):
16801678
# which means essentially reassign to the columns of a
16811679
# multi-dim object
16821680
# GH6149 (null slice), GH10408 (full bounds)
1683-
if isinstance(pi, tuple) and all(
1681+
if isinstance(plane_indexer, tuple) and all(
16841682
com.is_null_slice(idx) or com.is_full_slice(idx, len(self.obj))
1685-
for idx in pi
1683+
for idx in plane_indexer
16861684
):
16871685
ser = v
16881686
else:
16891687
# set the item, possibly having a dtype change
16901688
ser = ser.copy()
1691-
ser._mgr = ser._mgr.setitem(indexer=pi, value=v)
1689+
ser._mgr = ser._mgr.setitem(indexer=plane_indexer, value=v)
16921690
ser._maybe_update_cacher(clear=True)
16931691

16941692
# reset the sliced object if unique

pandas/tests/frame/indexing/test_setitem.py

+9
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,12 @@ def test_setitem_extension_types(self, obj, dtype):
184184
df["obj"] = obj
185185

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

0 commit comments

Comments
 (0)