Skip to content
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

feat(python!): Default to raising on out-of-bounds indices in all get/gather operations #16841

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 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 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
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=False),
layout2=pl.col("a").list.get(0, null_on_oob=True),
)

expected = pl.DataFrame(
Expand Down
Loading