diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 4c3e53ddcfa26..029fb187740b3 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -815,6 +815,7 @@ Indexing - Fixed regression where a single column ``np.matrix`` was no longer coerced to a 1d ``np.ndarray`` when added to a :class:`DataFrame` (:issue:`42376`) - Bug in :meth:`Series.__getitem__` with a :class:`CategoricalIndex` of integers treating lists of integers as positional indexers, inconsistent with the behavior with a single scalar integer (:issue:`15470`, :issue:`14865`) - Bug in :meth:`Series.__setitem__` when setting floats or integers into integer-dtype series failing to upcast when necessary to retain precision (:issue:`45121`) +- Bug in :meth:`DataFrame.iloc.__setitem__` ignores axis argument (:issue:`45032`) - Missing diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 8f776d280afad..0ac44ba53ae05 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1540,7 +1540,11 @@ def _convert_to_indexer(self, key, axis: int): def _get_setitem_indexer(self, key): # GH#32257 Fall through to let numpy do validation if is_iterator(key): - return list(key) + key = list(key) + + if self.axis is not None: + return self._convert_tuple(key) + return key # ------------------------------------------------------------------- diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 17a990a6c7a38..ee9d276925d41 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -444,6 +444,18 @@ def test_iloc_setitem(self): expected = Series([0, 1, 0], index=[4, 5, 6]) tm.assert_series_equal(s, expected) + def test_iloc_setitem_axis_argument(self): + # GH45032 + df = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]]) + expected = DataFrame([[6, "c", 10], [7, "d", 11], [5, 5, 5]]) + df.iloc(axis=0)[2] = 5 + tm.assert_frame_equal(df, expected) + + df = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]]) + expected = DataFrame([[6, "c", 5], [7, "d", 5], [8, "e", 5]]) + df.iloc(axis=1)[2] = 5 + tm.assert_frame_equal(df, expected) + def test_iloc_setitem_list(self): # setitem with an iloc list