Skip to content

Commit

Permalink
fix explode offsets for empty lists (#4005)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 13, 2022
1 parent 4dff8f8 commit 6a64d6f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
5 changes: 3 additions & 2 deletions polars/polars-core/src/chunked_array/ops/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ pub(crate) fn offsets_to_indexes(offsets: &[i64], capacity: usize) -> Vec<IdxSiz
}
previous_empty = previous_offset == *offset;
}
// last appended index.
let last_idx = idx[idx.len() - 1];
// undo latest increment
last_idx -= 1;

for _ in 0..(capacity - count as usize) {
idx.push(last_idx);
}
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 @@ -49,7 +49,7 @@ impl DataFrame {

for (i, s) in columns.iter().enumerate() {
// Safety:
// offsets are not take longer than the Series.
// offsets don't have indices exceeding Series length.
if let Ok((exploded, offsets)) = get_exploded(s) {
let col_idx = self.check_name_to_idx(s.name())?;

Expand Down
3 changes: 2 additions & 1 deletion polars/polars-core/src/series/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::series::arithmetic::coerce_lhs_rhs;
feature = "dtype-duration",
feature = "dtype-datetime",
feature = "dtype-date",
feature = "dtype-time"
feature = "dtype-time",
feature = "dtype-struct"
))]
use std::ops::Deref;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl PredicatePushDown {
lp_arena: &mut Arena<ALogicalPlan>,
expr_arena: &mut Arena<AExpr>,
) -> Result<ALogicalPlan> {
let acc_predicates = PlHashMap::with_capacity(32);
let acc_predicates = PlHashMap::new();
self.push_down(logical_plan, acc_predicates, lp_arena, expr_arena)
}
}
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ def test_explode_empty_df_3902() -> None:
}
)
assert df.explode("second").frame_equal(expected)


def test_explode_empty_list_4003() -> None:
df = pl.DataFrame(
[
{"id": 1, "nested": []},
{"id": 2, "nested": [1]},
{"id": 3, "nested": [2]},
]
)
assert df.explode("nested").to_dict(False) == {
"id": [1, 2, 3],
"nested": [None, 1, 2],
}

0 comments on commit 6a64d6f

Please sign in to comment.