nodes: 0, module; 1, function_definition; 2, function_name:minute_index_to_session_labels; 3, parameters; 4, block; 5, identifier:self; 6, identifier:index; 7, expression_statement; 8, if_statement; 9, comment:# Find the indices of the previous open and the next close for each; 10, comment:# minute.; 11, expression_statement; 12, expression_statement; 13, comment:# If they don't match, the minute is outside the trading day. Barf.; 14, expression_statement; 15, if_statement; 16, return_statement; 17, comment:"""
          Given a sorted DatetimeIndex of market minutes, return a
          DatetimeIndex of the corresponding session labels.
  
          Parameters
          ----------
          index: pd.DatetimeIndex or pd.Series
              The ordered list of market minutes we want session labels for.
  
          Returns
          -------
          pd.DatetimeIndex (UTC)
              The list of session labels corresponding to the given minutes.
          """; 18, not_operator; 19, block; 20, assignment; 21, assignment; 22, assignment; 23, call; 24, comment:# Show the first bad minute in the error message.; 25, block; 26, subscript; 27, attribute; 28, raise_statement; 29, identifier:prev_opens; 30, parenthesized_expression; 31, identifier:next_closes; 32, parenthesized_expression; 33, identifier:mismatches; 34, parenthesized_expression; 35, attribute; 36, argument_list; 37, expression_statement; 38, expression_statement; 39, expression_statement; 40, expression_statement; 41, expression_statement; 42, raise_statement; 43, attribute; 44, identifier:prev_opens; 45, identifier:index; 46, identifier:is_monotonic_increasing; 47, call; 48, binary_operator:self._opens.values.searchsorted(index.values, side='right') - 1; 49, call; 50, comparison_operator:prev_opens != next_closes; 51, identifier:mismatches; 52, identifier:any; 53, assignment; 54, assignment; 55, assignment; 56, assignment; 57, assignment; 58, call; 59, attribute; 60, identifier:index; 61, identifier:ValueError; 62, argument_list; 63, call; 64, integer:1; 65, attribute; 66, argument_list; 67, identifier:prev_opens; 68, identifier:next_closes; 69, identifier:bad_ix; 70, subscript; 71, identifier:example; 72, subscript; 73, identifier:prev_day; 74, subscript; 75, pattern_list; 76, subscript; 77, pattern_list; 78, subscript; 79, identifier:ValueError; 80, argument_list; 81, identifier:self; 82, identifier:schedule; 83, string:"Non-ordered index passed to minute_index_to_session_labels."; 84, attribute; 85, argument_list; 86, attribute; 87, identifier:searchsorted; 88, attribute; 89, keyword_argument; 90, call; 91, integer:0; 92, identifier:index; 93, identifier:bad_ix; 94, identifier:prev_opens; 95, identifier:bad_ix; 96, identifier:prev_open; 97, identifier:prev_close; 98, attribute; 99, identifier:prev_day; 100, identifier:next_open; 101, identifier:next_close; 102, attribute; 103, binary_operator:prev_day + 1; 104, call; 105, attribute; 106, identifier:searchsorted; 107, attribute; 108, keyword_argument; 109, attribute; 110, identifier:values; 111, identifier:index; 112, identifier:values; 113, identifier:side; 114, string; 115, attribute; 116, argument_list; 117, attribute; 118, identifier:iloc; 119, attribute; 120, identifier:iloc; 121, identifier:prev_day; 122, integer:1; 123, attribute; 124, argument_list; 125, attribute; 126, identifier:values; 127, identifier:index; 128, identifier:values; 129, identifier:side; 130, string; 131, identifier:self; 132, identifier:_closes; 133, string_content:left; 134, identifier:np; 135, identifier:flatnonzero; 136, identifier:mismatches; 137, identifier:self; 138, identifier:schedule; 139, identifier:self; 140, identifier:schedule; 141, concatenated_string; 142, identifier:format; 143, keyword_argument; 144, keyword_argument; 145, keyword_argument; 146, keyword_argument; 147, keyword_argument; 148, keyword_argument; 149, identifier:self; 150, identifier:_opens; 151, string_content:right; 152, string:"{num} non-market minutes in minute_index_to_session_labels:\n"; 153, string:"First Bad Minute: {first_bad}\n"; 154, string:"Previous Session: {prev_open} -> {prev_close}\n"; 155, string:"Next Session: {next_open} -> {next_close}"; 156, identifier:num; 157, call; 158, identifier:first_bad; 159, identifier:example; 160, identifier:prev_open; 161, identifier:prev_open; 162, identifier:prev_close; 163, identifier:prev_close; 164, identifier:next_open; 165, identifier:next_open; 166, identifier:next_close; 167, identifier:next_close; 168, attribute; 169, argument_list; 170, identifier:mismatches; 171, identifier:sum
code: def minute_index_to_session_labels(self, index):
          """
          Given a sorted DatetimeIndex of market minutes, return a
          DatetimeIndex of the corresponding session labels.
  
          Parameters
          ----------
          index: pd.DatetimeIndex or pd.Series
              The ordered list of market minutes we want session labels for.
  
          Returns
          -------
          pd.DatetimeIndex (UTC)
              The list of session labels corresponding to the given minutes.
          """
          if not index.is_monotonic_increasing:
              raise ValueError(
                  "Non-ordered index passed to minute_index_to_session_labels."
              )
  
          # Find the indices of the previous open and the next close for each
          # minute.
          prev_opens = (
              self._opens.values.searchsorted(index.values, side='right') - 1
          )
          next_closes = (
              self._closes.values.searchsorted(index.values, side='left')
          )
  
          # If they don't match, the minute is outside the trading day. Barf.
          mismatches = (prev_opens != next_closes)
          if mismatches.any():
              # Show the first bad minute in the error message.
              bad_ix = np.flatnonzero(mismatches)[0]
              example = index[bad_ix]
  
              prev_day = prev_opens[bad_ix]
              prev_open, prev_close = self.schedule.iloc[prev_day]
              next_open, next_close = self.schedule.iloc[prev_day + 1]
  
              raise ValueError(
                  "{num} non-market minutes in minute_index_to_session_labels:\n"
                  "First Bad Minute: {first_bad}\n"
                  "Previous Session: {prev_open} -> {prev_close}\n"
                  "Next Session: {next_open} -> {next_close}"
                  .format(
                      num=mismatches.sum(),
                      first_bad=example,
                      prev_open=prev_open, prev_close=prev_close,
                      next_open=next_open, next_close=next_close)
              )
  
          return self.schedule.index[prev_opens]