Skip to content

BUG: fix .iat assignment creates a new column #24495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)