Skip to content

Commit

Permalink
fix(python): Fix drop selector (#18034)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Aug 4, 2024
1 parent d84b3fd commit 9aa91a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions crates/polars-plan/src/plans/conversion/expr_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,11 +854,18 @@ pub(super) fn expand_selectors(
) -> PolarsResult<Arc<[ColumnName]>> {
let mut columns = vec![];

// Skip the column fast paths.
fn skip(name: &str) -> bool {
is_regex_projection(name) || name == "*"
}

for s in s {
match s {
Selector::Root(e) => match *e {
Expr::Column(name) => columns.push(name),
Expr::Columns(names) => columns.extend_from_slice(names.as_ref()),
Expr::Column(name) if !skip(name.as_ref()) => columns.push(name),
Expr::Columns(names) if names.iter().all(|n| !skip(n.as_ref())) => {
columns.extend_from_slice(names.as_ref())
},
Expr::Selector(s) => {
let names = expand_selector(s, schema, keys)?;
columns.extend_from_slice(names.as_ref());
Expand Down
3 changes: 3 additions & 0 deletions py-polars/tests/unit/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ def test_selector_drop(df: pl.DataFrame) -> None:
dfd = df.drop(cs.numeric(), cs.temporal())
assert dfd.columns == ["eee", "fgg", "qqR"]

df = pl.DataFrame([["x"], [1]], schema={"foo": pl.String, "foo_right": pl.Int8})
assert df.drop(cs.ends_with("_right")).schema == {"foo": pl.String()}


def test_selector_duration(df: pl.DataFrame) -> None:
assert df.select(cs.duration("ms")).columns == []
Expand Down

0 comments on commit 9aa91a7

Please sign in to comment.