Skip to content

API / CoW: constructing Series from Series creates lazy copy (with default copy=False) #49524

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

Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/main' into cow-constructor-fro…
…m-pandas
  • Loading branch information
jorisvandenbossche committed Jan 13, 2023
commit bc01a516c1ca79c277ef0b6be8494cd38c34fbae
48 changes: 46 additions & 2 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,57 @@ be set to ``"pyarrow"`` to return pyarrow-backed, nullable :class:`ArrowDtype` (
df_pyarrow = pd.read_csv(data, use_nullable_dtypes=True, engine="pyarrow")
df_pyarrow.dtypes

Copy on write improvements
Copy-on-Write improvements
^^^^^^^^^^^^^^^^^^^^^^^^^^
Copy link
Member Author

Choose a reason for hiding this comment

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

Added a new section like is being added in #50471. We can consolidate those different blurbs later on in a coherent whatsnew.


* The :class:`Series` constructor will now create a lazy copy (deferring the copy until
- A new lazy copy mechanism that defers the copy until the object in question is modified
was added to the following methods:

- :meth:`DataFrame.reset_index` / :meth:`Series.reset_index`
- :meth:`DataFrame.set_index` / :meth:`Series.set_index`
- :meth:`DataFrame.set_axis` / :meth:`Series.set_axis`
- :meth:`DataFrame.rename_axis` / :meth:`Series.rename_axis`
- :meth:`DataFrame.rename_columns`
- :meth:`DataFrame.reindex` / :meth:`Series.reindex`
- :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like`
- :meth:`DataFrame.assign`
- :meth:`DataFrame.drop`
- :meth:`DataFrame.dropna` / :meth:`Series.dropna`
- :meth:`DataFrame.select_dtypes`
- :meth:`DataFrame.align` / :meth:`Series.align`
- :meth:`Series.to_frame`
- :meth:`DataFrame.rename` / :meth:`Series.rename`
- :meth:`DataFrame.add_prefix` / :meth:`Series.add_prefix`
- :meth:`DataFrame.add_suffix` / :meth:`Series.add_suffix`
- :meth:`DataFrame.drop_duplicates` / :meth:`Series.drop_duplicates`
- :meth:`DataFrame.reorder_levels` / :meth:`Series.reorder_levels`

These methods return views when Copy-on-Write is enabled, which provides a significant
performance improvement compared to the regular execution (:issue:`49473`).

- Accessing a single column of a DataFrame as a Series (e.g. ``df["col"]``) now always
returns a new object every time it is constructed when Copy-on-Write is enabled (not
returning multiple times an identical, cached Series object). This ensures that those
Series objects correctly follow the Copy-on-Write rules (:issue:`49450`)

- The :class:`Series` constructor will now create a lazy copy (deferring the copy until
a modification to the data happens) when constructing a Series from an existing
Series with the default of ``copy=False`` (:issue:`50471`)

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_200.enhancements.other:

Other enhancements
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.