otp.Operation.str.substr#

substr(start, n_bytes=None, rtrim=False)#

Return n_bytes characters starting from start. For a positive start return num_bytes of the string, starting from the position specified by start (0-based). For a negative start, the position is counted from the end of the string (when specifying a negative index, please take into account that a field may have whitespace at the end, which you could remove by trim()). If the n_bytes parameter is omitted, returns the part of the input string starting at start.

Parameters
  • start (int or Column or Operation) – Index of first symbol in substring

  • n_bytes (int or Column or Operation) – Number of bytes in substring

  • rtrim (bool) – If the rtrim flag is set to True, original string will be rtrimmed before calling substr, this can be useful with negative start index.

Returns

Substring of string (n_bytes length starting with start).

Return type

Operation

Examples

>>> data = otp.Ticks(X=["abcdef", "12345"], START_INDEX=[2, 1], N=[2, 3])
>>> data["FIRST_3"] = data["X"].str.substr(0, 3)
>>> data["LAST_3"] = data["X"].str.substr(-3, 3, rtrim=True)
>>> data["CENTER"] = data["X"].str.substr(data["START_INDEX"], data["N"])
>>> otp.run(data)[["FIRST_3", "LAST_3", "CENTER"]]
  FIRST_3 LAST_3 CENTER
0     abc    def     cd
1     123    345    234