code: def aggregate_returns(returns, convert_to):
      """
      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
      """
  
      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 == QUARTERLY:
          grouping = [
              lambda x: x.year,
              lambda x: int(math.ceil(x.month / 3.0)),
          ]
      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)
  
imports: from __future__ import division
  import math
  import pandas
  import numpy
  from math import pow
  from scipy import stats, optimize
  from six import iteritems
  from sys import float_info
  from .utils import nanmean, nanstd, nanmin, up, down, roll, rolling_window
  from .periods import ANNUALIZATION_FACTORS, APPROX_BDAYS_PER_YEAR
  from .periods import DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY
label_desc: Use image node_id 9 for calling a global function with example usage: aggregate_returns(returns, convert_to) and returns: returns, cum_returns