Skip to content

Commit

Permalink
lazy; empty projection should give empty dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 20, 2021
1 parent 9e1c91b commit 9d8444e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 7 additions & 4 deletions polars/polars-lazy/src/logical_plan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,16 +933,19 @@ impl LogicalPlanBuilder {
pub fn project(self, exprs: Vec<Expr>) -> Self {
let (exprs, schema) = prepare_projection(exprs, self.0.schema());

// if len == 0, no projection has to be done. This is a select all operation.
if !exprs.is_empty() {
if exprs.is_empty() {
self.map(
|_| Ok(DataFrame::new_no_checks(vec![])),
AllowedOptimizations::default(),
Some(Arc::new(Schema::default())),
)
} else {
LogicalPlan::Projection {
expr: exprs,
input: Box::new(self.0),
schema: Arc::new(schema),
}
.into()
} else {
self
}
}

Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,3 +1325,7 @@ def test_df_schema_unique() -> None:

with pytest.raises(Exception):
df.rename({"b": "a"})


def test_empty_projection() -> None:
assert pl.DataFrame({"a": [1, 2], "b": [3, 4]}).select([]).shape == (0, 0)

0 comments on commit 9d8444e

Please sign in to comment.