polars
polars copied to clipboard
Allow casting integer types to Enum
Description
Currently it only seems to work for UInt32
:
import polars as pl
dtype = pl.Enum(["a", "b", "c", "d"])
s = pl.Series([0, 3, 2, 2], dtype=pl.UInt32).cast(dtype) # works
s = pl.Series([0, 3, 2, 2], dtype=pl.UInt64).cast(dtype) # ComputeError: cannot cast numeric types to 'Categorical'
@c-peters could we make this work for all integer types?
Any integer up to U32 yes since we could simply cast, however not U64. Our Categorical Type uses fixed U32 for indices. There is an open issue to change this, but it would be quite some work
Why can't we cast UInt64
? If it fits in UInt32
, great, if not, we can raise or set a null value (like with any other cast).
Basically, I am requesting a shortcut to s.cast(pl.UInt32).cast(enum_dtype)
which already works for UInt64
.
Aah misunderstanding, I thought you meant that the indices were going to be U64. But simply casting we can