From 9515bb744ff03d051cf4116fc45f4de7de23abc0 Mon Sep 17 00:00:00 2001 From: RoeiRaz Date: Sun, 30 Dec 2018 12:07:12 +0200 Subject: [PATCH 1/3] BUG: fix .iat assignment creates a new column - in response to gh-23236 - changes the fallback of .iat to .iloc on type error --- pandas/core/frame.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a34a34186cf45..7657ffd9cf7da 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2716,7 +2716,10 @@ def _set_value(self, index, col, value, takeable=False): except (KeyError, TypeError): # set using a non-recursive method & reset the cache - self.loc[index, col] = value + if takeable: + self.iloc[index, col] = value + else: + self.loc[index, col] = value self._item_cache.pop(col, None) return self From 2a38d8388fe019ac54a241d2eacb076032e038f3 Mon Sep 17 00:00:00 2001 From: RoeiRaz Date: Sun, 30 Dec 2018 22:25:44 +0200 Subject: [PATCH 2/3] adds tests and whatsnew entry to #9515bb7 --- doc/source/whatsnew/v0.24.0.rst | 1 + pandas/tests/indexing/test_scalar.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index a84fd118061bc..dd4e9bc862312 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1493,6 +1493,7 @@ Indexing - Bug in :class:`Index` slicing with boolean :class:`Index` may raise ``TypeError`` (:issue:`22533`) - Bug in ``PeriodArray.__setitem__`` when accepting slice and list-like value (:issue:`23978`) - Bug in :class:`DatetimeIndex`, :class:`TimedeltaIndex` where indexing with ``Ellipsis`` would lose their ``freq`` attribute (:issue:`21282`) +- Bug in ``iat`` where using it to assign an incompatible value would create a new column (:issue:`23236`) Missing ^^^^^^^ diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index fbbfdfefb67e6..bba00ab979bff 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -198,3 +198,10 @@ def test_mixed_index_at_iat_loc_iloc_dataframe(self): df.at[0, 3] with pytest.raises(KeyError): df.loc[0, 3] + + def test_iat_setter_incompatible_assignment(self): + # GH 23236 + df = DataFrame({'a': [0, 1], 'b': [4, 5]}) + df.iat[0, 0] = None + assert np.isnan(df.iat[0, 0]) + assert len(df.index) == 2 From 486c25de0e2423da1089cecb1d7becbfcabd4c8c Mon Sep 17 00:00:00 2001 From: RoeiRaz Date: Sun, 30 Dec 2018 23:08:31 +0200 Subject: [PATCH 3/3] changes test to use assert_frame_equal() --- pandas/tests/indexing/test_scalar.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index bba00ab979bff..e4b8181a67514 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -201,7 +201,7 @@ def test_mixed_index_at_iat_loc_iloc_dataframe(self): def test_iat_setter_incompatible_assignment(self): # GH 23236 - df = DataFrame({'a': [0, 1], 'b': [4, 5]}) - df.iat[0, 0] = None - assert np.isnan(df.iat[0, 0]) - assert len(df.index) == 2 + result = DataFrame({'a': [0, 1], 'b': [4, 5]}) + result.iat[0, 0] = None + expected = DataFrame({"a": [None, 1], "b": [4, 5]}) + tm.assert_frame_equal(result, expected)