Skip to content

Commit 57a5dde

Browse files
skritsotalakisskritsotalakis
andauthored
Feat: add list.get(index: int) (#2949)
--------- Co-authored-by: skritsotalakis <[email protected]>
1 parent e9c19d1 commit 57a5dde

File tree

13 files changed

+253
-0
lines changed

13 files changed

+253
-0
lines changed

docs/api-reference/expr_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
options:
66
members:
77
- contains
8+
- get
89
- len
910
- unique
1011
show_source: false

docs/api-reference/series_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
options:
66
members:
77
- contains
8+
- get
89
- len
910
- unique
1011
show_source: false

narwhals/_arrow/series_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ def len(self) -> ArrowSeries:
1717
return self.with_native(pc.list_value_length(self.native).cast(pa.uint32()))
1818

1919
unique = not_implemented()
20+
2021
contains = not_implemented()
22+
23+
def get(self, index: int) -> ArrowSeries:
24+
return self.with_native(pc.list_element(self.native, index))

narwhals/_compliant/any_namespace.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ def offset_by(self, by: str) -> CompliantT_co: ...
5252

5353

5454
class ListNamespace(_StoresCompliant[CompliantT_co], Protocol[CompliantT_co]):
55+
def get(self, index: int) -> CompliantT_co: ...
56+
5557
def len(self) -> CompliantT_co: ...
58+
5659
def unique(self) -> CompliantT_co: ...
5760
def contains(self, item: NonNestedLiteral) -> CompliantT_co: ...
5861

narwhals/_compliant/expr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,9 @@ def unique(self) -> EagerExprT:
10001000
def contains(self, item: NonNestedLiteral) -> EagerExprT:
10011001
return self.compliant._reuse_series_namespace("list", "contains", item=item)
10021002

1003+
def get(self, index: int) -> EagerExprT:
1004+
return self.compliant._reuse_series_namespace("list", "get", index=index)
1005+
10031006

10041007
class CompliantExprNameNamespace( # type: ignore[misc]
10051008
_ExprNamespace[CompliantExprT_co],

narwhals/_duckdb/expr_list.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def contains(self, item: NonNestedLiteral) -> DuckDBExpr:
3333
return self.compliant._with_elementwise(
3434
lambda expr: F("list_contains", expr, lit(item))
3535
)
36+
37+
def get(self, index: int) -> DuckDBExpr:
38+
return self.compliant._with_elementwise(
39+
lambda expr: F("list_extract", expr, lit(index + 1))
40+
)

narwhals/_ibis/expr_list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from narwhals._compliant.any_namespace import ListNamespace
77

88
if TYPE_CHECKING:
9+
import ibis.expr.types as ir
10+
911
from narwhals._ibis.expr import IbisExpr
1012
from narwhals.typing import NonNestedLiteral
1113

@@ -19,3 +21,9 @@ def unique(self) -> IbisExpr:
1921

2022
def contains(self, item: NonNestedLiteral) -> IbisExpr:
2123
return self.compliant._with_callable(lambda expr: expr.contains(item))
24+
25+
def get(self, index: int) -> IbisExpr:
26+
def _get(expr: ir.ArrayColumn) -> ir.Column:
27+
return expr[index]
28+
29+
return self.compliant._with_callable(_get)

narwhals/_pandas_like/series_list.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ def len(self) -> PandasLikeSeries:
3333
return self.with_native(result.astype(dtype)).alias(self.native.name)
3434

3535
unique = not_implemented()
36+
3637
contains = not_implemented()
38+
39+
def get(self, index: int) -> PandasLikeSeries:
40+
result = self.native.list[index]
41+
result.name = self.native.name
42+
return self.with_native(result)

narwhals/_polars/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ class PolarsListNamespace(PolarsAnyNamespace[CompliantT, NativeT_co]):
332332
@abc.abstractmethod
333333
def len(self) -> CompliantT: ...
334334

335+
get: Method[CompliantT]
336+
335337
unique: Method[CompliantT]
336338

337339

narwhals/_spark_like/expr_list.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ def func(expr: Column) -> Column:
2727
return F.array_contains(expr, F.lit(item))
2828

2929
return self.compliant._with_elementwise(func)
30+
31+
def get(self, index: int) -> SparkLikeExpr:
32+
def _get(expr: Column) -> Column:
33+
return expr.getItem(index)
34+
35+
return self.compliant._with_elementwise(_get)

0 commit comments

Comments
 (0)