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/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 diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index fbbfdfefb67e6..e4b8181a67514 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 + 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)