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: Added escape_regex operation to the str namespace and as a global function #19257

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

barak1412
Copy link
Contributor

Fixes #19207.

@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 Oct 16, 2024
@barak1412
Copy link
Contributor Author

barak1412 commented Oct 16, 2024

@orlp Just to be sure -

The code:

import polars as pl

df = pl.DataFrame({"text": ["abc", "def", None, "abc(\\w+)"]})

df.with_columns(escaped=pl.escape_regex('text'))

Should escape the 'text' literal, not the column, right?

Besides, I need to:

  1. Add tests.
  2. Add pl.escape_regex as function.
  3. Refactor the code such that the namespace Expr will be translated to the function Expr.
  4. Add to docs.

@orlp
Copy link
Collaborator

orlp commented Oct 16, 2024

@barak1412 pl.escape_regex should only work on Python strings, and not interact with the expression API at all. I would suggest adding a warning/error if you try to pass in an expression into it, suggesting you to use Expr.str.escape_regex instead.

Copy link

codecov bot commented Oct 16, 2024

Codecov Report

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

Project coverage is 80.21%. Comparing base (8a76dad) to head (f2a95af).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...rates/polars-plan/src/dsl/function_expr/strings.rs 85.71% 1 Missing ⚠️
.../polars-python/src/lazyframe/visitor/expr_nodes.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19257      +/-   ##
==========================================
- Coverage   80.24%   80.21%   -0.03%     
==========================================
  Files        1523     1526       +3     
  Lines      209545   209854     +309     
  Branches     2434     2436       +2     
==========================================
+ Hits       168148   168341     +193     
- Misses      40842    40958     +116     
  Partials      555      555              

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

use regex::escape;

#[inline]
pub fn escape_regex_str(s: &str) -> String {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this function so pl.escape_regex and str.escape_regex will be coupled by same implementation.

@barak1412
Copy link
Contributor Author

@orlp I will be glad if you can look, thanks.

@barak1412 barak1412 marked this pull request as draft October 18, 2024 10:35
@barak1412 barak1412 marked this pull request as ready for review October 19, 2024 17:08
@barak1412
Copy link
Contributor Author

@orlp Should be the right fix now, thanks.

@ritchie46
Copy link
Member

Looks great @barak1412, we only miss docs entries in the python reference guide. Then it is good to go.

@barak1412
Copy link
Contributor Author

barak1412 commented Oct 21, 2024

@ritchie46 Sure, added to the docs.

I hope I changed the right places, first time I am touching the docs.

Edit:
I think CI failures are unrelated to my changes.

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 rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose regex::escape in Polars Python API
3 participants