Skip to main content
Version: 0.9.0

tabsdata.tableframe.expr.string

TdExprStringNameSpace
TdExprStringNameSpace.containsEvaluate if the string contains a pattern.
TdExprStringNameSpace.contains_anyEvaluate if the string contains any of the given patterns.
TdExprStringNameSpace.count_matchesCounts the ocurrrences of the given pattern in the string.
TdExprStringNameSpace.ends_withEvaluate if the string ends with.
TdExprStringNameSpace.extractExtract a pattern from the string.
TdExprStringNameSpace.findFind the position of the first occurrence of the given pattern.
TdExprStringNameSpace.headExtract the start of the string up to the given length.
TdExprStringNameSpace.len_bytesReturn number of bytes (not chars) of a string.
TdExprStringNameSpace.len_charsReturn number of chars (not bytes) of a string.
TdExprStringNameSpace.pad_endPad string values at the end to the given length using the given fill character.
TdExprStringNameSpace.pad_startPad string values at the front to the given length using the given fill character.
TdExprStringNameSpace.replaceReplace the first occurence of a pattern with the given string.
TdExprStringNameSpace.replace_allReplace the all occurences of a pattern with the given string.
TdExprStringNameSpace.replace_manyReplace the all occurences of any the given patterns with the given string.
TdExprStringNameSpace.reverseReverse the string.
TdExprStringNameSpace.sliceExtract the substring at the given offset for the given length.
TdExprStringNameSpace.starts_withEvaluate if the string start with.
TdExprStringNameSpace.strip_charsTrim string values.
TdExprStringNameSpace.strip_chars_endTrim string values from the end of the string.
TdExprStringNameSpace.strip_chars_startTrim string values from the start of the string.
TdExprStringNameSpace.strip_prefixTrim string values removing the given prefix
TdExprStringNameSpace.strip_suffixTrim string values removing the given suffix
TdExprStringNameSpace.tailExtract the end of the string up to the given length.
TdExprStringNameSpace.to_dateConvert the string to a date.
TdExprStringNameSpace.to_datetimeConvert the string to a datetime.
TdExprStringNameSpace.to_integerCovert a string to integer.
TdExprStringNameSpace.to_lowercaseReturn the lowercase of a string.
TdExprStringNameSpace.to_timeConvert the string to a time.
TdExprStringNameSpace.to_titlecaseUppercase the first character and lowercase all the others ones of a string.
TdExprStringNameSpace.to_uppercaseReturn the uppercase of a string.
TdExprStringNameSpace.zfillPad numeric string values at the start to the given length using zeros.
to_tdexpr

TdExprStringNameSpace

class TdExprStringNameSpace(expr: ExprStringNameSpace)

contains
def contains(
pattern: str | TdExpr,
literal: bool = False,
strict: bool = True,
) -> TdExpr

Evaluate if the string contains a pattern.

Parameters:

parameter
pattern

The pattern to search for.

parameter
literal

Take the pattern as a literal string (not a regex).

parameter
strict

if the given pattern is not valid regex, raise an error.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.contains("ab").alias("contains"))
>>>
┌──────┬──────────┐
│ a ┆ contains │
------
strbool
╞══════╪══════════╡
│ a ┆ false │
│ ab ┆ true │
│ b ┆ false │
│ xaby ┆ true │
│ null ┆ null │
└──────┴──────────┘

contains_any
def contains_any(patterns: IntoTdExpr, ascii_case_insensitive: bool = False) -> TdExpr

Evaluate if the string contains any of the given patterns.

Parameters:

parameter
patterns

The patterns to search for.

parameter
ascii_case_insensitive

If true, the search is case-insensitive.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.contains_any(["a", "b"]).alias("contains_any"))
>>>
┌──────┬──────────────┐
│ a ┆ contains_any │
------
strbool
╞══════╪══════════════╡
│ abc ┆ true │
│ axy ┆ true │
│ xyb ┆ true │
│ xyz ┆ false │
│ null ┆ null │
└──────┴──────────────┘

