Skip to content

[Enh]: Add DType.to_<backend>() #2486

@dangotbanned

Description

@dangotbanned

Tracking

Description

Originally posted by @dangotbanned in #2460 (comment)

For all of the narwhals_to_native_dtype functions, I'd recommend the same solution.

Important

It'd be nice to also do this for native_to_narwhals, but the caching may be a challenge

It is a bit much (to implement) for (#2460), but hopefully it gives an example of where these kinds of changes can lead 🙂

General

We could instead have methods on each DType class like:

to_arrow
to_dask
to_duckdb
to_polars
# NOTE: Could be more granular for these
to_pandas_like
to_spark_like
  • Each would be much shorter (the goal of the rules) and very easy to digest.
  • They'd be more performant as we'd skip all of the isinstance checks
    • By just calling the same method for every single case
  • We could also make these public very easily
  • Unsupported conversions are handled by the abscence of methods

Implementation

We would want to borrow/adapt the classinstmethod descriptor from polars.
They use that for to_python which is the same kind of operation - and is something we might want in the future anyway (#2264) 😏

V1

Another big benefit of this is that we'd move the MAIN vs V1 logic into the V1 data types.
I think this would make it really clear for us what the behavior of Enum should be:

if isinstance_or_issubclass(dtype, dtypes.Enum):
if version is Version.V1:
msg = "Converting to Enum is not supported in narwhals.stable.v1"
raise NotImplementedError(msg)
if isinstance(dtype, dtypes.Enum):
return pl.Enum(dtype.categories)
msg = "Can not cast / initialize Enum without categories present"
raise ValueError(msg)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions