Skip to content

Commit

Permalink
avoid SettingWithCopyWarning
Browse files Browse the repository at this point in the history
  • Loading branch information
bl-young committed Nov 15, 2023
1 parent e6230c1 commit f28ac14
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions esupy/dqi.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ def get_weighted_average(df, data_col, weight_col, agg_cols):
df_agg[data_col] = get_weighted_average(df, data_col,
weight_col, agg_cols)
"""
df.loc[:, '_data_times_weight'] = df[data_col] * df[weight_col]
df.loc[:, '_weight_where_notnull'] = df[weight_col] * pd.notnull(df[data_col])
calc_cols = ['_weight_where_notnull', '_data_times_weight']
df[calc_cols] = df[calc_cols].applymap(float)
df = (df.assign(_data_times_weight = lambda x: x[data_col] * x[weight_col])
.assign(_weight_where_notnull = lambda x:
x[weight_col] * pd.notnull(x[data_col]))
)
g = df.groupby(agg_cols)
wt_avg = np.divide(g['_data_times_weight'].sum(), g['_weight_where_notnull'].sum(),
out=np.zeros_like(g['_data_times_weight'].sum()),
Expand Down

0 comments on commit f28ac14

Please sign in to comment.