Search code examples
pythonpython-3.xpandasdataframechained-assignment

Confusion about pandas copy of slice of dataframe warning


I've looked through a bunch of questions and answers related to this issue, but I'm still finding that I'm getting this copy of slice warning in places where I don't expect it. Also, it's cropping up in code that was running fine for me previously, leading me to wonder if some sort of update may be the culprit.

For example, this is a set of code where all I'm doing is reading in an Excel file into a pandas DataFrame, and cutting down the set of columns included with the df[[]] syntax.

df = pd.read_excel(filepath)
df1 = df[['Gender','Age','Date to Delivery','Date to insert']]

Now, any further changes I make to this df1 file raise the copy of slice warning. For example, the following code

df1['Age'] = df1.Age.fillna(0)
df1['Age'] = df1.Age.astype(int)

raises the following warning

/Users/samlilienfeld/anaconda/lib/python3.5/site-packages/ipykernel/__main__.py:2: SettingWithCopyWarning:   
A value is trying to be set on a copy of a slice from a DataFrame.   
Try using .loc[row_indexer,col_indexer] = value instead

I'm confused because I thought the df[[]] column subsetting returned a copy by default. The only way I've found to suppress the errors is by explicitly adding df[[]].copy(). I could have sworn that in the past I did not have to do that and did not raise the copy of slice error.

Similarly, I have some other code that runs a function on a dataframe to filter it in certain ways:

def lim(df):
    if (geography == "All"):
        df1 = df
    else:
        df1 = df[df.center_JO == geography]
    df_date = df1[(df1.date >= start) & (df1.date <= end)]
    return df_date

df_lim = lim(df)

From this point forward, any changes I make to any of the values of df_lim raise the copy of slice error. The only way around it that I've found is to change the function call to:

df_lim = lim(df).copy()

This just seems wrong to me. What am I missing? It seems like these use cases should return copies by default, and I could have sworn that the last time I ran these scripts I was not running into these errors.
Do I just need to start adding .copy() all over the place? Seems like there should be a cleaner way to do this.


Solution

  •  izmir = pd.read_excel(filepath)
     izmir_lim = izmir[['Gender','Age','MC_OLD_M>=60','MC_OLD_F>=60',
                        'MC_OLD_M>18','MC_OLD_F>18','MC_OLD_18>M>5',
                        'MC_OLD_18>F>5','MC_OLD_M_Child<5','MC_OLD_F_Child<5',
                        'MC_OLD_M>0<=1','MC_OLD_F>0<=1','Date to Delivery',
                        'Date to insert','Date of Entery']]
    

    izmir_lim is a view/copy of izmir. You subsequently attempt to assign to it. This is what is throwing the error. Use this instead:

     izmir_lim = izmir[['Gender','Age','MC_OLD_M>=60','MC_OLD_F>=60',
                        'MC_OLD_M>18','MC_OLD_F>18','MC_OLD_18>M>5',
                        'MC_OLD_18>F>5','MC_OLD_M_Child<5','MC_OLD_F_Child<5',
                        'MC_OLD_M>0<=1','MC_OLD_F>0<=1','Date to Delivery',
                        'Date to insert','Date of Entery']].copy()
    

    Whenever you 'create' a new dataframe from another in the following fashion:

    new_df = old_df[list_of_columns_names]
    

    new_df will have a truthy value in it's is_copy attribute. When you attempt to assign to it, pandas throws the SettingWithCopyWarning.

    new_df.iloc[0, 0] = 1  # Should throw an error
    

    You can overcome this in several ways.

    Option #1

    new_df = old_df[list_of_columns_names].copy()
    

    Option #2 (as @ayhan suggested in comments)

    new_df = old_df[list_of_columns_names]
    new_df.is_copy = None
    

    Option #3

    new_df = old_df.loc[:, list_of_columns_names]