Skip to content
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): Identify inefficient use of Python string replace in map_elements #19668

Merged

Conversation

alexander-beedie
Copy link
Collaborator

Ref: #9968

Can now identify (and warn against) Python-side string replace in UDFs:

Example

import polars as pl

df = pl.DataFrame({"s1": ["x::y::z"]})

df.with_columns(
    s2 = pl.col("s1").map_elements(lambda s: s.replace(":", ""))
)
# PolarsInefficientMapWarning:
#   Replace this expression...
#     - pl.col("s1").map_elements(lambda s: ...)
#   with this one instead:
#     + pl.col("s1").str.replace_all(':','',literal=True)

df.with_columns(
    s2 = pl.col("s1").map_elements(lambda s: s.replace(":", "", 2))
)
# PolarsInefficientMapWarning:
#   Replace this expression...
#     - pl.col("s1").map_elements(lambda s: ...)
#   with this one instead:
#     + pl.col("s1").str.replace(':','',n=2,literal=True)

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Nov 6, 2024
@alexander-beedie alexander-beedie force-pushed the identify-inefficient-str-replace branch from 528a2ec to 96ed7ec Compare November 6, 2024 20:58
@ritchie46 ritchie46 merged commit 8f9ba23 into pola-rs:main Nov 6, 2024
12 checks passed
@alexander-beedie alexander-beedie deleted the identify-inefficient-str-replace branch November 6, 2024 21:15
Copy link

codecov bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Project coverage is 79.74%. Comparing base (04c4acb) to head (96ed7ec).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
py-polars/polars/_utils/udfs.py 84.61% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19668   +/-   ##
=======================================
  Coverage   79.74%   79.74%           
=======================================
  Files        1541     1541           
  Lines      212010   212023   +13     
  Branches     2446     2449    +3     
=======================================
+ Hits       169059   169073   +14     
+ Misses      42397    42395    -2     
- Partials      554      555    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

tylerriccio33 pushed a commit to tylerriccio33/polars that referenced this pull request Nov 8, 2024
@alexander-beedie alexander-beedie changed the title feat: Identify inefficient use of Python string replace in map_elements feat(python): Identify inefficient use of Python string replace in map_elements Nov 9, 2024
@alexander-beedie alexander-beedie removed the rust Related to Rust Polars label Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants