markfairbanks / tidypolars

Tidy interface to polars
http://tidypolars.readthedocs.io
MIT License
337 stars 11 forks source link

changing dtypes in tibble #222

Closed kgilpas closed 1 year ago

kgilpas commented 1 year ago

Is there a way to change the dtypes seen in a tibble? say changing from f64 to i64?

markfairbanks commented 1 year ago

Yep - much like in R the best way to do this is inside a .mutate().

You can use as_boolean(), as_float(), as_string(), as_integer() or you can use a more general cast() function that gives you access to all polars types.

All of these can be found in the function reference here

import tidypolars as tp
from tidypolars import col

df = tp.Tibble(int_col = range(3), float_col = [1.0, 2.0, 3.0])

(
    df
    .mutate(as_boolean = tp.as_boolean('int_col'),
            as_integer = tp.as_integer('float_col'),
            cast = tp.cast('int_col', tp.Float64))
)
shape: (3, 5)
┌─────────┬───────────┬────────────┬────────────┬──────┐
│ int_col ┆ float_col ┆ as_boolean ┆ as_integer ┆ cast │
│ ---     ┆ ---       ┆ ---        ┆ ---        ┆ ---  │
│ i64     ┆ f64       ┆ bool       ┆ i64        ┆ f64  │
╞═════════╪═══════════╪════════════╪════════════╪══════╡
│ 0       ┆ 1.0       ┆ false      ┆ 1          ┆ 0.0  │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 1       ┆ 2.0       ┆ true       ┆ 2          ┆ 1.0  │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ 2       ┆ 3.0       ┆ true       ┆ 3          ┆ 2.0  │
└─────────┴───────────┴────────────┴────────────┴──────┘

Hope this helps - if you have any questions let me know.