Repository: pandas-dev/pandas
Pull request: BUG: fix GH45032 iloc(axis=1).__setitem__ axis argument
- [x] closes #45032 
- [x] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit) for how to run them
- [x] whatsnew entry


Problem statement:
BUG: iloc(axis=1).__setitem__ ignores axis

```
import numpy as np
import pandas as pd

arr = np.random.randn(4, 4)
df = pd.DataFrame(arr)
df[4] = "foo"

df.iloc(axis=1)[2] = 3
>>> df
          0         1         2         3    4
0 -0.042240  2.894031  0.086813  0.808991  foo
1  0.141410  1.998173  0.730306 -0.779878  foo
2  3.000000  3.000000  3.000000  3.000000    3
3  0.270897  1.830372  0.533242 -0.147240  foo
```

Expected Behavior:
```
>>> df
          0         1  2         3    4
0 -0.042240  2.894031  3  0.808991  foo
1  0.141410  1.998173  3 -0.779878  foo
2  1.611364  1.459234  3  0.572484  foo
3  0.270897  1.830372  3 -0.147240  foo
```

Hints from issue discussion:
hello. i'm unable to unassign myself from this issue. i attempted but couldn't figure it out. still a newbie in open source contribution. thanks
It looks like this is a side-effect of using numpy to validate the indexer in iloc._get_setitem_indexer (see #32257)

Commit message: update what's new

Patch to review:
diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst
index d770782d5dc62..eaf562500e7bd 100644
--- a/doc/source/whatsnew/v1.4.0.rst
+++ b/doc/source/whatsnew/v1.4.0.rst
@@ -842,7 +842,8 @@ 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..14604479f9309 100644
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -1540,7 +1540,12 @@ 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)
+
+        # GH#45032 handle iloc(axis=1)
+        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..ad3d3a5c89560 100644
--- a/pandas/tests/indexing/test_iloc.py
+++ b/pandas/tests/indexing/test_iloc.py
@@ -444,6 +444,19 @@ 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
+        dfa = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]])
+        dfb = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]])
+        expected_a = DataFrame([[6, "c", 10], [7, "d", 11], [5, 5, 5]])
+        expected_b = DataFrame([[6, "c", 5], [7, "d", 5], [8, "e", 5]])
+
+        dfa.iloc(axis=0)[2] = 5
+        tm.assert_frame_equal(dfa, expected_a)
+
+        dfb.iloc(axis=1)[2] = 5
+        tm.assert_frame_equal(dfb, expected_b)
+
     def test_iloc_setitem_list(self):
 
         # setitem with an iloc list


Code review comments:
diff_hunk: @@ -1540,7 +1540,12 @@ 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)
+
+        # GH#45032 handle iloc(axis=1)
path: pandas/core/indexing.py
text: @user1:
I think you can remove that comment, this is obvious?

@author:
sure, should I leave the GH45032 or remove it completely?

@user1:
I think you can remove it completly

diff_hunk: @@ -444,6 +444,19 @@ 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
+        dfa = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]])
path: pandas/tests/indexing/test_iloc.py
text: @user1:
could you do:

```
df = ...
expected = ...
df.iloc ...
tm....

df = ...
expected = ...
df.iloc...
tm...
```

Could also make two tests