Skip to content

Commit

Permalink
fix: Throw error for comparison of unequal length series (#18816)
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite authored Sep 23, 2024
1 parent 341df85 commit dcad7d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 11 additions & 0 deletions crates/polars-core/src/series/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ macro_rules! impl_compare {
let (lhs, rhs) = ($self, $rhs);
validate_types(lhs.dtype(), rhs.dtype())?;

polars_ensure!(
lhs.len() == rhs.len() ||

// Broadcast
lhs.len() == 1 ||
rhs.len() == 1,
ShapeMismatch: "could not compare between two series of different length ({} != {})",
lhs.len(),
rhs.len()
);

#[cfg(feature = "dtype-categorical")]
match (lhs.dtype(), rhs.dtype()) {
(Categorical(_, _) | Enum(_, _), Categorical(_, _) | Enum(_, _)) => {
Expand Down
5 changes: 0 additions & 5 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ def test_struct_equality() -> None:
assert (s5 != s6).all()
assert (~(s5 == s6)).all()

s7 = pl.Series("misc", [{"x": "a", "y": 0}, {"x": "b", "y": 0}])
s8 = pl.Series("misc", [{"x": "a", "y": 0}, {"x": "b", "y": 0}, {"x": "c", "y": 0}])
assert (s7 != s8).all()
assert (~(s7 == s8)).all()


def test_struct_equality_strict() -> None:
s1 = pl.Struct(
Expand Down

0 comments on commit dcad7d8

Please sign in to comment.