count_matches
def count_matches(pattern: str | TdExpr, literal: bool = False) -> TdExpr

Counts the ocurrrences of the given pattern in the string.

Parameters:

parameter
pattern

The pattern to extract.

parameter
literal

Take the pattern as a literal string (not a regex).

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.count_matches("b.").alias("count_matches"))
>>>
┌───────────┬───────────────┐
│ a ┆ count_matches │
------
str ┆ u32 │
╞═══════════╪═══════════════╡
│ a bAb c d ┆ 2
│ bCbb c d ┆ 2
│ bb ┆ 1
│ b ┆ 0
│ a ┆ 0
│ null ┆ null │
└───────────┴───────────────┘

ends_with
def ends_with(suffix: str | TdExpr) -> TdExpr

Evaluate if the string ends with.

Parameters:

parameter
suffix

The suffix to search for.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.ends_with("b").alias("ends_with"))
>>>
┌──────┬───────────┐
│ a ┆ ends_with │
------
strbool
╞══════╪═══════════╡
│ a ┆ false │
│ ab ┆ true │
│ b ┆ true │
│ xaby ┆ false │
│ null ┆ null │
└──────┴───────────┘

extract
def extract(pattern: IntoTdExprColumn, group_index: int = 1) -> TdExpr

Extract a pattern from the string.

Parameters:

parameter
pattern

The pattern to extract.

parameter
group_index

The group index to extract.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.extract("(b.b)", 1).alias("extract"))
>>>
┌───────────┬─────────┐
│ a ┆ extract │
------
strstr
╞═══════════╪═════════╡
│ a bAb c d ┆ bAb │
│ bCbb c d ┆ bCb │
│ bb ┆ null │
│ null ┆ null │
└───────────┴─────────┘

find
def find(pattern: str | TdExpr, literal: bool = False, strict: bool = True) -> TdExpr

Find the position of the first occurrence of the given pattern.

Parameters:

parameter
pattern

The pattern to search for.

parameter
literal

Take the pattern as a literal string (not a regex).

parameter
strict

if the given pattern is not valid regex, raise an error.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.find("b").alias("find"))
>>>
┌──────┬──────┐
│ a ┆ find │
------
str ┆ u32 │
╞══════╪══════╡
│ a ┆ null │
│ ab ┆ 1
│ b ┆ 0
│ xaby ┆ 2
│ null ┆ null │
└──────┴──────┘

head
def head(n: int | IntoTdExprColumn) -> TdExpr

Extract the start of the string up to the given length.

Parameters:

parameter
n

The length of the head.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.head(2).alias("head"))
>>>
┌──────┬──────┐
│ a ┆ head │
------
strstr
╞══════╪══════╡
│ abc ┆ ab │
│ a ┆ a │
│ null ┆ null │
└──────┴──────┘

len_bytes
def len_bytes() -> TdExpr

Return number of bytes (not chars) of a string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.len_bytes().alias("len_bytes"))
>>>
┌──────┬────────────┐
│ a ┆ to_decimal │
------
str ┆ u32 │
╞══════╪════════════╡
│ ab ┆ 2
│ 再 ┆ 3
│ null ┆ null │
└──────┴────────────┘

len_chars
def len_chars() -> TdExpr

Return number of chars (not bytes) of a string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.len_chars().alias("len_chars"))
>>>
┌──────┬────────────┐
│ a ┆ to_decimal │
------
str ┆ u32 │
╞══════╪════════════╡
│ ab ┆ 2
│ 再 ┆ 3
│ null ┆ null │
└──────┴────────────┘

pad_end
def pad_end(length: int, fill_char: str = ' ') -> TdExpr

Pad string values at the end to the given length using the given fill character.

Parameters:

parameter
length

The length to end pad the string to.

parameter
fill_char

