While upgrading Pandas from 0.11 to 0.13.0rc1, users may encounter numerous SettingWithCopyWarning messages. One such example is:
E:\FinReporter\FM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
The SettingWithCopyWarning flags potentially confusing "chained" assignments like:
df[df['A'] > 2]['B'] = new_val # new_val not set in df
This doesn't always work as expected, especially when the first selection returns a copy. To resolve this issue, use:
df.loc[df['A'] > 2, 'B'] = new_val
In the provided code snippet:
quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
you're effectively performing a chained assignment equivalent to:
quote_df = quote_df[quote_df['A'] > 2] quote_df['TVol'] = new_val
This pattern cannot be distinguished from the negative example and triggers the warning.
If you're confident that the writes won't affect the original frame, disable the warning with:
import pandas as pd pd.options.mode.chained_assignment = None # default='warn'
For further understanding, refer to these resources:
The above is the detailed content of How Can I Effectively Handle Pandas' SettingWithCopyWarning?. For more information, please follow other related articles on the PHP Chinese website!