Sub topic 1
The problem: was that a view or a copy?
pandas 3 uses Copy-on-Write (CoW) as its only behavior. CoW is a rule for handling data shared by a DataFrame and objects derived from it, such as a Series made by selecting a column.
Prerequisites
You should know how to create DataFrames, select rows and columns, and use .loc and .iloc. Basic Python references and NumPy arrays are helpful.
The problem: was that a view or a copy?
Before pandas 3, selecting data could produce either a view or a copy. A view shared the original data, so changing it might change the original DataFrame. A copy had separate data, so changing it did not.
The result could depend on the operation, data types, and internal details. Code could appear to work while producing a warning, or silently leave the original unchanged.
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
col = df["a"]
col.iloc[0] = 100
print(col.iloc[0]) # 100
print(df["a"].iloc[0]) # 1In pandas 3, col behaves independently. The assignment changes col, not df.
The one rule of Copy-on-Write
A derived DataFrame or Series never changes the object it came from. pandas may share the underlying memory at first, but it makes a private copy when a write requires one. This is called a lazy copy.
The rule removes the need to predict whether an operation returned a view. Always treat a selected object as independent from its parent.
Assigning values safely with `.loc` and `.iloc`
To update the original DataFrame, perform selection and assignment in one operation. Use .loc for labels or Boolean conditions, and .iloc for integer positions.
import pandas as pd
df = pd.DataFrame({"foo": [1, 2, 3], "bar": [4, 5, 6]})
df.loc[df["foo"] > 1, "bar"] = 100
df.iloc[0, 0] = 10
print(df)The first assignment updates bar where foo is greater than 1. The second updates the first row and first column. Both operations target df directly.
Exercise
After col = df["a"], which object changes when you run col.iloc[0] = 9 in pandas 3?
Show answer
Only col changes. The original df remains unchanged.