Skip to content

Commit

Permalink
fix explode
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 19, 2021
1 parent d053cd0 commit d7d63b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion polars/polars-core/src/chunked_array/ops/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,10 @@ impl ChunkExplode for ListChunked {
}
}

let values = Series::try_from(("", values)).unwrap();
let values = Series::try_from((self.name(), values)).unwrap();
values.explode_by_offsets(offsets)
};
debug_assert_eq!(s.name(), self.name());
Ok((s, offsets_buf))
}
}
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl DataFrame {
/// +-----+-----+-----+
/// ```
pub fn explode<'a, J, S: Selection<'a, J>>(&self, columns: S) -> Result<DataFrame> {
// We need to sort the column by order of original occurence. Otherwise the insert by index
// We need to sort the column by order of original occurrence. Otherwise the insert by index
// below will panic
let mut columns = self.select_series(columns)?;
columns.sort_by(|sa, sb| {
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,13 @@ def test_head_tail():
assert df.tail(100).height == 10


def test_explode():
df = pl.DataFrame({"letters": ["c", "a"], "nrs": [[1, 2], [1, 3]]})
out = df.explode("nrs")
out["letters"].to_list() == ["c", "c", "a", "a"]
out["nrs"].to_list() == [1, 2, 1, 3]


def test_groupby():
df = pl.DataFrame(
{
Expand Down

0 comments on commit d7d63b1

Please sign in to comment.