Skip to content

Commit

Permalink
fix explode empty df (#3405)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed May 16, 2022
1 parent 1182ccc commit 1135178
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
15 changes: 4 additions & 11 deletions polars/polars-core/src/chunked_array/ops/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,10 @@ impl ChunkExplode for ListChunked {

// all empty
if offsets[offsets.len() - 1] == 0 {
// Safety: empty dtype is correct
return unsafe {
Ok((
Series::from_chunks_and_dtype_unchecked(
self.name(),
vec![values],
&self.inner_dtype(),
),
Buffer::from_slice(&[]),
))
};
return Ok((
Series::new_empty(self.name(), &self.inner_dtype()),
Buffer::from_slice(&[]),
));
}

if !offsets.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub trait ChunkTakeEvery<T> {
fn take_every(&self, n: usize) -> ChunkedArray<T>;
}

/// Explode/ flatten a
/// Explode/ flatten a List or Utf8 Series
pub trait ChunkExplode {
fn explode(&self) -> Result<Series> {
self.explode_and_offsets().map(|t| t.0)
Expand Down
8 changes: 5 additions & 3 deletions polars/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ pub struct MeltArgs {

impl DataFrame {
pub fn explode_impl(&self, mut columns: Vec<Series>) -> Result<DataFrame> {
let mut df = self.clone();
if self.height() == 0 {
return Ok(self.clone());
for s in &columns {
df.with_column(s.explode()?)?;
}
return Ok(df);
}
columns.sort_by(|sa, sb| {
self.check_name_to_idx(sa.name())
Expand All @@ -39,8 +43,6 @@ impl DataFrame {
.expect("cmp usize -> Ordering")
});

let mut df = self.clone();

// TODO: optimize this.
// This is the slower easier option.
// instead of filtering the whole dataframe first
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/test_explode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pyarrow as pa

import polars as pl


def test_explode_empty_df_3402() -> None:
df = pl.DataFrame({"a": pa.array([], type=pa.large_list(pa.int32()))})
assert df.explode("a").dtypes == [pl.Int32]

0 comments on commit 1135178

Please sign in to comment.