Skip to content

Commit

Permalink
fix(rust, python): don't panic in wildcard apply
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 2, 2023
1 parent 9bde433 commit 0a5e1ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
8 changes: 5 additions & 3 deletions crates/polars-plan/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::Formatter;
use std::iter::{FlatMap, Map};
use std::iter::FlatMap;
use std::sync::Arc;
use std::vec::IntoIter;

Expand Down Expand Up @@ -202,10 +202,12 @@ pub(crate) fn get_single_leaf(expr: &Expr) -> PolarsResult<Arc<str>> {
}

#[allow(clippy::type_complexity)]
pub fn expr_to_leaf_column_names_iter(expr: &Expr) -> Map<IntoIter<Expr>, fn(Expr) -> Arc<str>> {
pub fn expr_to_leaf_column_names_iter(
expr: &Expr,
) -> FlatMap<IntoIter<Expr>, Option<Arc<str>>, fn(Expr) -> Option<Arc<str>>> {
expr_to_root_column_exprs(expr)
.into_iter()
.map(|e| expr_to_leaf_column_name(&e).unwrap())
.flat_map(|e| expr_to_leaf_column_name(&e).ok())
}

/// This should gradually replace expr_to_root_column as this will get all names in the tree.
Expand Down
15 changes: 4 additions & 11 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)
from polars.dependencies import _check_for_numpy
from polars.dependencies import numpy as np
from polars.exceptions import PolarsInefficientApplyWarning, PolarsPanicError
from polars.exceptions import PolarsInefficientApplyWarning
from polars.expr.array import ExprArrayNameSpace
from polars.expr.binary import ExprBinaryNameSpace
from polars.expr.categorical import ExprCatNameSpace
Expand Down Expand Up @@ -3806,16 +3806,9 @@ def apply(
# input x: Series of type list containing the group values
from polars.utils.udfs import warn_on_inefficient_apply

try:
root_names = self.meta.root_names()
except PolarsPanicError:
# no root names for pl.col('*')
pass
else:
if root_names:
warn_on_inefficient_apply(
function, columns=root_names, apply_target="expr"
)
root_names = self.meta.root_names()
if len(root_names) > 0:
warn_on_inefficient_apply(function, columns=root_names, apply_target="expr")

if pass_name:

Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/operations/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,8 @@ def test_apply_shifted_chunks() -> None:
def test_apply_dict_order_10128() -> None:
df = pl.select(pl.lit("").apply(lambda x: {"c": 1, "b": 2, "a": 3}))
assert df.to_dict(False) == {"literal": [{"c": 1, "b": 2, "a": 3}]}


def test_apply_10237() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})
assert df.select(pl.all().apply(lambda x: x > 50))["a"].to_list() == [False] * 3

0 comments on commit 0a5e1ec

Please sign in to comment.