[Python] List of int and floats in Python Polars casting to object

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    Why does the column floats in the following example casts to object instead of list[f64]? How can I change it to list[f64]?

    import polars as pl

    dfLists = pl.DataFrame({
    'ints':[ [0,1], [4,3,2]],
    'floats':[ [0.0,1], [2,3]],
    'strings':[ ["0","1"],["2","3"]]
    })
    dfLists


    shape: (2, 3)
    ┌───────────┬──────────┬────────────┐
    │ ints ┆ floats ┆ strings │
    │ --- ┆ --- ┆ --- │
    │ list[i64] ┆ object ┆ list[str] │
    ╞═══════════╪══════════╪════════════╡
    │ [0, 1] ┆ [0.0, 1] ┆ ["0", "1"] │
    │ [4, 3, 2] ┆ [2, 3] ┆ ["2", "3"] │
    └───────────┴──────────┴────────────┘


    Note: It seems this behavior may be new, based on this blog post (.html) where the same example casts to list[f64].

    Edit: After updating polars to version 0.17.2 the column casts to list[f64] as expected.

    I would still like to know if it is possible to change the dtypes of the elements of a list. For example, adapting the previous example:

    import polars as pl

    dfLists = pl.DataFrame({
    'ints':[ [0,1], [4,3,2]],
    'object':[ [0,"1"], [2,3]],
    'strings':[ ["0","1"],["2","3"]]
    })

    dfLists.select(pl.col("strings").list.eval(pl.element().cast(pl.Int16)))
    # works

    dfLists.select(pl.col("object").list.eval(pl.element().cast(pl.Int16))) # error
    # error:
    # PanicException: not implemented for dtype Object("object")

    Continue reading...

Compartilhe esta Página