diff --git a/doc/source/whatsnew/v1.1.4.rst b/doc/source/whatsnew/v1.1.4.rst index 467e3ef00c1a7..6cb728800dc68 100644 --- a/doc/source/whatsnew/v1.1.4.rst +++ b/doc/source/whatsnew/v1.1.4.rst @@ -27,6 +27,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() ` 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 setitem with :meth:`DataFrame.iloc` which raised error when trying to set a value while 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`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 5a24addf46d93..d21ff6ee17537 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1669,8 +1669,6 @@ def _setitem_with_indexer(self, indexer, value): "length than the value" ) - pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer - def isetter(loc, v): # positional setting on column loc ser = self.obj._ixs(loc, axis=1) @@ -1680,15 +1678,15 @@ def isetter(loc, v): # which means essentially reassign to the columns of a # multi-dim object # GH6149 (null slice), GH10408 (full bounds) - if isinstance(pi, tuple) and all( + if isinstance(plane_indexer, tuple) and all( com.is_null_slice(idx) or com.is_full_slice(idx, len(self.obj)) - for idx in pi + for idx in plane_indexer ): ser = v else: # set the item, possibly having a dtype change ser = ser.copy() - ser._mgr = ser._mgr.setitem(indexer=pi, value=v) + ser._mgr = ser._mgr.setitem(indexer=plane_indexer, value=v) ser._maybe_update_cacher(clear=True) # reset the sliced object if unique diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index c5945edfd3127..3d19a2567f3a5 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -184,3 +184,12 @@ def test_setitem_extension_types(self, obj, dtype): df["obj"] = obj tm.assert_frame_equal(df, expected) + + @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)