Skip to content

Commit

Permalink
fix: Fix melt panic (#17088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jun 20, 2024
1 parent ce0ce02 commit c156f68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ impl DataFrame {
// self.column() is linear
// together with this loop that would make it O^2 over value_vars
let (pos, _name, _dtype) = schema.try_get_full(value_column_name)?;
let value_col = self.columns[pos].cast(&st).unwrap();
let col = &self.columns[pos];
let value_col = col.cast(&st).map_err(
|_| polars_err!(InvalidOperation: "'melt/unpivot' not supported for dtype: {}", col.dtype()),
)?;
values.extend_from_slice(value_col.chunks())
}
let values_arr = concatenate_owned_unchecked(&values)?;
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/operations/test_melt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import polars as pl
import polars.selectors as cs
from polars.testing import assert_frame_equal
Expand Down Expand Up @@ -81,3 +83,10 @@ def test_melt_no_value_vars() -> None:
schema={"a": pl.Int64, "variable": pl.String, "value": pl.Null}
)
assert_frame_equal(result, expected)


def test_melt_raise_list() -> None:
with pytest.raises(pl.exceptions.InvalidOperationError):
pl.LazyFrame(
{"a": ["x", "y"], "b": [["test", "test2"], ["test3", "test4"]]}
).melt().collect()

0 comments on commit c156f68

Please sign in to comment.