zeratul314
zeratul314

Reputation: 123

SettingWithCopyWarning even when using .loc

Why am i getting SettingWithCopyWarning in this code? I know that when defining a new Dataframe, i need to use .copy(), but that doesn't help.

   def calc_quantity(input_df):
        when = input_df['SecurityId'].str.endswith(
            'TMS') | input_df['SecurityId'].str.endswith('TDS')
        input_df['Quantity'] = input_df['Quantity'].fillna(0).astype(float)
        input_df['Quantity'].loc[when] *= 100
            'TMS') | input_df['SecurityId'].str.endswith('TDS').copy()
        input_df['Quantity'] = input_df['Quantity'].fillna(0).astype(float).copy()
        input_df['Quantity'].loc[when] = input_df['Quantity'].loc[when]*100
        return input_df['Quantity']

Upvotes: 1

Views: 120

Answers (1)

greco
greco

Reputation: 325

The warning typically is a result of chained assignments and you can read more about it in this answer and this blog post also goes into great detail.

If you wish to turn it off, you can use:

pd.options.mode.chained_assignment = None

Upvotes: 1

Related Questions