Deep Dive into pandas Copy-on-Write Mode—Half II | by Patrick Hoefler | Aug, 2023

Explaining how Copy-on-Write optimizes efficiency

Photograph by Joshua Brown on Unsplash


The first post defined how the Copy-on-Write mechanism works. It highlights some areas the place copies are launched into the workflow. This put up will deal with optimizations that make sure that this received’t gradual the common workflow down.

We make the most of a way that pandas internals use to keep away from copying the entire DataFrame when it’s not crucial and thus, improve efficiency.

I’m a part of the pandas core crew and was closely concerned in implementing and enhancing CoW thus far. I’m an open supply engineer for Coiled the place I work on Dask, together with enhancing the pandas integration and making certain that Dask is compliant with CoW.

Elimination of defensive copies

Let’s begin with probably the most impactful enchancment. Many pandas strategies carried out defensive copies to keep away from unwanted effects to guard towards inplace modifications in a while.

df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df2 = df.reset_index()
df2.iloc[0, 0] = 100

There isn’t a want to repeat the information in reset_index, however returning a view would introduce unwanted effects when modifying the outcome, e.g. df could be up to date as properly. Therefore, a defensive copy is carried out in reset_index.

All these defensive copies are now not there when Copy-on-Write is enabled. This impacts many strategies. A full listing could be discovered here.

Moreover, choosing a columnar subset of a DataFrame will now at all times return a view as a substitute of a replica as earlier than.

Let’s have a look at what this implies performance-wise once we mix a few of these strategies:

import pandas as pd
import numpy as np

N = 2_000_000
int_df = pd.DataFrame(
np.random.randint(1, 100, (N, 10)),
columns=[f"col_{i}" for i in range(10)],
float_df = pd.DataFrame(
np.random.random((N, 10)),
columns=[f"col_{i}" for i in range(10, 20)],
str_df = pd.DataFrame(
columns=[f"col_{i}" for i in range(20, 30)],

df = pd.concat([int_df, float_df, str_df], axis=1)

This creates a DataFrame with 30 columns, 3 totally different dtypes and a pair of million rows. Let’s execute the next methodology chain on this DataFrame:

df.rename(columns={"col_1": "new_index"})
.assign(sum_val=df["col_1"] + df["col_2"])
.drop(columns=["col_10", "col_20"])
.astype({"col_5": "int32"})

All of those strategies carry out a defensive copy with out CoW enabled.

Efficiency with out CoW:

2.45 s ± 293 ms per loop (imply ± std. dev. of seven runs, 1 loop every)

Efficiency with CoW enabled:

13.7 ms ± 286 µs per loop (imply ± std. dev. of seven runs, 100 loops every)

An enchancment by roughly an element of 200. I selected this instance explicitly for example the potential advantages of CoW. Not each methodology will get that a lot sooner.

Optimizing copies triggered by inplace modifications

The earlier part illustrated many strategies the place a defensive copy is now not crucial. CoW ensures that you would be able to’t modify two objects directly. Which means now we have to introduce a replica when the identical information is referenced by two DataFrames. Let’s have a look at strategies to make these copies as environment friendly as potential.

The earlier put up confirmed that the next may set off a replica:

df.iloc[0, 0] = 100

The copy is triggered if the information that’s backing df is referenced by one other DataFrame. We assume that our DataFrame has n integer columns, e.g. is backed by a single Block.

Picture by creator

Our Reference monitoring object can be referencing one other Block, so we cannot modify the DataFrame inplace with out modifying one other object. A naive strategy could be to repeat the entire block and be executed with it.

Picture by creator

This could arrange a brand new reference monitoring object and create a brand new Block that’s backed by a contemporary NumPy array. This Block doesn’t have any extra references, so one other operation would have the ability to modify it inplace once more. This strategy copies n-1 columns that we do not essentially have to repeat. We make the most of a way we name Block splitting to keep away from this.

Picture by creator

Internally, solely the primary column is copied. All different columns are taken as views on the earlier array. The brand new Block doesn’t share any references with different columns. The previous Block nonetheless shares references with different objects since it’s only a view on the earlier values.

There may be one drawback to this method. The preliminary array has n columns. We created a view on columns 2 until n, however this retains the entire array alive. We additionally added a brand new array with one column for the primary column. This may preserve a bit extra reminiscence alive than crucial.

This technique straight interprets to DataFrames with totally different dtypes. All Blocks that aren’t modified in any respect are returned as is and solely Blocks which are modified inplace are break up.

Picture by creator

We now set a brand new worth into column n+1 the float Block to create a view on columns n+2 to m. The brand new Block will solely again column n+1.

df.iloc[0, n+1] = 100.5
Picture by creator

Strategies that may function inplace

The indexing operations we checked out don’t typically create a brand new object; they modify the present object inplace, together with the information of stated object. One other group of pandas strategies doesn’t contact the information of the DataFrame in any respect. One outstanding instance is rename. Rename solely adjustments the labels. These strategies can make the most of the lazy-copy mechanism talked about above.

There may be one other third group of strategies that may really be executed inplace, like change or fillna. These will at all times set off a replica.

df2 = df.change(...)

Modifying the information inplace with out triggering a replica would modify df and df2, which violates CoW guidelines. This is likely one of the explanation why we contemplate maintaining the inplace key phrase for these strategies.

df.change(..., inplace=True)

This could eliminate this downside. It’s nonetheless an open proposal and may go into a distinct course. That stated, this solely pertains to columns which are really modified; all different columns are returned as views anyway. Which means just one column is copied in case your worth is simply present in one column.


We examine how CoW adjustments pandas inner habits and the way this may translate to enhancements in your code. Many strategies will get sooner with CoW, whereas we’ll see a slowdown in a few indexing associated operations. Beforehand, these operations at all times operated inplace, which could have produced unwanted effects. These unwanted effects are gone with CoW and a modification on one DataFrame object won’t ever influence one other.

The subsequent put up on this collection will clarify how one can replace your code to be compliant with CoW. Additionally, we’ll clarify which patterns to keep away from sooner or later.

Thanks for studying. Be happy to achieve out to share your ideas and suggestions about Copy-on-Write.

Posit AI Weblog: torch 0.9.0

Posit AI Weblog: Discrete Fourier Rework