code: def aggregate_returns(returns, convert_to):
      
  
      def cumulate_returns(x):
          return cum_returns(x).iloc[-1]
  
      if convert_to == WEEKLY:
          grouping = [lambda x: x.year, lambda x: x.isocalendar()[1]]
      elif convert_to == MONTHLY:
          grouping = [lambda x: x.year, lambda x: x.month]
      elif convert_to == YEARLY:
          grouping = [lambda x: x.year]
      else:
          raise ValueError(
              'convert_to must be {}, {} or {}'.format(WEEKLY, MONTHLY, YEARLY)
          )
  
      return returns.groupby(grouping).apply(cumulate_returns)
docs: Aggregates returns by week, month, or year.
  
      Parameters
      ----------
      returns : pd.Series
         Daily returns of the strategy, noncumulative.
          - See full explanation in :func:`~empyrical.stats.cum_returns`.
      convert_to : str
          Can be 'weekly', 'monthly', or 'yearly'.
  
      Returns
      -------
      aggregated_returns : pd.Series