@@ -516,7 +516,7 @@ def _per_period_risk(
516516 """Groups per date risk metric to periods."""
517517
518518 ## I'm thinking this does not work with RPs... As you can't just sum impacts
519- ## Not sure what to do with it.
519+ ## Not sure what to do with it. -> Fixed I take the avg RP impact of the period
520520
521521 def identify_continuous_periods (group , time_unit ):
522522 # Calculate the difference between consecutive dates
@@ -532,6 +532,12 @@ def identify_continuous_periods(group, time_unit):
532532 group ["period_id" ] = (group ["date_diff" ] != 1 ).cumsum ()
533533 return group
534534
535+ def conditional_agg (group ):
536+ if "rp" in group .name [2 ]:
537+ return group .mean ()
538+ else :
539+ return group .sum ()
540+
535541 grouper = cls ._grouper
536542 if "group" in df .columns and "group" not in grouper :
537543 grouper = ["group" ] + grouper
@@ -549,21 +555,30 @@ def identify_continuous_periods(group, time_unit):
549555 "start_date" : pd .NamedAgg (column = "date" , aggfunc = "min" ),
550556 "end_date" : pd .NamedAgg (column = "date" , aggfunc = "max" ),
551557 }
552- for col in colname :
553- agg_dict [col ] = pd .NamedAgg (column = col , aggfunc = "sum" )
554- # Group by the identified periods and calculate start and end dates
555- df_periods = (
558+
559+ df_periods_dates = (
556560 df_periods .groupby (grouper + ["period_id" ], dropna = False , observed = True )
557561 .agg (** agg_dict )
558562 .reset_index ()
559563 )
560564
561- df_periods ["period" ] = (
562- df_periods ["start_date" ].astype (str )
565+ df_periods_dates ["period" ] = (
566+ df_periods_dates ["start_date" ].astype (str )
563567 + " to "
564- + df_periods ["end_date" ].astype (str )
568+ + df_periods_dates ["end_date" ].astype (str )
569+ )
570+
571+ df_periods = (
572+ df_periods .groupby (grouper + ["period_id" ], dropna = False , observed = True )[
573+ colname
574+ ]
575+ .apply (lambda group : conditional_agg (group ))
576+ .reset_index ()
577+ )
578+ df_periods = pd .merge (
579+ df_periods_dates [grouper + ["period" ]], df_periods , on = grouper
565580 )
566- df_periods = df_periods .drop (["period_id" , "start_date" , "end_date" ], axis = 1 )
581+ # df_periods = df_periods.drop(["period_id", "start_date", "end_date"], axis=1)
567582 return df_periods [
568583 ["period" ] + [col for col in df_periods .columns if col != "period" ]
569584 ]
0 commit comments