Skip to content

Commit e6bd15a

Browse files
RoeiRazPingviinituutti
authored andcommitted
BUG: fix .iat assignment creates a new column (pandas-dev#24495)
1 parent 757bb7a commit e6bd15a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,7 @@ Indexing
14951495
- Bug in :class:`Index` slicing with boolean :class:`Index` may raise ``TypeError`` (:issue:`22533`)
14961496
- Bug in ``PeriodArray.__setitem__`` when accepting slice and list-like value (:issue:`23978`)
14971497
- Bug in :class:`DatetimeIndex`, :class:`TimedeltaIndex` where indexing with ``Ellipsis`` would lose their ``freq`` attribute (:issue:`21282`)
1498+
- Bug in ``iat`` where using it to assign an incompatible value would create a new column (:issue:`23236`)
14981499

14991500
Missing
15001501
^^^^^^^

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2802,7 +2802,10 @@ def _set_value(self, index, col, value, takeable=False):
28022802
except (KeyError, TypeError):
28032803

28042804
# set using a non-recursive method & reset the cache
2805-
self.loc[index, col] = value
2805+
if takeable:
2806+
self.iloc[index, col] = value
2807+
else:
2808+
self.loc[index, col] = value
28062809
self._item_cache.pop(col, None)
28072810

28082811
return self

pandas/tests/indexing/test_scalar.py

+7
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,10 @@ def test_mixed_index_at_iat_loc_iloc_dataframe(self):
198198
df.at[0, 3]
199199
with pytest.raises(KeyError):
200200
df.loc[0, 3]
201+
202+
def test_iat_setter_incompatible_assignment(self):
203+
# GH 23236
204+
result = DataFrame({'a': [0, 1], 'b': [4, 5]})
205+
result.iat[0, 0] = None
206+
expected = DataFrame({"a": [None, 1], "b": [4, 5]})
207+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)