-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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(python): Warn on inefficient use of map_elements
for temporal attributes/methods
#14529
feat(python): Warn on inefficient use of map_elements
for temporal attributes/methods
#14529
Conversation
d6f5a0c
to
e312866
Compare
6713cf6
to
54fc948
Compare
…attribute access
54fc948
to
07e3b74
Compare
map_elements
for temporal attribute accessmap_elements
for temporal attributes/methods
Looks like we should better-handle the form Update: done 👌 |
thanks for doing this looks like 3.12 is failing? |
Ah! Tweaked my env and was able to replicate locally; should be a quick fix after all 🤞 |
This is some nice stuff. I took this for a spin and and came up with a more refined evil 😄 df.with_columns(time=pl.col("dtm").map_elements(datetime.time)) |
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 work!
just out of interest, why would someone write lambda x: x and x and x.date()
?
They likely wouldn't, but |
Devious; I can tackle that in a follow-up ;) |
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, always happy to do more "butt-kicking" if people use map_elements
unnecessarily :)
map_elements
for temporal attributes/methodsmap_elements
for temporal attributes/methods
Ref: #9968.
(Implements the ".dt.month and other stdlib datetime functions" suggestion).
Saw somebody do this in our own codebase today (shame on that person 🤣), so have added new bytecode detection logic for temporal attrs/methods, along with the appropriate native expression mapping.
Covers the following temporal attributes...
date
day
hour
microsecond
minute
month
second
year
...and temporal methods:
isoweekday()
date()
time()
Before
After
Can still do it, but now triggers a
PolarsInefficientMapWarning
:Also
Now optimises out unnecessary implicit existence checks from the final suggestion, eg:
lambda x: x and x and x and x.date()
...becomes:
pl.col("d").dt.date()