nodes: 0, module; 1, function_definition; 2, function_name:square_off; 3, parameters; 4, block; 5, identifier:series; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, if_statement; 10, return_statement; 11, identifier:time_delta; 12, None; 13, identifier:transition_seconds; 14, integer:1; 15, comment:"""Insert samples in regularly sampled data to produce stairsteps from ramps when plotted.
  
      New samples are 1 second (1e9 ns) before each existing samples, to facilitate plotting and sorting
  
      >>> square_off(pd.Series(range(3), index=pd.date_range('2014-01-01', periods=3, freq='15m')),
      ...            time_delta=5.5)  # doctest: +NORMALIZE_WHITESPACE
      2014-01-31 00:00:00           0
      2014-01-31 00:00:05.500000    0
      2015-04-30 00:00:00           1
      2015-04-30 00:00:05.500000    1
      2016-07-31 00:00:00           2
      2016-07-31 00:00:05.500000    2
      dtype: int64
      >>> square_off(pd.Series(range(2), index=pd.date_range('2014-01-01', periods=2, freq='15min')),
      ...            transition_seconds=2.5)  # doctest: +NORMALIZE_WHITESPACE
      2014-01-01 00:00:00           0
      2014-01-01 00:14:57.500000    0
      2014-01-01 00:15:00           1
      2014-01-01 00:29:57.500000    1
      dtype: int64
      """; 16, identifier:time_delta; 17, comment:# int, float means delta is in seconds (not years!); 18, block; 19, else_clause; 20, call; 21, if_statement; 22, expression_statement; 23, block; 24, attribute; 25, argument_list; 26, call; 27, block; 28, assignment; 29, expression_statement; 30, expression_statement; 31, expression_statement; 32, expression_statement; 33, call; 34, identifier:sort_index; 35, identifier:isinstance; 36, argument_list; 37, expression_statement; 38, identifier:new_times; 39, binary_operator:series.index + time_delta; 40, assignment; 41, assignment; 42, assignment; 43, assignment; 44, attribute; 45, argument_list; 46, identifier:time_delta; 47, tuple; 48, assignment; 49, attribute; 50, identifier:time_delta; 51, identifier:diff; 52, call; 53, identifier:time_delta; 54, call; 55, identifier:new_times; 56, binary_operator:series.index + time_delta; 57, identifier:new_times; 58, binary_operator:pd.DatetimeIndex(new_times) - datetime.timedelta(0, transition_seconds); 59, identifier:pd; 60, identifier:concat; 61, list; 62, identifier:int; 63, identifier:float; 64, identifier:time_delta; 65, call; 66, identifier:series; 67, identifier:index; 68, attribute; 69, argument_list; 70, attribute; 71, argument_list; 72, attribute; 73, identifier:time_delta; 74, call; 75, call; 76, identifier:series; 77, call; 78, attribute; 79, argument_list; 80, identifier:np; 81, identifier:diff; 82, attribute; 83, identifier:np; 84, identifier:append; 85, identifier:diff; 86, list; 87, identifier:series; 88, identifier:index; 89, attribute; 90, argument_list; 91, attribute; 92, argument_list; 93, attribute; 94, argument_list; 95, identifier:datetime; 96, identifier:timedelta; 97, integer:0; 98, identifier:time_delta; 99, identifier:series; 100, identifier:index; 101, subscript; 102, identifier:pd; 103, identifier:DatetimeIndex; 104, identifier:new_times; 105, identifier:datetime; 106, identifier:timedelta; 107, integer:0; 108, identifier:transition_seconds; 109, identifier:pd; 110, identifier:Series; 111, attribute; 112, keyword_argument; 113, identifier:diff; 114, unary_operator; 115, identifier:series; 116, identifier:values; 117, identifier:index; 118, identifier:new_times; 119, integer:1
code: def square_off(series, time_delta=None, transition_seconds=1):
      """Insert samples in regularly sampled data to produce stairsteps from ramps when plotted.
  
      New samples are 1 second (1e9 ns) before each existing samples, to facilitate plotting and sorting
  
      >>> square_off(pd.Series(range(3), index=pd.date_range('2014-01-01', periods=3, freq='15m')),
      ...            time_delta=5.5)  # doctest: +NORMALIZE_WHITESPACE
      2014-01-31 00:00:00           0
      2014-01-31 00:00:05.500000    0
      2015-04-30 00:00:00           1
      2015-04-30 00:00:05.500000    1
      2016-07-31 00:00:00           2
      2016-07-31 00:00:05.500000    2
      dtype: int64
      >>> square_off(pd.Series(range(2), index=pd.date_range('2014-01-01', periods=2, freq='15min')),
      ...            transition_seconds=2.5)  # doctest: +NORMALIZE_WHITESPACE
      2014-01-01 00:00:00           0
      2014-01-01 00:14:57.500000    0
      2014-01-01 00:15:00           1
      2014-01-01 00:29:57.500000    1
      dtype: int64
      """
      if time_delta:
          # int, float means delta is in seconds (not years!)
          if isinstance(time_delta, (int, float)):
              time_delta = datetime.timedelta(0, time_delta)
          new_times = series.index + time_delta
      else:
          diff = np.diff(series.index)
          time_delta = np.append(diff, [diff[-1]])
          new_times = series.index + time_delta
          new_times = pd.DatetimeIndex(new_times) - datetime.timedelta(0, transition_seconds)
      return pd.concat([series, pd.Series(series.values, index=new_times)]).sort_index()