Pandas SettingWithCopyWarning only when copied outside of scope

Viewed 25

Why does doing df.copy() not give a warning only if done inside the function scope, but errors if used outside of the scope?

Example 1 - Gives Warning:

def do_something(df):
    df = df.loc[df['x'].isin([1, 2, 2698])]
    df.loc[:, "y"] = 0
    return df

df = pd.DataFrame({"x": [1, 2, 4], "y": ["a", "b", "c"]})
do_something(df)

Example 2 - Does not give warning

def do_something(df):
    df = df.copy()  # <---------------------- This fixes the warning but is not desirable as it creates a copy (which can take a lot of memory)
    df = df.loc[df['x'].isin([1, 2, 2698])]
    df.loc[:, "y"] = 0
    return df

df = pd.DataFrame({"x": [1, 2, 4], "y": ["a", "b", "c"]})
do_something(df)
1 Answers

Solution - change the function to use df.loc[:] = instead of df =

def do_something(df):
    # df = df.loc[df['x'].isin([1, 2, 2698])] # <------ Replace this with the line below
    df.loc[:] = df.loc[df['x'].isin([1, 2, 2698])]
    #  ^^^^^^ <--- add this
    df.loc[:, "y"] = 0
    return df
Related