-
Notifications
You must be signed in to change notification settings - Fork 171
fix: BaseFrame.filter with list[bool] in predicates
#3183
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
1b3bcab
fix: BaseFrame.filter with list[bool] in predicates
FBruzzesi 5a3af55
polars msg in exception
FBruzzesi 23f83ba
tests and rm uncovered code
FBruzzesi d9c68a0
parse list of bools into series only in DataFrame
FBruzzesi d4a02a1
update docstrings, fix typo
FBruzzesi cb193ea
forgot to save :(
FBruzzesi df7acdd
make the type checker (and @dangotbanned) happier
FBruzzesi 3a4d01e
WIP?
FBruzzesi 280b6e2
avoid to_dict
FBruzzesi 598d5c4
add a no cover
FBruzzesi 5278199
chore(typing): Simplify `_is_generator` signature
dangotbanned d59d191
refactor(suggestion): Mark externally used functions as public
dangotbanned cfc95b9
fix(typing): Un-double negative, fix `LazyFrame.filter` signature
dangotbanned 79041bb
Merge branch 'main' into fix/dataframe-filter
dangotbanned 9c0705f
simplify DataFrame parsing
FBruzzesi 30102d9
split eager-lazy boolean list test
FBruzzesi 3e5cce1
fix typing
FBruzzesi 924b43c
use Collection type in predicates_contains_list_of_bool
FBruzzesi df58126
Merge branch 'main' into fix/dataframe-filter
dangotbanned 60d6ec1
refactor: Simplify to `is_iterator`
dangotbanned File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,12 @@ | |
| is_compliant_lazyframe, | ||
| is_eager_allowed, | ||
| is_index_selector, | ||
| is_iterator, | ||
| is_lazy_allowed, | ||
| is_list_of, | ||
| is_sequence_like, | ||
| is_slice_none, | ||
| predicates_contains_list_of_bool, | ||
| qualified_type_name, | ||
| supports_arrow_c_stream, | ||
| zip_strict, | ||
|
|
@@ -242,24 +244,20 @@ def drop(self, *columns: Iterable[str], strict: bool) -> Self: | |
| return self._with_compliant(self._compliant_frame.drop(columns, strict=strict)) | ||
|
|
||
| def filter( | ||
| self, *predicates: IntoExpr | Iterable[IntoExpr] | list[bool], **constraints: Any | ||
| self, *predicates: IntoExpr | Iterable[IntoExpr], **constraints: Any | ||
| ) -> Self: | ||
| if len(predicates) == 1 and is_list_of(predicates[0], bool): | ||
| predicate = predicates[0] | ||
| else: | ||
| from narwhals.functions import col | ||
|
|
||
| flat_predicates = flatten(predicates) | ||
| check_expressions_preserve_length(*flat_predicates, function_name="filter") | ||
| plx = self.__narwhals_namespace__() | ||
| compliant_predicates, _kinds = self._flatten_and_extract(*flat_predicates) | ||
| compliant_constraints = ( | ||
| (col(name) == v)._to_compliant_expr(plx) | ||
| for name, v in constraints.items() | ||
| ) | ||
| predicate = plx.all_horizontal( | ||
| *chain(compliant_predicates, compliant_constraints), ignore_nulls=False | ||
| ) | ||
| from narwhals.functions import col | ||
|
|
||
| flat_predicates = flatten(predicates) | ||
| check_expressions_preserve_length(*flat_predicates, function_name="filter") | ||
| plx = self.__narwhals_namespace__() | ||
FBruzzesi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| compliant_predicates, _kinds = self._flatten_and_extract(*flat_predicates) | ||
| compliant_constraints = ( | ||
| (col(name) == v)._to_compliant_expr(plx) for name, v in constraints.items() | ||
| ) | ||
| predicate = plx.all_horizontal( | ||
| *chain(compliant_predicates, compliant_constraints), ignore_nulls=False | ||
| ) | ||
| return self._with_compliant(self._compliant_frame.filter(predicate)) | ||
|
|
||
| def sort( | ||
|
|
@@ -1653,7 +1651,7 @@ def filter( | |
|
|
||
| Arguments: | ||
| *predicates: Expression(s) that evaluates to a boolean Series. Can | ||
| also be a (single!) boolean list. | ||
| also be a boolean list(s). | ||
| **constraints: Column filters; use `name = value` to filter columns by the supplied value. | ||
| Each constraint will behave the same as `nw.col(name).eq(value)`, and will be implicitly | ||
| joined with the other filter conditions using &. | ||
|
|
@@ -1695,7 +1693,12 @@ def filter( | |
| foo bar ham | ||
| 1 2 7 b | ||
| """ | ||
| return super().filter(*predicates, **constraints) | ||
| impl = self.implementation | ||
| parsed_predicates = ( | ||
| self._series.from_iterable("", p, backend=impl) if is_list_of(p, bool) else p | ||
| for p in predicates | ||
| ) | ||
| return super().filter(*parsed_predicates, **constraints) | ||
|
|
||
| @overload | ||
| def group_by( | ||
|
|
@@ -2850,15 +2853,14 @@ def unique( | |
| ) | ||
|
|
||
| def filter( | ||
| self, *predicates: IntoExpr | Iterable[IntoExpr] | list[bool], **constraints: Any | ||
| self, *predicates: IntoExpr | Iterable[IntoExpr], **constraints: Any | ||
| ) -> Self: | ||
| r"""Filter the rows in the LazyFrame based on a predicate expression. | ||
|
|
||
| The original order of the remaining rows is preserved. | ||
|
|
||
| Arguments: | ||
| *predicates: Expression that evaluates to a boolean Series. Can | ||
| also be a (single!) boolean list. | ||
| *predicates: Expression(s) that evaluates to a boolean Series. | ||
| **constraints: Column filters; use `name = value` to filter columns by the supplied value. | ||
| Each constraint will behave the same as `nw.col(name).eq(value)`, and will be implicitly | ||
| joined with the other filter conditions using &. | ||
|
|
@@ -2924,13 +2926,12 @@ def filter( | |
| βββββββββ΄ββββββββ΄ββββββββββ | ||
| <BLANKLINE> | ||
| """ | ||
| if ( | ||
| len(predicates) == 1 and is_list_of(predicates[0], bool) and not constraints | ||
| ): # pragma: no cover | ||
|
Comment on lines
-2927
to
-2929
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not covering full spectrum of possibilities |
||
| predicates_ = tuple(tuple(p) if is_iterator(p) else p for p in predicates) | ||
| if predicates_contains_list_of_bool(predicates_): | ||
| msg = "`LazyFrame.filter` is not supported with Python boolean masks - use expressions instead." | ||
| raise TypeError(msg) | ||
|
|
||
| return super().filter(*predicates, **constraints) | ||
| return super().filter(*predicates_, **constraints) | ||
|
|
||
| def sink_parquet(self, file: str | Path | BytesIO) -> None: | ||
| """Write LazyFrame to Parquet file. | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.