Copy-on-Write Enabled by Default
Pandas 3.0 enables Copy-on-Write (CoW) by default. This mechanism fundamentally changes copy semantics: a DataFrame derived from another shares the underlying memory until a modification is made, at which point a real copy is created.
Before / After
The old behaviour could cause unexpected modifications through shared views. With CoW, every modification is isolated.
python
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
# Create a subset
subset = df[['a']]
# BEFORE Pandas 3.0 (without CoW):
# subset['a'] = 99 # ALSO modifies df! (SettingWithCopyWarning)
# WITH Pandas 3.0 (CoW enabled):
subset['a'] = 99
print(subset)
# a
# 0 99
# 1 99
# 2 99
print(df)
# a b
# 0 1 4 <- df is NOT modified
# 1 2 5
# 2 3 6
Performance Impact and Migration
CoW reduces unnecessary memory copies: as long as you don't modify, memory is shared. Operations like df.rename(), df.set_index(), or df.reset_index() no longer copy data. To migrate, remove defensive .copy() calls that are no longer needed.
python
import pandas as pd
df = pd.DataFrame({'x': range(1_000_000), 'y': range(1_000_000)})
# These operations DO NOT copy data (CoW)
df2 = df.rename(columns={'x': 'col_x'})
df3 = df.reset_index()
df4 = df[['x']] # no copy as long as we don't modify
# Copy only happens WHEN you modify
df4['x'] = 0 # here, a copy is triggered for df4
# Migration: remove defensive .copy() calls
# Before: subset = df[['a']].copy() # necessary
# After: subset = df[['a']] # sufficient with CoW
