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 7ef2e8f
Show file tree
Hide file tree
Showing 2 changed files with 23 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
11 changes: 11 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,14 @@ 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(pl.repeat(True, 2, dtype=pl.Boolean)).then(
pl.repeat(None, 1, dtype=pl.Null)
)
).height
== 2
)

0 comments on commit 7ef2e8f

Please sign in to comment.