Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/functions/python_udf.rs
6940 views
1
use super::*;
2
3
pub(super) fn call_python_udf(
4
function: &PythonFunction,
5
df: DataFrame,
6
validate_output: bool,
7
opt_schema: Option<SchemaRef>,
8
) -> PolarsResult<DataFrame> {
9
let expected_schema = if let Some(schema) = opt_schema {
10
Some(schema)
11
}
12
// only materialize if we validate the output
13
else if validate_output {
14
Some(df.schema().clone())
15
}
16
// do not materialize the schema, we will ignore it.
17
else {
18
None
19
};
20
let out = DataFrameUdf::call_udf(function, df)?;
21
22
if validate_output {
23
let output_schema = out.schema();
24
let expected = expected_schema.unwrap();
25
if &expected != output_schema {
26
return Err(PolarsError::ComputeError(
27
format!(
28
"The output schema of 'LazyFrame.map' is incorrect. Expected: {expected:?}\n\
29
Got: {output_schema:?}"
30
)
31
.into(),
32
));
33
}
34
}
35
Ok(out)
36
}
37
38