From de85a1edf25c2b6f5a3657c8e2806df96fae9c71 Mon Sep 17 00:00:00 2001 From: Elle Hanson Date: Fri, 31 Dec 2021 18:27:24 -0500 Subject: [PATCH 1/5] GH45032 fix iloc._get_setitem_indexer --- pandas/core/indexing.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 # ------------------------------------------------------------------- From d0803dd25289e645501d67ba80b935f893d2d542 Mon Sep 17 00:00:00 2001 From: Elle Hanson Date: Fri, 31 Dec 2021 18:27:51 -0500 Subject: [PATCH 2/5] test for GH45032 --- pandas/tests/indexing/test_iloc.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 From 2bc92563eb7fb6e1d724b612cac5f803d526042f Mon Sep 17 00:00:00 2001 From: Elle <42851573+ellequelle@users.noreply.github.com> Date: Fri, 31 Dec 2021 18:51:32 -0500 Subject: [PATCH 3/5] update what's new --- doc/source/whatsnew/v1.4.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 ^^^^^^^ From ed058d2f077d3656ac49271b4c0fa253902efeec Mon Sep 17 00:00:00 2001 From: Elle Hanson Date: Fri, 31 Dec 2021 19:28:29 -0500 Subject: [PATCH 4/5] reoganize tests for GH45032 & remove comment --- pandas/core/indexing.py | 1 - pandas/tests/indexing/test_iloc.py | 17 ++++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 14604479f9309..0ac44ba53ae05 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1542,7 +1542,6 @@ def _get_setitem_indexer(self, key): if is_iterator(key): key = list(key) - # GH#45032 handle iloc(axis=1) if self.axis is not None: return self._convert_tuple(key) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index ad3d3a5c89560..ee9d276925d41 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -446,16 +446,15 @@ def test_iloc_setitem(self): 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) + 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) - dfb.iloc(axis=1)[2] = 5 - tm.assert_frame_equal(dfb, expected_b) + 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): From ab8b2aa269936c06b533eac195b3892b542a1911 Mon Sep 17 00:00:00 2001 From: Elle Hanson Date: Sat, 1 Jan 2022 11:17:28 -0500 Subject: [PATCH 5/5] pre-commit changes --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 4046e3cc6754b..029fb187740b3 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -816,7 +816,7 @@ Indexing - 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 ^^^^^^^