Skip to content

Commit

Permalink
groupby apply: use inner type to infer dtype (#3955)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 9, 2022
1 parent c4231a5 commit 507da45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions polars/polars-lazy/src/physical_plan/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,16 @@ impl PhysicalExpr for ApplyExpr {
// collection of empty list leads to a null dtype
// see: #3687
if s.len() == 0 {
let s = self.function.call_udf(&mut [s.clone()])?;
let ca = ListChunked::full(s.name(), &s, 0);
// create input for the function to determine the output dtype
// see #3946
let agg = ac.aggregated();
let agg = agg.list().unwrap();
let input_dtype = agg.inner_dtype();

let input = Series::full_null("", 0, &input_dtype);

let output = self.function.call_udf(&mut [input])?;
let ca = ListChunked::full(ac.series().name(), &output, 0);
return Ok(self.finish_apply_groups(ac, ca));
}

Expand Down
23 changes: 23 additions & 0 deletions polars/tests/it/lazy/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,26 @@ fn test_expand_list() -> Result<()> {

Ok(())
}

#[test]
fn test_apply_groups_empty() -> Result<()> {
let df = df![
"id" => [1, 1],
"hi" => ["here", "here"]
]?;

let out = df
.lazy()
.filter(col("id").eq(lit(2)))
.groupby([col("id")])
.agg([col("hi").drop_nulls().unique()])
.collect()?;

assert_eq!(
out.dtypes(),
&[DataType::Int32, DataType::List(Box::new(DataType::Utf8))]
);
assert_eq!(out.shape(), (0, 2));

Ok(())
}

0 comments on commit 507da45

Please sign in to comment.