Skip to content

Copy-on-Write (CoW): index labels are still shared mutable state

Core Problem

When turning on Copy-on-Write mode in pandas, index labels remain as shared mutable state across Series and DataFrame. This issue can cause unexpected behavior when working with Series and DataFrames.

Solution & Analysis

Reproducible Example

import pandas as pd

pd.options.mode.copy_on_write = True

df = pd.DataFrame({"a": [1, 2, 3]})
print(df)
a
0 1
1 2
2 3
s1 = df["a"]
print(s1)

s2 = df["a"]
s2.loc[1] = 100
print(s2)
0      1
1    100
2      3
Name: a, dtype: int64

new name

new name

Question: Should this Index object be treated independently from DataFrame or is it okay that a mutation here affects the DataFrame?

i = df.index
i.name = "new name"
print(df.index.name)

s1.loc[1] = 100
new name

new name

Conclusion

When using Copy-on-Write mode in pandas, index labels are still shared mutable state across Series and DataFrame. This can cause unexpected behavior when working with Series and DataFrames. To avoid this issue, consider using a different data structure or approach that does not rely on shared mutable state.

Reference