Skip to content

Commit

Permalink
fix: Fix explode invalid check (#17651)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 16, 2024
1 parent 30bf890 commit 6203f50
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ impl DataFrame {
let check_offsets = || {
let first_offsets = exploded_columns[0].1.as_slice();
for (_, offsets) in &exploded_columns[1..] {
polars_ensure!(first_offsets == offsets.as_slice(),
let offsets = offsets.as_slice();

let offset_l = first_offsets[0];
let offset_r = offsets[0];
let all_equal_len = first_offsets.len() != offsets.len() || {
first_offsets
.iter()
.zip(offsets.iter())
.all(|(l, r)| (*l - offset_l) == (*r - offset_r))
};

polars_ensure!(all_equal_len,
ShapeMismatch: "exploded columns must have matching element counts"
)
}
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/operations/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,12 @@ def test_undefined_col_15852() -> None:

with pytest.raises(pl.exceptions.ColumnNotFoundError):
lf.explode("bar").join(lf, on="foo").collect()


def test_explode_17648() -> None:
df = pl.DataFrame({"a": [[1, 3], [2, 6, 7], [3, 9, 2], [4], [5, 1, 2, 3, 4]]})
assert (
df.slice(1, 2)
.with_columns(pl.int_ranges(pl.col("a").list.len()).alias("count"))
.explode("a", "count")
).to_dict(as_series=False) == {"a": [2, 6, 7, 3, 9, 2], "count": [0, 1, 2, 0, 1, 2]}

0 comments on commit 6203f50

Please sign in to comment.