Type Casting

Expr.cast(
dtype: Boolean | Categorical | Date | Datetime | Decimal | Duration | Enum | Float32 | Float64 | Int8 | Int16 | Int64 | Int32 | Int128 | Null | String | Time | UInt8 | UInt16 | UInt32 | UInt64 | type[Any],
*,
strict: bool = True,
wrap_numerical: bool = False,
) Expr
Categories:

type_casting

Cast a value to d different type.

Parameters:
  • dtype – The data type to cast to.

  • strict – If false, invalid casts produce null’s; if true, an excetion is raised.

  • wrap_numerical – If true, overflowing numbers ara handled; if false, an excetion is raised.

Example:

>>> import tabsdata as td
>>> import polars as pl
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf = tf.select(td.col("val"), td.col("val").cast(td.Float64)).alias("cast")
>>>
┌──────┬──────┐
│ val  ┆ cast │
│ ---  ┆ ---  │
│ i64  ┆ f64  │
╞══════╪══════╡
│ 1    ┆ 1.0  │
│ 15   ┆ 15.0 │
│ 18   ┆ 18.0 │
│ 60   ┆ 60.0 │
│ 60   ┆ 60.0 │
│ 75   ┆ 75.0 │
│ null ┆ null │
└──────┴──────┘
ExprStringNameSpace.to_date(
fmt: str | None = None,
*,
strict: bool = True,
) Expr
Categories:

type_casting

Convert the string to a date.

Parameters:
  • fmt – The date format string (default %Y-%m-%d). See formats.

  • 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-13 ┆ 2024-12-13 │
│ 2024-12-15 ┆ 2024-12-15 │
│ null       ┆ null       │
└────────────┴────────────┘
ExprStringNameSpace.to_datetime(
format: str | None = None,
*,
time_unit: Literal['ns', 'us', 'ms'] | None = None,
time_zone: str | None = None,
strict: bool = True,
ambiguous: Literal['earliest', 'latest', 'raise', 'null'] | Expr = 'raise',
) Expr
Categories:

type_casting

Convert the string to a datetime.

Parameters:
  • format

    The datetime format string (default %Y-%m-%d %H:%M:%S). See formats.

  • time_unit – {None, ‘us’, ‘ns’, ‘ms’} If None (default), it inferred from the format string

  • time_zone – Time zone for the resulting value.

  • strict – If the conversion fails an error will be raised.

  • ambiguous – Policy to apply on ambiguos Datetimes: ‘raise’: saises an error ‘earliest’: use the earliest datetime ‘latest’: use the latest datetime ‘null’: set to null

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
>>> tf.select(td.col("a"), td.col("a").str.to_datetime().alias("to_datetime"))
>>>
┌─────────────────────┬─────────────────────┐
│ a                   ┆ to_datetime         │
│ ---                 ┆ ---                 │
│ str                 ┆ datetime[μs]        │
╞═════════════════════╪═════════════════════╡
│ 2024-12-13 08:45:34 ┆ 2024-12-13 08:45:34 │
│ 2024-12-15 18:33:00 ┆ 2024-12-15 18:33:00 │
│ null                ┆ null                │
└─────────────────────┴─────────────────────┘
ExprStringNameSpace.to_integer(
*,
base: int | td_typing.IntoExprColumn = 10,
strict: bool = True,
) td_expr.Expr
Categories:

type_casting

Covert a string to integer.

Parameters:
  • base – The base of the integer.

  • 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        │
╞══════╪════════════╡
│ 1    ┆ 1          │
│ 2.2  ┆ null       │
│ a    ┆ null       │
│ null ┆ null       │
└──────┴────────────┘
ExprStringNameSpace.to_time(
fmt: str | None = None,
*,
strict: bool = True,
cache: bool = True,
) Expr
Categories:

type_casting

Convert the string to a time.

Parameters:
  • fmt

    The time format string (default %H:%M:%S). See formats.

  • strict – Whether to parse the date strictly.

  • 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:34 ┆ 2024-12-13 08:45:34 │
│ 2024-12-15 18:33:00 ┆ 2024-12-15 18:33:00 │
│ null                ┆ null                │
└─────────────────────┴─────────────────────┘
ExprDateTimeNameSpace.to_string(
fmt: str | None = None,
) Expr
Categories:

type_casting