Repository navigation
feat: support list.contains for PyArrow, pandas and Dask
#4001
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
FBruzzesi
merged 12 commits into
narwhals-dev:main
from
jonasdedden:upstream/list-contains-backends
Oct 3, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e5fb1bd
feat: support `list.contains` for PyArrow and pandas-like backends
jonasdedden 9c55198
feat: support `list.contains` and casting to `List` for Dask
jonasdedden ba10a96
fix(arrow): match NaN in `list.contains`, process it per chunk and ha…
jonasdedden b97ffee
test(list-contains): skip decimal and NaN cases on Polars<1.28
jonasdedden a3f3bc6
test(list-contains): cover ±inf, address review nits
jonasdedden 2d5a039
perf(arrow): count `list.contains` matches in narrow types over block…
jonasdedden 608d8a6
Slight simplification
jonasdedden 460c036
perf(arrow): combine small chunks in list.contains, cover its edge paths
FBruzzesi a82e1df
fix(dask): raise on List literals instead of returning a String column
FBruzzesi 796855c
test: simplify the Dask test constructor and nested-dtype skips
FBruzzesi b93bfe9
Merge pull request #1 from narwhals-dev/review/list-contains-backends
jonasdedden 8756c7c
xfail strict=False
FBruzzesi 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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import pandas as pd | ||
|
|
||
| from narwhals._compliant import LazyExprNamespace | ||
| from narwhals._compliant.any_namespace import ListNamespace | ||
| from narwhals._pandas_like.utils import is_dtype_pyarrow | ||
| from narwhals._utils import not_implemented | ||
|
|
||
| if TYPE_CHECKING: | ||
| import dask.dataframe.dask_expr as dx | ||
|
|
||
| from narwhals._dask.dataframe import Incomplete | ||
| from narwhals._dask.expr import DaskExpr | ||
| from narwhals.typing import NonNestedLiteral | ||
|
|
||
|
|
||
| def _list_contains_partition(partition: pd.Series, item: NonNestedLiteral) -> pd.Series: | ||
| from narwhals._arrow.utils import list_contains | ||
|
|
||
| array: Incomplete = partition.array | ||
| result = pd.arrays.ArrowExtensionArray(list_contains(array._pa_array, item)) | ||
| return pd.Series(result, index=partition.index, name=partition.name) | ||
|
|
||
|
|
||
| class DaskExprListNamespace(LazyExprNamespace["DaskExpr"], ListNamespace["DaskExpr"]): | ||
| def contains(self, item: NonNestedLiteral) -> DaskExpr: | ||
| def func(expr: dx.Series) -> dx.Series: | ||
| if not is_dtype_pyarrow(expr.dtype): | ||
| msg = "Only pyarrow-backed lists are supported for Dask." | ||
| raise NotImplementedError(msg) | ||
| return expr.map_partitions( | ||
| _list_contains_partition, item, meta=(expr.name, "bool[pyarrow]") | ||
| ) | ||
|
|
||
| return self.compliant._with_callable(func) | ||
|
|
||
| len = not_implemented() | ||
| unique = not_implemented() | ||
| get = not_implemented() | ||
| min = not_implemented() | ||
| max = not_implemented() | ||
| mean = not_implemented() | ||
| median = not_implemented() | ||
| sum = not_implemented() | ||
| sort = not_implemented() |
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
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.