nodes: 0, module; 1, function_definition; 2, function_name:previous_event_indexer; 3, parameters; 4, block; 5, identifier:data_query_cutoff_times; 6, identifier:all_sids; 7, identifier:event_dates; 8, identifier:event_timestamps; 9, identifier:event_sids; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, expression_statement; 16, comment:# Walk backwards through the events, writing the index of the event into; 17, comment:# slots ranging from max(event_date, event_timestamp) to the start of the; 18, comment:# previously-written event.  This depends for correctness on the fact that; 19, comment:# event_dates is sorted in ascending order, because we need to have written; 20, comment:# later events so we know where to stop forward-filling earlier events.; 21, expression_statement; 22, for_statement; 23, return_statement; 24, comment:"""
      Construct an index array that, when applied to an array of values, produces
      a 2D array containing the values associated with the previous event for
      each sid at each moment in time.
  
      Locations where no previous event was known will be filled with -1.
  
      Parameters
      ----------
      data_query_cutoff : pd.DatetimeIndex
          The boundaries for the given trading sessions.
      all_dates : ndarray[datetime64[ns], ndim=1]
          Row labels for the target output.
      all_sids : ndarray[int, ndim=1]
          Column labels for the target output.
      event_dates : ndarray[datetime64[ns], ndim=1]
          Dates on which each input events occurred/will occur.  ``event_dates``
          must be in sorted order, and may not contain any NaT values.
      event_timestamps : ndarray[datetime64[ns], ndim=1]
          Dates on which we learned about each input event.
      event_sids : ndarray[int, ndim=1]
          Sids assocated with each input event.
  
      Returns
      -------
      indexer : ndarray[int, ndim=2]
          An array of shape (len(all_dates), len(all_sids)) of indices into
          ``event_{dates,timestamps,sids}``.
      """; 25, call; 26, assignment; 27, assignment; 28, assignment; 29, assignment; 30, assignment; 31, identifier:i; 32, call; 33, block; 34, identifier:out; 35, identifier:validate_event_metadata; 36, argument_list; 37, identifier:out; 38, call; 39, identifier:eff_dts; 40, call; 41, identifier:sid_ixs; 42, call; 43, identifier:dt_ixs; 44, call; 45, identifier:last_written; 46, dictionary; 47, identifier:range; 48, argument_list; 49, expression_statement; 50, expression_statement; 51, expression_statement; 52, expression_statement; 53, identifier:event_dates; 54, identifier:event_timestamps; 55, identifier:event_sids; 56, attribute; 57, argument_list; 58, attribute; 59, argument_list; 60, attribute; 61, argument_list; 62, attribute; 63, argument_list; 64, binary_operator:len(event_dates) - 1; 65, unary_operator; 66, unary_operator; 67, assignment; 68, assignment; 69, assignment; 70, assignment; 71, identifier:np; 72, identifier:full; 73, tuple; 74, unary_operator; 75, keyword_argument; 76, identifier:np; 77, identifier:maximum; 78, identifier:event_dates; 79, identifier:event_timestamps; 80, identifier:all_sids; 81, identifier:searchsorted; 82, identifier:event_sids; 83, identifier:data_query_cutoff_times; 84, identifier:searchsorted; 85, identifier:eff_dts; 86, keyword_argument; 87, call; 88, integer:1; 89, integer:1; 90, integer:1; 91, identifier:sid_ix; 92, subscript; 93, identifier:dt_ix; 94, subscript; 95, subscript; 96, identifier:i; 97, subscript; 98, identifier:dt_ix; 99, call; 100, call; 101, integer:1; 102, identifier:dtype; 103, attribute; 104, identifier:side; 105, string; 106, identifier:len; 107, argument_list; 108, identifier:sid_ixs; 109, identifier:i; 110, identifier:dt_ixs; 111, identifier:i; 112, identifier:out; 113, slice; 114, identifier:sid_ix; 115, identifier:last_written; 116, identifier:sid_ix; 117, identifier:len; 118, argument_list; 119, identifier:len; 120, argument_list; 121, identifier:np; 122, identifier:int64; 123, string_content:right; 124, identifier:event_dates; 125, identifier:dt_ix; 126, call; 127, identifier:data_query_cutoff_times; 128, identifier:all_sids; 129, attribute; 130, argument_list; 131, identifier:last_written; 132, identifier:get; 133, identifier:sid_ix; 134, None
code: def previous_event_indexer(data_query_cutoff_times,
                             all_sids,
                             event_dates,
                             event_timestamps,
                             event_sids):
      """
      Construct an index array that, when applied to an array of values, produces
      a 2D array containing the values associated with the previous event for
      each sid at each moment in time.
  
      Locations where no previous event was known will be filled with -1.
  
      Parameters
      ----------
      data_query_cutoff : pd.DatetimeIndex
          The boundaries for the given trading sessions.
      all_dates : ndarray[datetime64[ns], ndim=1]
          Row labels for the target output.
      all_sids : ndarray[int, ndim=1]
          Column labels for the target output.
      event_dates : ndarray[datetime64[ns], ndim=1]
          Dates on which each input events occurred/will occur.  ``event_dates``
          must be in sorted order, and may not contain any NaT values.
      event_timestamps : ndarray[datetime64[ns], ndim=1]
          Dates on which we learned about each input event.
      event_sids : ndarray[int, ndim=1]
          Sids assocated with each input event.
  
      Returns
      -------
      indexer : ndarray[int, ndim=2]
          An array of shape (len(all_dates), len(all_sids)) of indices into
          ``event_{dates,timestamps,sids}``.
      """
      validate_event_metadata(event_dates, event_timestamps, event_sids)
      out = np.full(
          (len(data_query_cutoff_times), len(all_sids)),
          -1,
          dtype=np.int64,
      )
  
      eff_dts = np.maximum(event_dates, event_timestamps)
      sid_ixs = all_sids.searchsorted(event_sids)
      dt_ixs = data_query_cutoff_times.searchsorted(eff_dts, side='right')
  
      # Walk backwards through the events, writing the index of the event into
      # slots ranging from max(event_date, event_timestamp) to the start of the
      # previously-written event.  This depends for correctness on the fact that
      # event_dates is sorted in ascending order, because we need to have written
      # later events so we know where to stop forward-filling earlier events.
      last_written = {}
      for i in range(len(event_dates) - 1, -1, -1):
          sid_ix = sid_ixs[i]
          dt_ix = dt_ixs[i]
          out[dt_ix:last_written.get(sid_ix, None), sid_ix] = i
          last_written[sid_ix] = dt_ix
      return out