-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Add Copy on write whatsnew #50470
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,6 +290,52 @@ 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 (: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. | ||
|
||
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): | ||
... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a small example that shows the effect of copy on write? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
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 <https://phofl.github.io/cow-introduction.html>`_. | ||
|
||
.. _whatsnew_150.enhancements.other: | ||
|
||
Other enhancements | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure we should encourage this usage by explicitly mentioning it (it's a general option, so if you are familiar with the option function, you can always use it this way using the existing
option_context
).This is tricky as this will only work if the data you are using it created within that block. Once you only put this around an operation, with data created before the block, things might go sideways.
(that's probably a limitation of the setting the option that we should document anyway)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep it for now, since support on the 1.5.x branch is limited. It's more or less just to try it out. But agree we should maybe document this more clearly.