-
Notifications
You must be signed in to change notification settings - Fork 172
Description
Tracking
- Add
IntoDTypealias, replaceDType | type[DType]#2626 - [Enh]: Add
DType.{from,to}_python#2264 - feat: Add
Schema.from_{native,<backend>}#2957
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
isinstancechecks- By just calling the same method for every single case
- We could also make these public very easily
- I imagine they'd be helpful in the same way as ([Enh]:
nw.(DType|Schema)conversion API #1912) 😉
- I imagine they'd be helpful in the same way as ([Enh]:
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:
narwhals/narwhals/_polars/utils.py
Lines 184 to 191 in 162dd5f
| 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) |