The character to use for padding.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.pad_end(6, "-").alias("pad_end"))
>>>
┌────────┬─────────┐
│ a ┆ pad_end │
------
strstr
╞════════╪═════════╡
│ abc ┆ abc---
defdef
│ null ┆ null │
└────────┴─────────┘

pad_start
def pad_start(length: int, fill_char: str = ' ') -> TdExpr

Pad string values at the front to the given length using the given fill character.

Parameters:

parameter
length

The length to front pad the string to.

parameter
fill_char

The character to use for padding.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.pad_start(6, "-").alias("pad_start"))
>>>
┌────────┬───────────┐
│ a ┆ pad_start │
------
strstr
╞════════╪═══════════╡
│ abc ┆ ---abc │
defdef
│ null ┆ null │
└────────┴───────────┘

replace
def replace(
pattern: str | TdExpr,
value: str | TdExpr,
literal: bool = False,
n: int = 1,
) -> TdExpr

Replace the first occurence of a pattern with the given string.

Parameters:

parameter
pattern

The pattern to replace.

parameter
value

The value to replace the pattern with.

parameter
literal

Take the pattern as a literal string (not a regex).

parameter
n

Number of matches to replace.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.replace("b", "X").alias("replace"))
>>>
┌───────────┬───────────┐
│ a ┆ replace │
------
strstr
╞═══════════╪═══════════╡
│ a bAb c d ┆ a XAb c d │
│ bCbb c d ┆ XCbb c d │
│ bb ┆ Xb │
│ b ┆ X │
│ a ┆ a │
│ null ┆ null │
└───────────┴───────────┘

replace_all
def replace_all(
pattern: str | TdExpr,
value: str | TdExpr,
literal: bool = False,
) -> TdExpr

Replace the all occurences of a pattern with the given string.

Parameters:

parameter
pattern

The pattern to replace.

parameter
value

The value to replace the pattern with.

parameter
literal

Take the pattern as a literal string (not a regex).

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.replace("b", "X").alias("replace"))
>>>
┌───────────┬─────────────┐
│ a ┆ replace_all │
------
strstr
╞═══════════╪═════════════╡
│ a bAb c d ┆ a XAX c d │
│ bCbb c d ┆ XCXX c d │
│ bb ┆ XX │
│ b ┆ X │
│ a ┆ a │
│ null ┆ null │
└───────────┴─────────────┘

replace_many
def replace_many(
patterns: IntoTdExpr | Mapping[str, str],
replace_with: IntoTdExpr | NoDefault = <no_default>,
ascii_case_insensitive: bool = False,
) -> TdExpr

Replace the all occurences of any the given patterns with the given string.

Parameters:

parameter
patterns

The patterns to replace.

parameter
replace_with

The value to replace the pattern with.

parameter
ascii_case_insensitive

If true, the search is case-insensitive.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.replace_many(["a", "b"], "X").alias("replace_many"))
>>>
┌──────┬──────────────┐
│ a ┆ replace_many │
------
strstr
╞══════╪══════════════╡
│ abc ┆ XXc │
│ axy ┆ Xxy │
│ xyb ┆ xyX │
│ xyz ┆ xyz │
│ null ┆ null │
└──────┴──────────────┘

reverse
def reverse() -> TdExpr

Reverse the string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.reverse().alias("reverse"))
>>>
┌──────┬─────────┐
│ a ┆ reverse │
------
strstr
╞══════╪═════════╡
│ abc ┆ cba │
│ a ┆ a │
│ null ┆ null │
└──────┴─────────┘

slice
def slice(
offset: int | IntoTdExprColumn,
length: int | IntoTdExprColumn | None = None,
) -> TdExpr

Extract the substring at the given offset for the given length.

Parameters:

parameter
offset

The offset to start the slice.

parameter
length

The length of the slice. If None, slice until the end of the string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.slice(1,1).alias("slice"))
>>>
┌──────┬───────┐
│ a ┆ slice
------
strstr
╞══════╪═══════╡
│ abc ┆ b │
│ a ┆ │
│ null ┆ null │
└──────┴───────┘

