code: def _calculate_label_rank(df: pd.DataFrame) -> pd.DataFrame:
      """calculate label rank
  
      :param df:
      :return:
      """
      _label_name = "label"
  
      def _calculate_day_value(g_df: pd.DataFrame):
          g_df = g_df.copy()
          g_df["rank_ratio"] = (
              g_df[_label_name].rank(ascending=False) / len(g_df) * 100
          )
  
          # Sell: -1, Hold: 0, Buy: 1
          for i in [-1, 0, 1]:
              g_df.loc[g_df["status"] == i, "rank_label_mean"] = g_df[
                  g_df["status"] == i
              ]["rank_ratio"].mean()
  
          g_df["excess_return"] = (
              g_df[_label_name] - g_df[_label_name].mean()
          )
          return g_df
  
      return df.groupby(level="datetime").apply(_calculate_day_value)
  
imports: import pandas
  from ....backtest.profit_attribution import get_stock_weight_df
label_desc: Use image node_id 4 for calling a global function with example usage: _calculate_label_rank(df) and returns: df, g_df