polars cast
For dataframe.
Cast a column to a different dtype.
warning This command requires a plugin The
polars castcommand resides in thepolarsplugin. To use this command, you must install and registernu_plugin_polars. See the Plugins chapter in the book for more information.
Command Type
plugin
Signature
> polars cast {flags} (dtype) (column)
Parameters
dtype: The dtype to cast the column to.column: The column to cast. Required when used with a dataframe.
Input/output types:
| input | output |
|---|---|
| polars_expression | polars_expression |
| polars_selector | polars_expression |
| polars_dataframe | polars_dataframe |
| polars_lazyframe | polars_lazyframe |
Examples
Cast a column in a dataframe to a different dtype
> [[a b]; [1 2] [3 4]] | polars into-df | polars cast u8 a | polars schema
╭───┬─────╮
│ a │ u8 │
│ b │ i64 │
╰───┴─────╯
Cast a column in a lazy dataframe to a different dtype
> [[a b]; [1 2] [3 4]] | polars into-df | polars into-lazy | polars cast u8 a | polars schema
╭───┬─────╮
│ a │ u8 │
│ b │ i64 │
╰───┴─────╯
Cast a column in a expression to a different dtype
> [[a b]; [1 2] [1 4]] | polars into-df | polars group-by a | polars agg [ (polars col b | polars cast u8 | polars min | polars as "b_min") ] | polars schema
Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.