starts_with
def starts_with(prefix: str | TdExpr) -> TdExpr

Evaluate if the string start with.

Parameters:

parameter
prefix

The suffix to search for.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.starts_with("a").alias("starts_with"))
>>>
┌──────┬────────────┐
│ a ┆ start_with │
------
strbool
╞══════╪════════════╡
│ a ┆ true │
│ ab ┆ true │
│ b ┆ false │
│ xaby ┆ false │
│ null ┆ null │
└──────┴────────────┘

strip_chars
def strip_chars(characters: IntoTdExpr = None) -> TdExpr

Trim string values.

Parameters:

parameter
characters

Characters to trim from start and end of the string. All characteres in the given string are removed, regardless the order. Default is whitespace.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.strip_chars("a ").alias("strip_chars"))
>>>
┌─────────────────────────────────┬─────────────┐
│ a ┆ strip_chars │
------
strstr
╞═════════════════════════════════╪═════════════╡
│ acba cda … ┆ cba cd │
│ xy z ┆ xy z │
│ null ┆ null │
└─────────────────────────────────┴─────────────┘

strip_chars_end
def strip_chars_end(characters: IntoTdExpr = None) -> TdExpr

Trim string values from the end of the string.

Parameters:

parameter
characters

Characters to trim from start of the string. All ending characteres in the given string are removed, regardless the order. Default is whitespace.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.strip_chars_end("dc ").alias("strip_chars_end"))
>>>
┌───────────────────────────────┬─────────────────┐
│ a ┆ strip_chars_end │
------
strstr
╞═══════════════════════════════╪═════════════════╡
│ cba cd ┆ cba │
│ xy z ┆ xy z │
│ null ┆ null │
└───────────────────────────────┴─────────────────┘

strip_chars_start
def strip_chars_start(characters: IntoTdExpr = None) -> TdExpr

Trim string values from the start of the string.

Parameters:

parameter
characters

Characters to trim from start of the string. All starting characteres in the given string are removed, regardless the order. Default is whitespace.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.strip_chars_start("abc").alias("strip_chars_start"))
>>>
┌───────────────────────────────┬────────────────────────────┐
│ a ┆ strip_chars_start │
------
strstr
╞═══════════════════════════════╪════════════════════════════╡
│ cba cd ┆ cd │
│ xy z ┆ xy z │
│ null ┆ null │
└───────────────────────────────┴────────────────────────────┘

strip_prefix
def strip_prefix(prefix: IntoTdExpr) -> TdExpr

Trim string values removing the given prefix

Parameters:

parameter
prefix

Prefix to remove from the string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.strip_prefix("cb").alias("strip_prefix"))
>>>
┌───────────────────────────────┬─────────────────┐
│ a ┆ strip_prefix │
------
strstr
╞═══════════════════════════════╪═════════════════╡
│ cba cd ┆ a cd │
│ bx ┆ bx │
│ null ┆ null │
└───────────────────────────────┴─────────────────┘

strip_suffix
def strip_suffix(suffix: IntoTdExpr) -> TdExpr

Trim string values removing the given suffix

Parameters:

parameter
suffix

Suffix to remove from the string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.strip_suffix("cd").alias("strip_suffix"))
>>>
┌───────────────────────────────┬─────────────────┐
│ a ┆ strip_suffix │
------
strstr
╞═══════════════════════════════╪═════════════════╡
│ cba cd ┆ cba │
│ bx ┆ bx │
│ null ┆ null │
└───────────────────────────────┴─────────────────┘

tail
def tail(n: int | IntoTdExprColumn) -> TdExpr

Extract the end of the string up to the given length.

Parameters:

parameter
n

The length of the tail.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.tail(2).alias("tail"))
>>>
┌──────┬──────┐
│ a ┆ tail │
------
strstr
╞══════╪══════╡
│ abc ┆ bc │
│ a ┆ a │
│ null ┆ null │
└──────┴──────┘

