code: def backshift_returns_series(series, N):
      """Shift a multi-indexed series backwards by N observations in
      the first level.
  
      This can be used to convert backward-looking returns into a
      forward-returns series.
      """
      ix = series.index
      dates, sids = ix.levels
      date_labels, sid_labels = map(np.array, ix.labels)
  
      # Output date labels will contain the all but the last N dates.
      new_dates = dates[:-N]
  
      # Output data will remove the first M rows, where M is the index of the
      # last record with one of the first N dates.
      cutoff = date_labels.searchsorted(N)
      new_date_labels = date_labels[cutoff:] - N
      new_sid_labels = sid_labels[cutoff:]
      new_values = series.values[cutoff:]
  
      assert new_date_labels[0] == 0
  
      new_index = pd.MultiIndex(
          levels=[new_dates, sids],
          labels=[new_date_labels, new_sid_labels],
          sortorder=1,
          names=ix.names,
      )
  
      return pd.Series(data=new_values, index=new_index)
  
imports: import pandas
  import numpy
  import re
  import warnings
  from IPython.display import display
  from pandas.tseries.offsets import CustomBusinessDay, Day, BusinessDay
  from scipy.stats import mode
label_desc: Use image node_id 6 for calling a global function with example usage: backshift_returns_series(series, N) and returns: pd