Skip to content

Commit

Permalink
fix(rust, python): deal with empty structs (#6039)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jan 4, 2023
1 parent 9f01023 commit 977c552
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
3 changes: 3 additions & 0 deletions polars/polars-core/src/chunked_array/logical/struct_/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl StructChunked {
}
}
Ok(Self::new_unchecked(name, &new_fields))
} else if fields.is_empty() {
let fields = &[Series::full_null("", 1, &DataType::Null)];
Ok(Self::new_unchecked(name, fields))
} else {
Ok(Self::new_unchecked(name, fields))
}
Expand Down
1 change: 1 addition & 0 deletions polars/polars-core/src/chunked_array/ops/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub(crate) unsafe fn arr_to_any_value<'a>(
let arr = &*(arr as *const dyn Array as *const FixedSizeBinaryArray);
PolarsExtension::arr_to_av(arr, idx)
}
DataType::Null => AnyValue::Null,
dt => panic!("not implemented for {dt:?}"),
}
}
Expand Down
21 changes: 0 additions & 21 deletions polars/polars-lazy/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,24 +2009,3 @@ fn test_partitioned_gb_ternary() -> PolarsResult<()> {

Ok(())
}

#[test]
fn test_foo() -> PolarsResult<()> {
let df = df![
"a" => [2, 2],
"b" => [1, 2]
]?;

let out = df
.lazy()
.groupby([col("a")])
.agg([
(col("a").filter(col("b").eq(0)).diff(1, Default::default()) * lit(100))
.diff(1, Default::default())
.alias("foo"),
])
.collect();
dbg!(out);

Ok(())
}
12 changes: 12 additions & 0 deletions py-polars/tests/unit/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,15 @@ def test_struct_categorical_5843() -> None:
{"foo": "c", "counts": 1},
]
}


def test_struct_empty() -> None:
# List<struct>
df = pl.DataFrame({"a": [[{}]]})
assert df.to_dict(False) == {"a": [[{"": None}]]}
# Struct one not empty
df = pl.DataFrame({"a": [[{}, {"a": 10}]]})
assert df.to_dict(False) == {"a": [[{"a": None}, {"a": 10}]]}
# Empty struct
df = pl.DataFrame({"a": [{}]})
assert df.to_dict(False) == {"a": [{"": None}]}

0 comments on commit 977c552

Please sign in to comment.