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: Raise on invalid set dtypes #17157

Merged
merged 1 commit into from
Jun 24, 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
1 change: 1 addition & 0 deletions crates/polars-ops/src/chunked_array/list/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ pub fn list_set_operation(
set_op: SetOperation,
) -> PolarsResult<ListChunked> {
polars_ensure!(a.len() == b.len() || b.len() == 1 || a.len() == 1, ShapeMismatch: "column lengths don't match");
polars_ensure!(a.dtype() == b.dtype(), InvalidOperation: "cannot do 'set' operation on dtypes: {} and {}", a.dtype(), b.dtype());
let mut a = a.clone();
let mut b = b.clone();
if a.len() != b.len() {
Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/operations/test_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ def test_set_operations_cats(set_operation: str, outcome: list[set[str]]) -> Non
)
assert df.get_column("b").dtype == pl.List(pl.Categorical)
assert [set(el) for el in df["b"].to_list()] == outcome


def test_set_invalid_types() -> None:
df = pl.DataFrame({"a": [1, 2, 2, 3, 3], "b": [2, 2, 4, 7, 8]})

with pytest.raises(pl.exceptions.InvalidOperationError):
df.with_columns(
pl.col("b")
.implode()
.over("a", mapping_strategy="join")
.list.set_intersection([1])
)