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: Fix filter shape on empty null #16670

Merged
merged 1 commit into from
Jun 3, 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
11 changes: 9 additions & 2 deletions crates/polars-core/src/series/implementations/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,15 @@ impl SeriesTrait for NullChunked {
}

fn filter(&self, filter: &BooleanChunked) -> PolarsResult<Series> {
let len = filter.sum().unwrap_or(0);
Ok(NullChunked::new(self.name.clone(), len as usize).into_series())
let len = if self.is_empty() {
// We still allow a length of `1` because it could be `lit(true)`.
polars_ensure!(filter.len() <= 1, ShapeMismatch: "filter's length: {} differs from that of the series: 0", filter.len());
0
} else {
polars_ensure!(filter.len() == self.len(), ShapeMismatch: "filter's length: {} differs from that of the series: {}", filter.len(), self.len());
filter.sum().unwrap_or(0) as usize
};
Ok(NullChunked::new(self.name.clone(), len).into_series())
}

fn shift(&self, _periods: i64) -> Series {
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/datatypes/test_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ def test_null_hash_rows_14100() -> None:
assert df.hash_rows().dtype == pl.UInt64
assert df["b"].hash().dtype == pl.UInt64
assert df.select([pl.col("b").hash().alias("foo")])["foo"].dtype == pl.UInt64


def test_null_lit_filter_16664() -> None:
assert pl.DataFrame({"x": []}).filter(pl.lit(True)).shape == (0, 1)