Skip to content

Commit 033c259

Browse files
Backport PR #50470 on branch 1.5.x (DOC: Add Copy on write whatsnew) (#50546)
Backport PR #50470: DOC: Add Copy on write whatsnew Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 6d269a6 commit 033c259

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

+46
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,52 @@ and attributes without holding entire tree in memory (:issue:`45442`).
290290
.. _`lxml's iterparse`: https://lxml.de/3.2/parsing.html#iterparse-and-iterwalk
291291
.. _`etree's iterparse`: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse
292292

293+
.. _whatsnew_150.enhancements.copy_on_write:
294+
295+
Copy on Write
296+
^^^^^^^^^^^^^
297+
298+
A new feature ``copy_on_write`` was added (:issue:`46958`). Copy on write ensures that
299+
any DataFrame or Series derived from another in any way always behaves as a copy.
300+
Copy on write disallows updating any other object than the object the method
301+
was applied to.
302+
303+
Copy on write can be enabled through:
304+
305+
.. code-block:: python
306+
307+
pd.set_option("mode.copy_on_write", True)
308+
pd.options.mode.copy_on_write = True
309+
310+
Alternatively, copy on write can be enabled locally through:
311+
312+
.. code-block:: python
313+
314+
with pd.option_context("mode.copy_on_write", True):
315+
...
316+
317+
Without copy on write, the parent :class:`DataFrame` is updated when updating a child
318+
:class:`DataFrame` that was derived from this :class:`DataFrame`.
319+
320+
.. ipython:: python
321+
322+
df = pd.DataFrame({"foo": [1, 2, 3], "bar": 1})
323+
view = df["foo"]
324+
view.iloc[0]
325+
df
326+
327+
With copy on write enabled, df won't be updated anymore:
328+
329+
.. ipython:: python
330+
331+
with pd.option_context("mode.copy_on_write", True):
332+
df = pd.DataFrame({"foo": [1, 2, 3], "bar": 1})
333+
view = df["foo"]
334+
view.iloc[0]
335+
df
336+
337+
A more detailed explanation can be found `here <https://phofl.github.io/cow-introduction.html>`_.
338+
293339
.. _whatsnew_150.enhancements.other:
294340

295341
Other enhancements

0 commit comments

Comments
 (0)