famafrench.utils.grouped_vwAvg

famafrench.utils.grouped_vwAvg(df0, col_values, col_weights, \*groupby_args, \*\*groupby_kwargs)[source]

Calculate (net) weighted portfolio return for portfolio with weights col_weights within a group or groups.

Parameters
  • df0 (pandas.DataFrame) – Dataset containing firm-level stock returns and portfolio weights, both indexed by dates and firm identifiers when required.

  • col_values (list, str) – Column(s) to average over.

  • col_weights (str) – Column containing the portfolio weights.

  • group_args (list, str, [optional]) – args to pass into groupby (ie the level to group on).

  • group_kwargs (list, str, [optional]) – kwargs to pass into groupby.

Returns

df1 – Original dataset augmented w/ value-weighted returns.

Return type

pandas.Series, or pandas.DataFrame

Note

Function is FASTER than pandas.core.groupby.GroupBy.apply() since it avoids non-optimized aggregation.