feat: Disallow casting temporal to numeric - #3430
Conversation
| @@ -171,6 +171,15 @@ def cast(self, dtype: IntoDType) -> Self: | |||
| Arguments: | |||
| dtype: Data type that the object will be cast into. | |||
|
|
|||
| Note: | |||
| Unlike polars, we don't allow to cast from a temporal to a numeric data type. | |||
There was a problem hiding this comment.
TIL: polars allows also casting to Float, not only to Integer
|
Test was failing due to |
|
@FBruzzesi I know this PR isn't active, but I've been reading through I think this would be pretty painful for them here: In short, they kinda need to be able to do this because |
|
Hey @dangotbanned - which particular bit concerns you? The only cast to numeric in such file are from either decimal or types that are already numerical |
wow 🤦♂️ wow 🤦♂️ wow 🤦♂️ Sorry @FBruzzesi false alarm, JS is rubbing off on me: Contextnarwhals/src/narwhals/_plan/altair/expression.py Lines 302 to 318 in a5c38b7 The right-hand-side are vega expressions,. My mistakeSo I connected the wrong dot here: if dtype == nw.Time:
# Convert to timestamp in ms
col_in_ms = (
col.dt.hour().cast(nw.Int64) * 3600000
# ^^^^Maybe they did too? |
|
Thanks for looking into this I'm not sure about inserting an extra collect_schema everywhere for the lazy backends, i was thinking that even just doing it for Series would be enough to dissuade people from doing this |
Thanks @MarcoGorelli I also have mixed feelings. Two additional comments I should have mentioned before:
I would still understand if you want to keep this eager only, but now you have a better picture |
|
Now there's been some activity I'm swooping in with another curveball ... What if - instead - we added these guys?: Then we would have explicit options for the equivalent of casting a temporal Arrow data type in either direction 😎 |
|
that's orthogonal, the purpose of the issue was to prevent people from relying on integer -> datetime conversions
thanks! if you still have the scripts i'd suggest comparing the minimums rather than the medians maybe it's fine then, no strong opinions here. i also haven't seen any issues related to people relying on integer->datetime conversions and getting thrown off by different libraries using different default resolutions, so maybe the original issue is even out-of-date at this point and unnecessary? not sure 🤷 |
Why would you need to rely on a cast when a method does it?
You've stated that (#3430 (comment)) prevention is off the table. The original issue
Encouraging people to take a safer option with clear APIs for what they want to do is not orthogonal. Being open to alternative solutions to the problem is a healthy thing |
you wouldn't need to, but it's the kind of thing i've seen people do unfortunately (even though better alternatives already exist) |
Description
It aligns with the decision of not following polars in supertyping between temporal and numeric in #3396.
Questions/Observations:
What should we do in the case of a polars expr?
Let me know if we want to keep stable also V2, or none at all and do the check for all versions
For lazy backends, you will see an "extra"
dtype.is_numeric(), this is to avoid having to trigger acollect_schema()if there is no need for itWhat type of PR is this? (check all applicable)
Related issues
Checklist