-
Notifications
You must be signed in to change notification settings - Fork 147
feat(typing): Add IntoDType
alias
#2654
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
Conversation
@MarcoGorelli sorry I butchered a rebase π |
IntoDType: TypeAlias = "dtypes.DType | type[NonNestedDType]" | ||
"""Anything that can be converted into a Narwhals DType. | ||
|
||
Examples: | ||
>>> import polars as pl | ||
>>> import narwhals as nw | ||
>>> df_native = pl.DataFrame({"a": [1, 2, 3], "b": [4.0, 5.0, 6.0]}) | ||
>>> df = nw.from_native(df_native) | ||
>>> df.select( | ||
... nw.col("a").cast(nw.Int32), | ||
... nw.col("b").cast(nw.String()).str.split(".").cast(nw.List(nw.Int8)), | ||
... ) | ||
ββββββββββββββββββββ | ||
|Narwhals DataFrame| | ||
|------------------| | ||
|shape: (3, 2) | | ||
|βββββββ¬βββββββββββ| | ||
|β a β b β| | ||
|β --- β --- β| | ||
|β i32 β list[i8] β| | ||
|βββββββͺβββββββββββ‘| | ||
|β 1 β [4, 0] β| | ||
|β 2 β [5, 0] β| | ||
|β 3 β [6, 0] β| | ||
|βββββββ΄βββββββββββ| | ||
ββββββββββββββββββββ | ||
""" |
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 logic I'm following for this is that we use IntoDType
for anything that can trigger a cast
.
Or another way to look at it is if the call reaches into _<backend>.utils.<thing>_to_<other>_dtype
then this should warn when you're likely to raise
@@ -125,7 +125,7 @@ def test_cast_to_enum_vmain( | |||
with pytest.raises( | |||
ValueError, match="Can not cast / initialize Enum without categories present" | |||
): | |||
df_nw.select(col_a.cast(nw.Enum)) | |||
df_nw.select(col_a.cast(nw.Enum)) # type: ignore[arg-type] |
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.
Not sure if this got lost, but needing these ignores is a positive outcome π
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.
Nice one
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 - it's look pretty straightforward and definitly a nice improvement to be able to differentiate nested vs non-nested dtypes.
I only have a tiny question
@@ -125,7 +125,7 @@ def test_cast_to_enum_vmain( | |||
with pytest.raises( | |||
ValueError, match="Can not cast / initialize Enum without categories present" | |||
): | |||
df_nw.select(col_a.cast(nw.Enum)) | |||
df_nw.select(col_a.cast(nw.Enum)) # type: ignore[arg-type] |
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.
Nice one
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.
nice one, thanks both! |
Closes #2626
What type of PR is this? (check all applicable)
Related issues
IntoDType
alias, replaceDType | type[DType]
Β #2626Checklist
If you have comments or can explain your changes, please do so below
I think
selectors
are safe as-is, but will follow that up separately if needed