Repository navigation
Narwhals support (alongside existing Polars support) - #4979
Open
MarcoGorelli wants to merge 1 commit into
Open
MarcoGorelli wants to merge 1 commit into
MarcoGorelli wants to merge 1 commit into
Conversation
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D120721406. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
MarcoGorelli
marked this pull request as ready for review
October 22, 2026 17:38
This comment has been minimized.
This comment has been minimized.
stroxler
requested changes
Oct 27, 2026
stroxler
left a comment
Contributor
There was a problem hiding this comment.
This basically looks good to me.
Back to you for two minor things:
- A rebase would help me merge, there's now a conflict with trunk (probably very minor)
- Should we have test stubs for the v2 API? I don't know much about this so I trust you, but an agent review flagged that this might be worth asking about
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Collaborator
Author
|
thanks for your review! sure, done |
stroxler
approved these changes
Oct 30, 2026
stroxler
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4976. The general idea is to use
DataFrameKind::Narwhalsto piggy-back off of the existing Polars support, and only track computed dtypes in cases where Narwhals guarantees them (which is fewer than when Polars does)Demo
What works well
Let's start with
Note that Narwhals dataframes are themselves generic over the native library. We can either write
nw.DataFrame[pl.DataFrame], or justnw.DataFrameand leave the native library unknownPyrefly shows the expected types:
narwhals.dataframe.DataFrame[polars.dataframe.frame.DataFrame][region: String, units: Int64, price: Float64]DataFrame[Unknown][region: String, units: Int64, price: Float64, ...]It also correctly finds the error in
entry:but is fine with it in
partial 👍Series types are detected. Adding
reveal_type(df['region'])toentryshowsProjections are fine / work as expected in Polars
A few more demos:
Stable namespaces (narwhals.stable.v1 / narwhals.stable.v2) are tracked the same way
What's left out
nw.from_dict/nw.from_native. Initially at least, I think this is fine. If there's interest, it could be addressed as a follow-upnw.len()) than there are in Polars, as the result in Narwhals may vary depending on the backend. I think this is OK, it's easier to start more relaxed and become stricter later than to do the other way roundTest Plan
tests are included