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

fix(python): Fix boolean trap issue in top_k/bottom_k #16489

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 28 additions & 4 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2096,7 +2096,14 @@ def top_k(
└───────┴──────────┘
"""
k = parse_as_expression(k)
return self._from_pyexpr(self._pyexpr.top_k(k, nulls_last, multithreaded))
return self._from_pyexpr(
self._pyexpr.top_k(
k,
nulls_last=nulls_last,
maintain_order=maintain_order,
multithreaded=multithreaded,
)
)

def top_k_by(
self,
Expand Down Expand Up @@ -2228,7 +2235,12 @@ def top_k_by(
raise ValueError(msg)
return self._from_pyexpr(
self._pyexpr.top_k_by(
k, by, descending, nulls_last, maintain_order, multithreaded
k,
by,
descending=descending,
nulls_last=nulls_last,
maintain_order=maintain_order,
multithreaded=multithreaded,
)
)

Expand Down Expand Up @@ -2291,7 +2303,14 @@ def bottom_k(
└───────┴──────────┘
"""
k = parse_as_expression(k)
return self._from_pyexpr(self._pyexpr.bottom_k(k, nulls_last, multithreaded))
return self._from_pyexpr(
self._pyexpr.bottom_k(
k,
nulls_last=nulls_last,
maintain_order=maintain_order,
multithreaded=multithreaded,
)
)

def bottom_k_by(
self,
Expand Down Expand Up @@ -2423,7 +2442,12 @@ def bottom_k_by(
raise ValueError(msg)
return self._from_pyexpr(
self._pyexpr.bottom_k_by(
k, by, descending, nulls_last, maintain_order, multithreaded
k,
by,
descending=descending,
nulls_last=nulls_last,
maintain_order=maintain_order,
multithreaded=multithreaded,
)
)

Expand Down
20 changes: 14 additions & 6 deletions py-polars/src/expr/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,15 @@ impl PyExpr {
}

#[cfg(feature = "top_k")]
fn top_k(&self, k: Self, nulls_last: bool, multithreaded: bool) -> Self {
fn top_k(&self, k: Self, nulls_last: bool, maintain_order: bool, multithreaded: bool) -> Self {
self.inner
.clone()
.top_k(
k.inner,
SortOptions::default()
.with_nulls_last(nulls_last)
.with_maintain_order(multithreaded),
.with_maintain_order(maintain_order)
.with_multithreaded(multithreaded),
)
.into()
}
Expand All @@ -324,22 +325,29 @@ impl PyExpr {
SortMultipleOptions {
descending,
nulls_last,
multithreaded,
maintain_order,
multithreaded,
},
)
.into()
}

#[cfg(feature = "top_k")]
fn bottom_k(&self, k: Self, nulls_last: bool, multithreaded: bool) -> Self {
fn bottom_k(
&self,
k: Self,
nulls_last: bool,
maintain_order: bool,
multithreaded: bool,
) -> Self {
self.inner
.clone()
.bottom_k(
k.inner,
SortOptions::default()
.with_nulls_last(nulls_last)
.with_maintain_order(multithreaded),
.with_maintain_order(maintain_order)
.with_multithreaded(multithreaded),
)
.into()
}
Expand All @@ -363,8 +371,8 @@ impl PyExpr {
SortMultipleOptions {
descending,
nulls_last,
multithreaded,
maintain_order,
multithreaded,
},
)
.into()
Expand Down