Skip to content

Commit 0046ac6

Browse files
committed
Improve docstring to CoW and chained assignments
1 parent 499c5d4 commit 0046ac6

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

doc/source/user_guide/copy_on_write.rst

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,32 @@ The following code snippet updated both ``df`` and ``subset`` without CoW:
116116
117117
This is not possible anymore with CoW, since the CoW rules explicitly forbid this.
118118
This includes updating a single column as a :class:`Series` and relying on the change
119-
propagating back to the parent :class:`DataFrame`.
120-
This statement can be rewritten into a single statement with ``loc`` or ``iloc`` if
121-
this behavior is necessary. :meth:`DataFrame.where` is another suitable alternative
122-
for this case.
119+
propagating back to the parent :class:`DataFrame`. To modify a DataFrame value in a given
120+
column and row, the code must be rewritten as a single assignment to ``loc`` or ``iloc``.
121+
When the column is given by name (``loc``) and the row by position (``iloc``), you either
122+
need to convert the column name to its position using :meth:`Index.get_loc` or you need
123+
to convert the row position to its index. Both variants as shown in the following snippet:
124+
125+
.. code-block:: ipython
126+
127+
In [1]: df = pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})
128+
In [2]: df.iloc[0, df.columns.get_loc("foo")] = 100
129+
In [3]: df.loc[df.index[1], "bar"] = 200
130+
In [4]: df
131+
Out[4]:
132+
foo bar
133+
0 100 4
134+
1 2 200
135+
2 3 6
136+
137+
The ``iloc`` variant works as a direct replacement of the old code ``df["foo"].iloc[0] = 100``
138+
while the ``loc`` variant first translates the position to the index and then finds all
139+
positions with that index. It does more work and only does the same if the DataFrame has
140+
a unique row index.
141+
142+
Note that many such statements in the code can potentially hurt the performance. If possible,
143+
prefer to update the whole column at once. If you have boolean mask,
144+
:meth:`DataFrame.where` could be another suitable alternative for this case.
123145

124146
Updating a column selected from a :class:`DataFrame` with an inplace method will
125147
also not work anymore.

0 commit comments

Comments
 (0)