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 |
Mutating a Series breaks the link to the DataFrame index
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?
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.