feat: Add a dedicated remove
method for DataFrame
and LazyFrame
#21259
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.
We currently have frame-level
filter
, but after adding "DELETE" support to the SQL engine it felt like we should really offer a dedicatedremove
method too:Aside from improved semantics (eg: it's much clearer to say "remove rows that match this" rather than using
filter
to say "don't remove rows that don't match this") there is also a slight subtlety asremove(predicate)
is not the same thing asfilter(~predicate)
. This is because of null value comparison.For
filter
andremove
to share common/consistent semantics (we require the predicate to evaluate asTrue
in order to be acted on) the correct conversion is actuallyfilter(predicate.ne_missing(true))
- this is not necessarily immediately obvious; handling it correctly in a clearly-named method is good UX.Common code was factored out, and no new query/graph nodes are required (
remove
guarantees that the correct predicate conversion is done, re-usingfilter
internally).Plenty of new tests added, various docstrings tidied-up / clarified, and SQL "DELETE" support was repointed to the new
remove
method.Example
Note where
total > 0
evaluates as True......and those are the rows that
remove
discards:If we had naïvely inverted the predicate using
filter
, we would also have incorrectly dropped the row with anull
total: