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)