-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enh]: nw.(DType|Schema)
conversion API
#1912
Comments
Hey @dangotbanned , thanks for the request. Namely, you would be able to do: any_schema = AnySchema(model=nw_schema)
any_schema.to_<pandas|polars|arrow>() and get the native schemas out. This is actually some good motivation to put it out 😇 Edit: I got a link for you if interested in taking a look: anyschema |
Thanks @FBruzzesi, taking a look at https://github.com/FBruzzesi/anyschema now |
Interesting package @FBruzzesi, funny timing for this to come up Sadly its too heavy of a dependency for us to add to Note I've probably gone off the deep end trying to minimise increasing our package size 😅 However, the methods you have there are exactly what I'm after 🙂 |
Thanks for taking the time to look into it, I really appreciate it!
It's not even on pypi yet, altair should definitly not depend on it 😅
I hope it helped indirectly then. I would not expect them to be available in Narwhals itself soon (but who knows 😉) |
Thanks for your request! I think this can be in-scope, but for |
Yeah most likely @MarcoGorelli IIRC there was some versioning stuff for some of them? |
I think other backends tend to just have a single definition of a dtype. In Polars for example int64 is just The versioning should just be on the Narwhals side, the bottom-most bullet point (starting with "Since Narwhals 1.9.0") on https://narwhals-dev.github.io/narwhals/backcompat/#after-stablev1 explains - but that's not something the user would need to pass. If you made |
Got it thanks @MarcoGorelli Would you be open to me doing a PR, or is there anything else you'd wanna hash out? It feels like this could be as simple as:
|
would love to have a PR from you on this, thanks @dangotbanned ! |
Will close narwhals-dev#1912 - Starting with porting `nw.functions._from_dict_impl` - Thinking that `Schema` should have `._version: ClassVar[Version]` to remove the need for user-facing arg (narwhals-dev#1912 (comment))
We would like to learn about your use case. For example, if this feature is needed to adopt Narwhals in an open source project, could you please enter the link to it below?
We've already adopted
narwhals
in https://github.com/vega/altairThis feature would be helpful for vega/altair#3631
Please describe the purpose of the new feature or describe the problem to solve.
In (vega/altair#3631), I've used serialized dataset schemas to improve consistency between
polars
,pandas
andpyarrow
when reading from file.A challenge I've had is how to incorporate these data types:
nw.Schema
?Maybe this is a niche problem, but I'd really appreciate some public API to utilize all the
narwhals
-> native type conversion logic.Suggest a solution if possible.
The
nw.Schema
-> native logic for this is already specified innw.functions._from_dict_impl
.nw.functions._from_dict_impl
narwhals/narwhals/functions.py
Lines 444 to 546 in 53e780c
Additionally, each of these functions for the individual types:
6x
nw._(.*).utils.narwhals_to_native_dtype
narwhals/narwhals/_arrow/utils.py
Line 86 in 53e780c
narwhals/narwhals/_duckdb/utils.py
Line 142 in 53e780c
narwhals/narwhals/_dask/utils.py
Line 109 in 53e780c
narwhals/narwhals/_pandas_like/utils.py
Line 518 in 53e780c
narwhals/narwhals/_polars/utils.py
Line 150 in 53e780c
narwhals/narwhals/_spark_like/utils.py
Line 82 in 53e780c
Solution 1
Add method(s) on
DType
narwhals/narwhals/dtypes.py
Line 27 in 53e780c
Solution 2 (Preferred)
Add method(s) on
Schema
narwhals/narwhals/schema.py
Line 27 in 53e780c
This doesn't rule out Solution 1, but I think it could be the cleaner API if only one were chosen.
Something like this would be pretty ergonomic.
For my use case, I could just pass the
nw.Schema
around and only convert it when needed:If you have tried alternatives, please describe them below.
This is a short version of what I'm doing currently in (vega/altair#3631 (comment)).
It would be great to not rely on the
narwhals
internals for this though:Additional information that may help us understand your needs.
API overview vega/altair#3631
narwhals
I'd be happy to help with a PR to implement this if anyone else can see the potential value.
I've really been enjoying using
narwhals
and it has played a central role in this pretty long-running PR.Big thank you to anyone who has contributed 🙏
The text was updated successfully, but these errors were encountered: