Skip to content

DOC: clarify "inplace"-ness of DataFrame.setitem #51328

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 7 commits into from
Feb 20, 2023
Merged
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3867,23 +3867,26 @@ def _get_value(self, index, col, takeable: bool = False) -> Scalar:

def isetitem(self, loc, value) -> None:
"""
Set the given value in the column with position 'loc'.
Set the given value in the column with position `loc`.

This is a positional analogue to __setitem__.
This is a positional analogue to `__setitem__`.

Parameters
----------
loc : int or sequence of ints
Index position for the column.
value : scalar or arraylike
Value(s) for the column.

Notes
-----
Unlike `frame.iloc[:, i] = value`, `frame.isetitem(loc, value)` will
_never_ try to set the values in place, but will always insert a new
array.
``frame.isetimtem(loc, value)`` is an in-place method as it will
modify the frame in place (not returning a new object) but it will
not update the values of the column itself, it will instead insert
a new array.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add something like "in contrast to frame.iloc[:, i] = value which will try to update the existing values inplace"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just made some examples on my side and indeed this is quite important to keep that in the docs, I will update that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be good now, let me know if the phrasing is clear enough

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be good now, let me know if the phrasing is clear enough


In cases where `frame.columns` is unique, this is equivalent to
`frame[frame.columns[i]] = value`.
In cases where ``frame.columns`` is unique, this is equivalent to
``frame[frame.columns[i]] = value``.
"""
if isinstance(value, DataFrame):
if is_scalar(loc):
Expand Down