Skip to content

Commit

Permalink
suppress the SettingWithCopyWarning of pandas (#1513)
Browse files Browse the repository at this point in the history
* df value is set as expected, suppress the warning;

* depress warning with pandas option_context

---------

Co-authored-by: Cadenza-Li <362237642@qq.com>
  • Loading branch information
Fivele-Li and Cadenza-Li committed Sep 1, 2023
1 parent 4c30e58 commit 69e28ce
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions qlib/data/dataset/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,13 @@ def __call__(self, df):
# try not modify original dataframe
if not isinstance(self.fields_group, list):
self.fields_group = [self.fields_group]
for g in self.fields_group:
cols = get_group_columns(df, g)
df[cols] = df[cols].groupby("datetime", group_keys=False).apply(self.zscore_func)
# depress warning by references:
# https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas
# https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html#getting-and-setting-options
with pd.option_context("mode.chained_assignment", None):
for g in self.fields_group:
cols = get_group_columns(df, g)
df[cols] = df[cols].groupby("datetime", group_keys=False).apply(self.zscore_func)
return df


Expand Down

0 comments on commit 69e28ce

Please sign in to comment.