Skip to content

Commit

Permalink
fix: broadcast in zip_with for Null dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion committed Jan 9, 2024
1 parent a16fceb commit d0a6ac5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crates/polars-core/src/series/implementations/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,18 @@ impl PrivateSeries for NullChunked {
}

#[cfg(feature = "zip_with")]
fn zip_with_same_type(&self, _mask: &BooleanChunked, _other: &Series) -> PolarsResult<Series> {
Ok(self.clone().into_series())
fn zip_with_same_type(&self, mask: &BooleanChunked, other: &Series) -> PolarsResult<Series> {
let len = match (self.len(), mask.len(), other.len()) {
(a, b, c) if a == b && b == c => a,
(1, a, b) | (a, 1, b) | (a, b, 1) if a == b => a,
(a, 1, 1) | (1, a, 1) | (1, 1, a) => a,
(_, 0, _) => 0,
_ => {
polars_bail!(ShapeMismatch: "shapes of `self`, `mask` and `other` are not suitable for `zip_with` operation")
},
};

Ok(Self::new(self.name().into(), len).into_series())
}
fn explode_by_offsets(&self, offsets: &[i64]) -> Series {
ExplodeByOffsets::explode_by_offsets(self, offsets)
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/unit/functions/test_whenthen.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,7 @@ def test_when_predicates_kwargs() -> None:
),
pl.DataFrame({"misc": ["?", "z in (a|b), y<0", "?", "y=1"]}),
)


def test_when_then_null_broadcast() -> None:
assert pl.select(pl.when([True, True]).then(pl.repeat(None, 1))).height == 2

0 comments on commit d0a6ac5

Please sign in to comment.