Skip to content

Commit

Permalink
feat: Add Series .str.escape_regex() (#19419)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdlineluser authored Oct 25, 2024
1 parent 591971b commit 77b939a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/series/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following methods are available under the `Series.str` attribute.
Series.str.decode
Series.str.encode
Series.str.ends_with
Series.str.escape_regex
Series.str.explode
Series.str.extract
Series.str.extract_all
Expand Down
22 changes: 22 additions & 0 deletions py-polars/polars/series/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,3 +2077,25 @@ def concat(
null
]
"""

def escape_regex(self) -> Series:
r"""
Returns string values with all regular expression meta characters escaped.
Returns
-------
Series
Series of data type :class:`String`.
Examples
--------
>>> pl.Series(["abc", "def", None, "abc(\\w+)"]).str.escape_regex())
shape: (4,)
Series: '' [str]
[
"abc"
"def"
null
"abc\(\\w\+\)"
]
"""
Original file line number Diff line number Diff line change
Expand Up @@ -1806,3 +1806,4 @@ def test_escape_regex() -> None:
)

assert_frame_equal(result_df, expected_df)
assert_series_equal(result_df["escaped"], expected_df["escaped"])

0 comments on commit 77b939a

Please sign in to comment.