ExprStringNameSpace
class ExprStringNameSpace
Categories: string
String methods, accessed via Expr.str.
Examples
tf.with_columns(col("name").str.to_uppercase().alias("upper"))
Methods
to_datedef to_date(
format: str | None = None,
*,
strict: bool = True,
exact: bool = True,
cache: bool = True,
) -> Expr
Parse each string into a date.
Parameters:
exactboolIf True, the whole string must match; if False,
match a date found anywhere in it.
Example:
tf.select(col("day").str.to_date("%Y-%m-%d").alias("date"))
Omit the format to have it inferred, and pass strict=False to null out the strings that do not parse:
tf.select(col("day").str.to_date(strict=False).alias("date"))
to_datetimedef to_datetime(
format: str | None = None,
*,
time_unit: TimeUnit | None = None,
time_zone: str | None = None,
strict: bool = True,
exact: bool = True,
cache: bool = True,
ambiguous: Ambiguous | Expr = 'raise',
) -> Expr
Parse each string into a datetime.
Parameters:
time_unitTimeUnit | None (Literal['ns', 'us', 'ms'] | None)Resolution of the result: ns, us or ms.
exactboolIf True, the whole string must match; if False,
match a datetime found anywhere in it.
Policy for ambiguous local times: earliest,
latest, raise or null.
Example:
tf.select(col("when").str.to_datetime("%Y-%m-%d %H:%M:%S"))
to_timedef to_time(
format: str | None = None,
*,
strict: bool = True,
cache: bool = True,
) -> Expr
Parse each string into a time.
Parameters:
Example:
tf.select(col("clock").str.to_time("%H:%M:%S").alias("time"))
len_bytesdef len_bytes() -> Expr
Number of bytes in each string (not characters).
Example:
tf.select(col("name").str.len_bytes().alias("bytes"))
len_charsdef len_chars() -> Expr
Number of characters in each string (not bytes).
Example:
tf.select(col("name").str.len_chars().alias("chars"))
to_uppercasedef to_uppercase() -> Expr
Convert each string to uppercase.
Example:
tf.with_columns(col("name").str.to_uppercase().alias("upper"))
to_lowercasedef to_lowercase() -> Expr
Convert each string to lowercase.
Example:
tf.with_columns(col("name").str.to_lowercase().alias("lower"))
to_titlecasedef to_titlecase() -> Expr
Title-case each string, capitalizing the first letter of a word.
Example:
tf.with_columns(col("name").str.to_titlecase().alias("title"))
strip_charsdef strip_chars(characters: IntoExpr = None) -> Expr
Trim the given characters from both ends of each string.
Parameters:
charactersIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Set of characters to remove, in any order; whitespace when omitted.
Example:
tf.with_columns(col("name").str.strip_chars().alias("trimmed"))
Pass the characters to remove to strip something other than whitespace:
tf.select(col("email").str.strip_chars(".moc").alias("stripped"))
strip_chars_startdef strip_chars_start(characters: IntoExpr = None) -> Expr
Trim the given characters from the start of each string.
Parameters:
charactersIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Set of characters to remove, in any order; whitespace when omitted.
Example:
tf.select(col("name").str.strip_chars_start().alias("lstripped"))
strip_chars_enddef strip_chars_end(characters: IntoExpr = None) -> Expr
Trim the given characters from the end of each string.
Parameters:
charactersIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Set of characters to remove, in any order; whitespace when omitted.
Example:
tf.select(col("name").str.strip_chars_end().alias("rstripped"))
strip_prefixdef strip_prefix(prefix: IntoExpr) -> Expr
Remove prefix from the start of each string if present.
Parameters:
prefixIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Exact prefix to remove.
Example:
tf.select(col("email").str.strip_prefix("ada").alias("rest"))
strip_suffixdef strip_suffix(suffix: IntoExpr) -> Expr
Remove suffix from the end of each string if present.
Parameters:
suffixIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Exact suffix to remove.
Example:
tf.select(col("email").str.strip_suffix(".com").alias("rest"))
pad_startdef pad_start(length: int | IntoExprColumn, fill_char: str = ' ') -> Expr
Left-pad each string to length with a fill character.
Strings already at or over length are unchanged.
Parameters:
Example:
tf.select(col("qty").str.pad_start(6, "-").alias("padded"))
pad_enddef pad_end(length: int | IntoExprColumn, fill_char: str = ' ') -> Expr
Right-pad each string to length with a fill character.
Strings already at or over length are unchanged.
Parameters:
Example:
tf.select(col("qty").str.pad_end(6, "-").alias("padded"))
zfilldef zfill(length: int | IntoExprColumn) -> Expr
Left-pad each numeric string with zeros to length.
A leading sign is preserved before the zeros. Strings already at
or over length are unchanged.
Parameters:
Example:
tf.select(col("qty").str.zfill(5).alias("padded")) # "42" -> "00042"
containsdef contains(pattern: str | Expr, *, literal: bool = False, strict: bool = True) -> Expr
Return whether each string contains pattern.
Parameters:
Example:
tf.filter(col("email").str.contains("@example"))
The pattern is a regular expression unless literal is set:
tf.filter(col("email").str.contains(".com", literal=True))
finddef find(pattern: str | Expr, *, literal: bool = False, strict: bool = True) -> Expr
Return the index of the first match of pattern, or null.
Parameters:
Example:
tf.select(col("email").str.find("@").alias("at"))
ends_withstarts_withextractdef extract(pattern: IntoExprColumn, group_index: int = 1) -> Expr
Extract a regex capture group from each string.
Returns null where the pattern does not match.
Parameters:
Example:
col("email").str.extract(r"(.+)@(.+)", 2) # the domain
count_matchesdef count_matches(pattern: str | Expr, *, literal: bool = False) -> Expr
Count non-overlapping matches of pattern in each string.
Parameters:
Example:
tf.select(
col("email").str.count_matches(".", literal=True).alias("dots"),
)
replacedef replace(
pattern: str | Expr,
value: str | Expr,
*,
literal: bool = False,
n: int = 1,
) -> Expr
Replace matches of pattern with value in each string.
Parameters:
Example:
tf.with_columns(col("email").str.replace("example", "acme"))
Only the first match is replaced; use replace_all for every match:
tf.select(col("email").str.replace(r"[aeiou]", "*").alias("masked"))
replace_alldef replace_all(
pattern: str | Expr,
value: str | Expr,
*,
literal: bool = False,
) -> Expr
Replace all matches of pattern with value in each string.
Parameters:
Example:
tf.select(col("email").str.replace_all(r"[aeiou]", "*"))
reversedef reverse() -> Expr
Reverse the characters of each string.
Example:
tf.select(col("name").str.reverse().alias("reversed"))
slicedef slice(
offset: int | IntoExprColumn,
length: int | IntoExprColumn | None = None,
) -> Expr
Extract a substring at an offset for a given length.
Parameters:
Start index; a negative value counts from the end.
Number of characters; to the end of the string when omitted.
Example:
tf.select(col("email").str.slice(0, 3).alias("prefix"))
A negative offset counts from the end of the string:
tf.select(col("email").str.slice(-3).alias("tld"))
headtailto_integerdef to_integer(
*,
base: int | IntoExprColumn = 10,
dtype: PolarsIntegerType = Int64,
strict: bool = True,
) -> Expr
Parse each string into an integer.
#MANUAL CHANGE POST GEN: default dtype is the Tabsdata alias
Int64 from tabsdatak.tableframe.datatypes (import added); the
generated stub referenced a bare, unimported Int64.
Parameters:
Example:
tf.select(col("qty").str.to_integer().alias("count"))
Parse in another base, or widen the result type:
tf.select(col("qty").str.to_integer(base=16, dtype=Int32))
contains_anydef contains_any(patterns: IntoExpr, *, ascii_case_insensitive: bool = False) -> Expr
Return whether each string contains any of the patterns.
Parameters:
patternsIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Collection of substrings to search for.
Example:
tf.filter(col("email").str.contains_any(["example", "navy"]))
replace_manydef replace_many(
patterns: IntoExpr | Mapping[str, str],
replace_with: IntoExpr = ...,
*,
ascii_case_insensitive: bool = False,
leftmost: bool = False,
) -> Expr
Replace all occurrences of many substrings at once.
Parameters:
patternsIntoExpr | Mapping[str, str] (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None | Mapping[str, str])Substrings to replace, or a {pattern: replacement}
mapping.
replace_withIntoExpr (int | float | Decimal | date | time | datetime | timedelta | str | bool | bytes | list[Any] | Expr | str | None)Replacement for each pattern, matched by
position; omit when patterns is a mapping.
Example:
col("email").str.replace_many({"example": "acme", ".com": ".io"})