The SettingWithCopyWarning, introduced in Pandas 0.13.0rc1, alerts users to potential issues when setting values on slices or copies of DataFrames. This warning aims to prevent confusion and unexpected behavior.
The warning arises due to "chained assignments," where a value is set on a slice or copy of a DataFrame, such as:
df[df['A'] > 2]['B'] = new_val # new_val not set in df
In this example, the first selection (df[df['A'] > 2]) returns a copy, and attempting to set 'B' on this copy won't update the original DataFrame 'df'. To correctly update 'df', use the loc accessor:
df.loc[df['A'] > 2, 'B'] = new_val
If you are certain that you don't need the changes to be reflected back to the original DataFrame, you can disable the warning:
import pandas as pd pd.options.mode.chained_assignment = None # default='warn'
For assignments on slices or copies, use the loc or iloc accessors, which allow direct modification of the original DataFrame:
df.loc[df['A'] > 2, 'B'] = new_val df.iloc[2:5, 4] = [1, 8, 8]
If you need a new reference for your DataFrame, create a copy using DataFrame.copy() before modifying it:
new_df = df.copy() new_df['C'] = df['A'] + df['B']
In your specific case, instead of modifying the original quote_df, you could regenerate a new DataFrame with the desired changes:
quote_df = pd.read_csv(StringIO(str_of_all), sep=',', names=list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg')) quote_df.rename(columns={'A':'STK', 'B':'TOpen', ...}, inplace=True) quote_df['TVol'] = quote_df['TVol'] / TVOL_SCALE quote_df['TAmt'] = quote_df['TAmt'] / TAMT_SCALE quote_df['TDate'] = quote_df.TDate.map(lambda x: x[0:4]+x[5:7]+x[8:10])
The above is the detailed content of How to Understand and Handle Pandas' SettingWithCopyWarning?. For more information, please follow other related articles on the PHP Chinese website!