From 35bcee1e53ca644f6881b4e675bd2bd1fc9c274b Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 28 Dec 2022 20:25:47 +0100 Subject: [PATCH 1/3] DOC: Add Copy on write whatsnew --- doc/source/whatsnew/v1.5.0.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index a1c374db91f8b..2335745c21f66 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -290,6 +290,30 @@ and attributes without holding entire tree in memory (:issue:`45442`). .. _`lxml's iterparse`: https://lxml.de/3.2/parsing.html#iterparse-and-iterwalk .. _`etree's iterparse`: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse +.. _whatsnew_150.enhancements.copy_on_write: + +Copy on Write +^^^^^^^^^^^^^ + +A new feature ``copy_on_write`` was added. Copy on write ensures that +any DataFrame or Series derived from another in any way always behaves as a copy. +Copy on write disallows updating any other object than the object the method +was applied to. + +Copy on write can be enabled through: + +.. code-block:: python + + pd.set_option("mode.copy_on_write", True) + pd.options.mode.copy_on_write = True + +Alternatively, copy on write can be enabled locally through: + +.. code-block:: python + + with pd.option_context("mode.copy_on_write", True): + ... + .. _whatsnew_150.enhancements.other: Other enhancements From 4d5e7381dfdf381617d1326e06a2bbf67340f116 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Dec 2022 21:05:53 +0100 Subject: [PATCH 2/3] Expand whatsnew --- doc/source/whatsnew/v1.5.0.rst | 24 +++++++++++++++++++++++- pandas/conftest.py | 1 + 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 2335745c21f66..b61547d1523cf 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -295,7 +295,7 @@ and attributes without holding entire tree in memory (:issue:`45442`). Copy on Write ^^^^^^^^^^^^^ -A new feature ``copy_on_write`` was added. Copy on write ensures that +A new feature ``copy_on_write`` was added (:issue:`46958`). Copy on write ensures that any DataFrame or Series derived from another in any way always behaves as a copy. Copy on write disallows updating any other object than the object the method was applied to. @@ -314,6 +314,28 @@ Alternatively, copy on write can be enabled locally through: with pd.option_context("mode.copy_on_write", True): ... +Without copy on write, the parent :class:`DataFrame` is updated when updating a child +:class:`DataFrame` that was derived from this :class:`DataFrame`. + +.. ipython:: python + + df = pd.DataFrame({"foo": [1, 2, 3], "bar": 1}) + view = df["foo"] + view.iloc[0] + df + +With copy on write enabled, df won't be updated anymore: + +.. ipython:: python + + with pd.option_context("mode.copy_on_write", True): + df = pd.DataFrame({"foo": [1, 2, 3], "bar": 1}) + view = df["foo"] + view.iloc[0] + df + +A more detailed explanation can be found `here `_. + .. _whatsnew_150.enhancements.other: Other enhancements diff --git a/pandas/conftest.py b/pandas/conftest.py index 3b167d9ef4fe2..14c4e59778c9a 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -1903,6 +1903,7 @@ def using_copy_on_write() -> bool: """ Fixture to check if Copy-on-Write is enabled. """ + pd.options.mode.copy_on_write = True return pd.options.mode.copy_on_write and pd.options.mode.data_manager == "block" From 7cf0a0038102046148755d74ad9143c382fea7c6 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Thu, 29 Dec 2022 21:08:01 +0100 Subject: [PATCH 3/3] Fix --- pandas/conftest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 14c4e59778c9a..3b167d9ef4fe2 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -1903,7 +1903,6 @@ def using_copy_on_write() -> bool: """ Fixture to check if Copy-on-Write is enabled. """ - pd.options.mode.copy_on_write = True return pd.options.mode.copy_on_write and pd.options.mode.data_manager == "block"