I believe your df
is a (not-properly-done) copy of another pandas.DataFrame
.
Following reproduces the SettingWithCopyWarning
.
import pandas as pd
raw = pd.DataFrame({"a": [1,2], "b": [2,3], "c": [3,4]})
df = raw[["a", "b"]]
df.drop(["a"], 1, inplace = True)
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
errors=errors)
When you make another pandas.DataFrame
from an existing one, never do direct assignment like df = raw[["a", "b"]]
. Instead, use pandas.DataFrame.copy()
.
raw = pd.DataFrame({"a": [1,2], "b": [2,3], "c": [3,4]})
df = raw[["a", "b"]].copy()
df.drop(["a"], 1, inplace = True)
Now the warning disappears, as df
is a new object created, and thus according to the official document:
Modifications to the data or indices of the copy will not be reflected in the original object