Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdlineluser committed Jul 31, 2023
1 parent 0eb2b86 commit fc8346c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
29 changes: 15 additions & 14 deletions py-polars/polars/expr/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,24 +1289,25 @@ def extract_groups(self, pattern: str) -> Expr:
... data={
... "url": [
... "http://vote.com/ballon_dor?candidate=messi&ref=python",
... "http://vote.com/ballon_dor?candidate=haaland&ref=polars",
... "http://vote.com/ballon_dor?candidate=weghorst&ref=polars",
... "http://vote.com/ballon_dor?error=404&ref=rust",
... ]
... }
... )
>>> df.select(
... captures = pl.col("url").str.extract_groups(r"candidate=(?<candidate>\w+)&ref=(?<ref>\w+)")
... ).unnest("captures")
shape: (3, 3)
┌──────────────────────────────┬───────────┬────────┐
│ 0 ┆ candidate ┆ ref │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞══════════════════════════════╪═══════════╪════════╡
│ candidate=messi&ref=python ┆ messi ┆ python │
│ candidate=haaland&ref=polars ┆ haaland ┆ polars │
│ null ┆ null ┆ null │
└──────────────────────────────┴───────────┴────────┘
>>> pattern = r"candidate=(?<candidate>\w+)&ref=(?<ref>\w+)"
>>> df.select(captures=pl.col("url").str.extract_groups(pattern)).unnest(
... "captures"
... )
shape: (3, 2)
┌───────────┬────────┐
│ candidate ┆ ref │
│ --- ┆ --- │
│ str ┆ str │
╞═══════════╪════════╡
│ messi ┆ python │
│ weghorst ┆ polars │
│ null ┆ null │
└───────────┴────────┘
"""
return wrap_expr(self._pyexpr.str_extract_groups(pattern))
Expand Down
10 changes: 5 additions & 5 deletions py-polars/polars/series/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,17 +791,17 @@ def extract_groups(self, pattern: str) -> Series:
... name="url",
... values=[
... "http://vote.com/ballon_dor?candidate=messi&ref=python",
... "http://vote.com/ballon_dor?candidate=haaland&ref=polars",
... "http://vote.com/ballon_dor?candidate=weghorst&ref=polars",
... "http://vote.com/ballon_dor?error=404&ref=rust",
... ],
... )
>>> s.str.extract_groups(r"candidate=(?<candidate>\w+)&ref=(?<ref>\w+)")
shape: (3,)
Series: 'url' [struct[3]]
Series: 'url' [struct[2]]
[
{"candidate=messi&ref=python","messi","python"}
{"candidate=haaland&ref=polars","haaland","polars"}
{null,null,null}
{"messi","python"}
{"weghorst","polars"}
{null,null}
]
"""
Expand Down

0 comments on commit fc8346c

Please sign in to comment.