I would like to insert a column into an existing DataFrame. Ideally without copying existing data. Whatever I try, later assignments to the resulting DataFrame generate a SettingWithCopyWarning, if the inserted data contains null values.
import pandas as pd
df = pd.DataFrame(data={'a': [1]})
df = df.assign(b=pd.Series(data=pd.NaT, index=df.index))
df['a'].iloc[0] = 5
Replacing assign
with either of
df['b'] = pd.Series(data=pd.NaT, index=df.index)
df.insert(column='b', loc=0, value=pd.NaT)
results in the same warning.
Strange enough, if the inserted value is not null (replacing pd.NaT with, e.g., 0) doesn't generate a warning. Is that a bug?
Your issue seems to be with df['a'].iloc[0] = 5
, where you're using chained assignment. Try this instead:
df.at[0, 'a'] = 5
# Or: df.loc[0, 'a'] = 5, but `.at` is preferred when assigning scalar