Skip to content

Commit

Permalink
feat(python!): Default to raising for oob on all get/gather opera…
Browse files Browse the repository at this point in the history
…tions (pola-rs#16841)
  • Loading branch information
ritchie46 authored and Wouittone committed Jun 22, 2024
1 parent be5bad6 commit b6f9aea
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions py-polars/polars/expr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def arg_max(self) -> Expr:
"""
return wrap_expr(self._pyexpr.arr_arg_max())

def get(self, index: int | IntoExprColumn, *, null_on_oob: bool = True) -> Expr:
def get(self, index: int | IntoExprColumn, *, null_on_oob: bool = False) -> Expr:
"""
Get the value by index in the sub-arrays.
Expand Down Expand Up @@ -503,7 +503,7 @@ def first(self) -> Expr:
└───────────────┴───────┘
"""
return self.get(0)
return self.get(0, null_on_oob=True)

def last(self) -> Expr:
"""
Expand All @@ -528,7 +528,7 @@ def last(self) -> Expr:
└───────────────┴──────┘
"""
return self.get(-1)
return self.get(-1, null_on_oob=True)

def join(self, separator: IntoExprColumn, *, ignore_nulls: bool = True) -> Expr:
"""
Expand Down
8 changes: 4 additions & 4 deletions py-polars/polars/expr/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def get(
self,
index: int | Expr | str,
*,
null_on_oob: bool = True,
null_on_oob: bool = False,
) -> Expr:
"""
Get the value by index in the sublists.
Expand All @@ -526,7 +526,7 @@ def get(
Examples
--------
>>> df = pl.DataFrame({"a": [[3, 2, 1], [], [1, 2]]})
>>> df.with_columns(get=pl.col("a").list.get(0))
>>> df.with_columns(get=pl.col("a").list.get(0, null_on_oob=True))
shape: (3, 2)
┌───────────┬──────┐
│ a ┆ get │
Expand Down Expand Up @@ -646,7 +646,7 @@ def first(self) -> Expr:
│ [1, 2] ┆ 1 │
└───────────┴───────┘
"""
return self.get(0)
return self.get(0, null_on_oob=True)

def last(self) -> Expr:
"""
Expand All @@ -667,7 +667,7 @@ def last(self) -> Expr:
│ [1, 2] ┆ 2 │
└───────────┴──────┘
"""
return self.get(-1)
return self.get(-1, null_on_oob=True)

def contains(
self, item: float | str | bool | int | date | datetime | time | IntoExprColumn
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/series/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def arg_max(self) -> Series:
"""

def get(self, index: int | IntoExprColumn, *, null_on_oob: bool = True) -> Series:
def get(self, index: int | IntoExprColumn, *, null_on_oob: bool = False) -> Series:
"""
Get the value by index in the sub-arrays.
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/series/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def get(
self,
index: int | Series | list[int],
*,
null_on_oob: bool = True,
null_on_oob: bool = False,
) -> Series:
"""
Get the value by index in the sublists.
Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,9 @@ def test_take_list_15719() -> None:
)
df = df.select(
a_explode=pl.col("a").explode(),
a_get=pl.col("a").list.get(0),
a_get=pl.col("a").list.get(0, null_on_oob=True),
b_explode=pl.col("b").explode(),
b_get=pl.col("b").list.get(0),
b_get=pl.col("b").list.get(0, null_on_oob=True),
)

expected_schema = pl.List(pl.Int64)
Expand Down
4 changes: 2 additions & 2 deletions py-polars/tests/unit/operations/namespaces/list/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,8 +878,8 @@ def test_list_get_with_null() -> None:
# 2. null element are not stored in `value` array.
out = df.select(
# For performance reasons, when-then-otherwise produces the list with layout-1.
layout1=pl.when(pl.col("b")).then([1, 2]).list.get(0),
layout2=pl.col("a").list.get(0),
layout1=pl.when(pl.col("b")).then([1, 2]).list.get(0, null_on_oob=True),
layout2=pl.col("a").list.get(0, null_on_oob=True),
)

expected = pl.DataFrame(
Expand Down

0 comments on commit b6f9aea

Please sign in to comment.