SettingWithCopyWarning signals that an assignment may target a view or copied subset instead of the intended pandas DataFrame. The assignment might work, but pandas cannot guarantee that the change will affect the original object. section 2.26
A chained assignment
This temperature correction first selects rows and then selects the column: df[df['temp'] < 0]['temp'] = 0. Because pandas may cache the intermediate result, the assignment may not change df. section 2.26
df.loc[
df['temp'] < 0, #1
'temp' #2
] = 0
Use .loc with one row selector and one column selector to express the assignment directly on the DataFrame. section 2.26
Use two complementary safeguards:
- Prefer df.loc[boolean_index, 'column'] = value instead of chained indexing such as ][. section 2.26
- After operations such as dropna that return a new object, call .copy() before modifying the result. section 2.20 section 3.10
Cleaning imported taxi data
After removing missing rows, make ownership explicit before changing dtypes: df = df.dropna().copy(). Then convert passenger_count and payment_type with astype(np.int8). Without copy(), later changes may not stick. section 3.10