to_date
def to_date(fmt: str | None = None, strict: bool = True) -> TdExpr

Convert the string to a date.

Parameters:

parameter
fmt

The date format string (default %Y-%m-%d) [formats] (https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html).

parameter
strict

Whether to parse the date strictly.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.to_date().alias("to_date"))
>>>
┌────────────┬────────────┐
│ a ┆ to_date │
------
str ┆ date │
╞════════════╪════════════╡
2024-12-132024-12-13
2024-12-152024-12-15
│ null ┆ null │
└────────────┴────────────┘

to_datetime
def to_datetime(
fmt: str | None = None,
time_unit: TimeUnit | None = None,
time_zone: str | None = None,
strict: bool = True,
ambiguous: Ambiguous | TdExpr = 'raise',
) -> TdExpr

Convert the string to a datetime.

to_integer
def to_integer(base: int | IntoTdExprColumn = 10, strict: bool = True) -> TdExpr

Covert a string to integer.

Parameters:

parameter
base

The base of the integer.

parameter
strict

If true, raise an error if the string is not a valid integer.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a")
.str.to_integer(strict=False).alias("to_integer"))
>>>
┌──────┬────────────┐
│ a ┆ to_integer │
------
str ┆ i64 │
╞══════╪════════════╡
11
2.2 ┆ null │
│ a ┆ null │
│ null ┆ null │
└──────┴────────────┘

to_lowercase
def to_lowercase() -> TdExpr

Return the lowercase of a string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.to_lowercase().alias("to_lowercase"))
>>>
┌──────┬───────────────┐
│ a ┆ to_lowerrcase │
------
str ┆ u32 │
╞══════╪═══════════════╡
│ aB ┆ ab │
│ null ┆ null │
└──────┴───────────────┘

to_time
def to_time(fmt: str | None = None, strict: bool = True, cache: bool = True) -> TdExpr

Convert the string to a time.

Parameters:

parameter
fmt

The time format string (default %H:%M:%S) [formats] (https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html).

parameter
strict

Whether to parse the date strictly.

parameter
cache

Whether to cache the date.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.to_time().alias("to_time"))
>>>
┌─────────────────────┬─────────────────────┐
│ a ┆ to_datetime │
------
str ┆ datetime[μs]
╞═════════════════════╪═════════════════════╡
2024-12-13 08:45:342024-12-13 08:45:34
2024-12-15 18:33:002024-12-15 18:33:00
│ null ┆ null │
└─────────────────────┴─────────────────────┘

to_titlecase
def to_titlecase() -> TdExpr

Uppercase the first character and lowercase all the others ones of a string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.to_titlecase().alias("titlecase"))
>>>
┌──────┬───────────┐
│ a ┆ titlecase │
------
strstr
╞══════╪═══════════╡
│ ab ┆ Ab │
│ Ab ┆ Ab │
│ AB ┆ Ab │
│ aB ┆ Ab │
│ null ┆ null │
└──────┴───────────┘

to_uppercase
def to_uppercase() -> TdExpr

Return the uppercase of a string.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.to_uppercase().alias("to_uppercase"))
>>>
┌──────┬──────────────┐
│ a ┆ to_uppercase │
------
str ┆ u32 │
╞══════╪══════════════╡
│ aB ┆ AB │
│ null ┆ null │
└──────┴──────────────┘

zfill
def zfill(length: int | IntoTdExprColumn) -> TdExpr

Pad numeric string values at the start to the given length using zeros.

Parameters:

parameter
length

The length to end pad the string to.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.zfill(2).alias("zfill"))
>>>
┌──────┬───────┐
│ a ┆ zfill │
------
strstr
╞══════╪═══════╡
000
101
10001000
│ null ┆ null │
└──────┴───────┘

to_tdexpr

def to_tdexpr(expr: Expr) -> TdExpr