polars.Expr.cat.ends_with#

Expr.cat.ends_with(suffix: str) Expr[source]#

Check if string representations of values end with a substring.

Parameters:
suffix

Suffix substring.

See also

contains

Check if string reprs contains a substring that matches a pattern.

starts_with

Check if string reprs start with a substring.

Notes

Whereas str.ends_with allows expression inputs, cat.ends_with requires a literal string value.

Examples

>>> df = pl.DataFrame(
...     {"fruits": pl.Series(["apple", "mango", None], dtype=pl.Categorical)}
... )
>>> df.with_columns(pl.col("fruits").cat.ends_with("go").alias("has_suffix"))
shape: (3, 2)
┌────────┬────────────┐
│ fruits ┆ has_suffix │
│ ---    ┆ ---        │
│ cat    ┆ bool       │
╞════════╪════════════╡
│ apple  ┆ false      │
│ mango  ┆ true       │
│ null   ┆ null       │
└────────┴────────────┘

Using ends_with as a filter condition:

>>> df.filter(pl.col("fruits").cat.ends_with("go"))
shape: (1, 1)
┌────────┐
│ fruits │
│ ---    │
│ cat    │
╞════════╡
│ mango  │
└────────┘