Skip to content

BUG: fix GH45032 iloc(axis=1).__setitem__ axis argument #45150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 3, 2022
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,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
^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# -------------------------------------------------------------------
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down