tabsdata.tableframe.lazyframe.frame.TableFrame.cast#

TableFrame.cast(dtypes: Mapping[ColumnNameOrSelector | td_typing.TdDataType, td_typing.TdDataType] | td_typing.TdDataType, *, strict: bool = True) TableFrame[source]#

Cast columns to a new data type.

Parameters:
  • dtypes – Mapping of the column name(s) to the new data type(s).

  • strict – If True, raises an error if the cast cannot be performed.

Example:

>>> import tabsdata as td
>>>
>>> tf: td.TableFrame ...
>>>
┌──────┬──────┐
│ a    ┆ b    │
│ ---  ┆ ---  │
│ str  ┆ i64  │
╞══════╪══════╡
│ A    ┆ 1    │
│ X    ┆ 10   │
│ C    ┆ 3    │
│ D    ┆ 5    │
│ M    ┆ 9    │
│ A    ┆ 100  │
│ M    ┆ 50   │
│ null ┆ 20   │
│ F    ┆ null │
└──────┴──────┘
>>>
>>> tf.cast({"b":pl.Float32}).collect()
>>>
┌──────┬───────┐
│ a    ┆ b     │
│ ---  ┆ ---   │
│ str  ┆ f32   │
╞══════╪═══════╡
│ A    ┆ 1.0   │
│ X    ┆ 10.0  │
│ C    ┆ 3.0   │
│ D    ┆ 5.0   │
│ M    ┆ 9.0   │
│ A    ┆ 100.0 │
│ M    ┆ 50.0  │
│ null ┆ 20.0  │
│ F    ┆ null  │
└──────┴───────┘