I have been struggling with the SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame, although I have specifically changed my code to avoid it.
def df_creation(df,per):
# Sorting the categories
df_cat_3 = df.loc[:,('CAT_1','CAT_2', 'CAT_3')].copy()
df_cat_3.drop_duplicates(inplace=True)
df_cat_2 = df.loc[:,('CAT_1','CAT_2')].copy()
df_cat_2.drop_duplicates(inplace=True)
df_cat_2['CAT_3']='X'
df_cat_1=df.loc[:,('CAT_1')].copy()
df_cat_1.drop_duplicates(inplace=True)
df_cat_1['CAT_2']='X'
df_cat_1['CAT_3']='X'
df_cat_1
df_categories = pd.concat([df_cat_1,df_cat_2,df_cat_3], ignore_index=True).sort_values(by=['CAT_1', 'CAT_2', 'CAT_3'])
# Adding the dates
if per =='period':
dates=df[['KEY_FINANCIAL_PERIOD']]
elif per == 'weeks':
dates=df[['KEY_WEEK']]
elif per=='current':
dates=[]
dates.drop_duplicates(inplace=True)
df_outputs = pd.merge(dates, df_categories, how='cross')
return df_outputs
Running this function with my df gets me the warning :(