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] Add str.extract_all expression #2038

Merged
merged 3 commits into from
Mar 26, 2024
Merged

[FEAT] Add str.extract_all expression #2038

merged 3 commits into from
Mar 26, 2024

Conversation

colin-ho
Copy link
Contributor

extract_all(self, pattern: str | Expression, index: int = 0)
Extracts the specified match group from all regex matches in each string in a string column. If index is 0, the entire match is returned. If the pattern does not match or the group does not exist, a null value is returned.

As a drive by:

  • Refactored + made some optimizations to the existing str.extract functionality

@github-actions github-actions bot added the enhancement New feature or request label Mar 25, 2024
Copy link

codecov bot commented Mar 25, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 84.75%. Comparing base (841e9a9) to head (4e4ff6d).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2038   +/-   ##
=======================================
  Coverage   84.74%   84.75%           
=======================================
  Files          62       62           
  Lines        6819     6827    +8     
=======================================
+ Hits         5779     5786    +7     
- Misses       1040     1041    +1     
Files Coverage Δ
daft/expressions/expressions.py 91.68% <100.00%> (+0.05%) ⬆️
daft/series.py 93.09% <80.00%> (-0.14%) ⬇️

Copy link
Contributor

@jaychia jaychia left a comment

Choose a reason for hiding this comment

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

Nice!

│ --- ┆ --- │
│ Utf8 ┆ List[Utf8] │
╞═════════╪════════════╡
│ 123 456 ┆ [123 456] │
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, does our repr not do a , between values? Interesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it does, it's just that the regex (\d+) (\d+) was matching the whole string. That's my bad, it shud just be a single (\d+) that has two matches.

@@ -822,6 +822,35 @@ def extract(self, pattern: str | Expression, index: int = 0) -> Expression:
pattern_expr = Expression._to_expression(pattern)
return Expression._from_pyexpr(self._expr.utf8_extract(pattern_expr._expr, index))

def extract_all(self, pattern: str | Expression, index: int = 0) -> Expression:
r"""Extracts the specified match group from all regex matches in each string in a string column. If index is 0, the entire match is returned.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we include also that this function always returns a list[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.

Yes, will include.

index: The index of the regex match group to extract

Returns:
Expression: a List[Utf8] expression with the extracted regex matches
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also try adding a See Also: section here that links to .str.extract? Could be good to start doing it.

};
let flat_child = Series::try_from((
"matches",
Box::new(matches) as Box<dyn arrow2::array::Array>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there no matches.into() that you can use here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah there's a to_boxed() method

}
// Mismatched len case:
(self_len, pattern_len) => Err(DaftError::ComputeError(format!(
"lhs and rhs have different length arrays: {self_len} vs {pattern_len}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Specify that this occurred in extract, and also for the extract_all case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do this for the other str kernels as well

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Mar 25, 2024
@colin-ho colin-ho merged commit 45c6039 into main Mar 26, 2024
31 checks passed
@colin-ho colin-ho deleted the colin/extract-all branch March 26, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants