Background:
Pandas versions 0.13 and higher introduce the SettingWithCopyWarning to highlight potential confusion caused by "chained" assignments. This warning aims to address scenarios where a copy of a DataFrame is modified, leading to unexpected results.
What Does the Warning Mean?
The warning indicates that a value is being set on a copy of a slice from a DataFrame. Specifically, it suggests replacing the following line:
quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
with:
quote_df.loc[row_index, 'TVol'] = value
How to Fix the Warning:
The solution provided by the warning is to use loc indexing instead. However, this may not be suitable for all use cases. If you do not care about updating the original DataFrame, you can safely disable the warning with:
import pandas as pd pd.options.mode.chained_assignment = None # default='warn'
Other Explanation:
The SettingWithCopyWarning flags chained assignments that do not always work as expected. The issue arises when the first selection returns a copy of the DataFrame, and subsequent assignments are made to that copy. This pattern can lead to confusion because the changes are not reflected in the original DataFrame.
In your case, the following code is causing the warning:
quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
Since quote_df['TVol'] is a copy, the assignment will not modify the original DataFrame. Instead, consider using:
quote_df = quote_df[quote_df['TVol'] > 0] quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE
Additional Resources:
The above is the detailed content of How to Understand and Resolve Pandas' SettingWithCopyWarning?. For more information, please follow other related articles on the PHP Chinese website!