-
Notifications
You must be signed in to change notification settings - Fork 123
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
feat: add Schema.to_(arrow|pandas|polars)
#1924
feat: add Schema.to_(arrow|pandas|polars)
#1924
Conversation
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))
@MarcoGorelli I've pushed this early to get some thoughts on handling To avoid needing a user-provided arg, I think we'd need to approach it differently than in here with the public/private function pair: narwhals/narwhals/functions.py Lines 436 to 451 in 53e780c
If you have something like: class Schema(BaseSchema):
_version: ClassVar[Version] = Version.MAIN That could easily be overridden for v1: class Schema(NwSchema):
_version = Version.V1 |
New API can start without deprecations, related narwhals-dev#1931
This comment was marked as resolved.
This comment was marked as resolved.
thanks Dan! Yes, having a private Some other comments:
|
This comment was marked as outdated.
This comment was marked as outdated.
yup, thanks! |
Great, I'll get that added soon
I think you've stumbled into an interesting topic here @MarcoGorelli. Mainly this was from adapting narwhals/narwhals/functions.py Lines 473 to 480 in 71a5bc5
Yeah definitely! QuestionIf we drop the I can keep this local to |
I think in general we need a but |
`pandas` seems a bit more complex, leaving for now
https://results.pre-commit.ci/run/github/760058710/1738698879.vjLSyWjmSYiQy0B_ViLJlA @MarcoGorelli should this fall under the same exception-to-the-rule as the EditSeems I misunderstood how the check works https://github.com/narwhals-dev/narwhals/blob/71a5bc5d6848da10e658db9d68a4a139f37c099b/utils/import_check.py Not sure how this could fit in |
When needed, this is now available on `Schema` (d1e0576)
thanks, looking good! you can add a |
Thanks @MarcoGorelli, will give this a try tomorrow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @dangotbanned
just made some minor edits, and got dtype_backend
to match how it currently is in pandas
happy with this?
Thanks @MarcoGorelli, just taking a look now |
narwhals/schema.py
Outdated
if parse_version(pl.__version__) < (1, 0, 0): # pragma: no cover | ||
return dict(schema) # type: ignore[return-value] | ||
return pl.Schema(schema) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MarcoGorelli why is the order reversed now?
This is just one small example from typing_extensions
:
I find the pattern there is easier to follow:
if parse_version(pl.__version__) >= (1, 0, 0):
# happy path first
...
else:
# compat path
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import of cast
was blocking me adding it as a suggestion, but this is my suggestion
(3bcebc1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it just became easier to not have type ignore
and pragma no cover
on the same line (we measure coverage in the ci job with the latest versions - i've found combining coverage jobs to be too unreliable and flaky)
sure, no objections to 3bcebc1
|
||
def test_schema_to_pandas_invalid() -> None: | ||
schema = nw.Schema({"a": nw.Int64()}) | ||
msg = "Expected one of {None, 'pyarrow', 'numpy_nullable'}, got: 'cabbage'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
Ready to merge if you're onboard w/ (#1924 (comment)) @MarcoGorelli Thanks for helping me through this 🎉 Note Maybe rename the PR to make it more discoverable? |
sure, looks good, feel free to apply that and ship it |
Ah great, sorry last note in (#1924 (comment)) (if you didn't see it) |
changing the title? sure, i think you should have permission to edit |
Oh yeah 🎉 |
nw.Schema.to_*
methodsSchema.to_(arrow|pandas|polars)
Will close #1912
What type of PR is this? (check all applicable)
Related issues
nw.(DType|Schema)
conversion API #1912Checklist
If you have comments or can explain your changes, please do so below
nw.functions._from_dict_impl
Schema
w/._version