Skip to content

Commit db54c28

Browse files
authored
BUG: fix GH45032 iloc(axis=1).__setitem__ axis argument (#45150)
1 parent d70b95b commit db54c28

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ Indexing
815815
- 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`)
816816
- 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`)
817817
- Bug in :meth:`Series.__setitem__` when setting floats or integers into integer-dtype series failing to upcast when necessary to retain precision (:issue:`45121`)
818+
- Bug in :meth:`DataFrame.iloc.__setitem__` ignores axis argument (:issue:`45032`)
818819
-
819820

820821
Missing

pandas/core/indexing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,11 @@ def _convert_to_indexer(self, key, axis: int):
15401540
def _get_setitem_indexer(self, key):
15411541
# GH#32257 Fall through to let numpy do validation
15421542
if is_iterator(key):
1543-
return list(key)
1543+
key = list(key)
1544+
1545+
if self.axis is not None:
1546+
return self._convert_tuple(key)
1547+
15441548
return key
15451549

15461550
# -------------------------------------------------------------------

pandas/tests/indexing/test_iloc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,18 @@ def test_iloc_setitem(self):
444444
expected = Series([0, 1, 0], index=[4, 5, 6])
445445
tm.assert_series_equal(s, expected)
446446

447+
def test_iloc_setitem_axis_argument(self):
448+
# GH45032
449+
df = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]])
450+
expected = DataFrame([[6, "c", 10], [7, "d", 11], [5, 5, 5]])
451+
df.iloc(axis=0)[2] = 5
452+
tm.assert_frame_equal(df, expected)
453+
454+
df = DataFrame([[6, "c", 10], [7, "d", 11], [8, "e", 12]])
455+
expected = DataFrame([[6, "c", 5], [7, "d", 5], [8, "e", 5]])
456+
df.iloc(axis=1)[2] = 5
457+
tm.assert_frame_equal(df, expected)
458+
447459
def test_iloc_setitem_list(self):
448460

449461
# setitem with an iloc list

0 commit comments

Comments
 (0)