pola-rs/polars · error
not yet implemented: `apply` cannot be used with `having`…
Error message
not yet implemented: `apply` cannot be used with `having` predicates
What it means
LazyFrame's group_by `apply` (which materializes each group as a full DataFrame and applies a callback) explicitly refuses to run when the frame has `having` predicates attached, because the interaction is not implemented. It panics with a not-yet-implemented message at plan construction time.
Solutions
- Move the filtering logic into the apply callback itself (filter each group's DataFrame inside f).
- Replace `apply` with expression-based agg + filter, which composes with having predicates.
- Apply the predicate filtering after the apply via LazyFrame::filter on the result.
- Avoid `apply` entirely (it materializes groups; expressions are faster).
Example fix
// before
lf.clone().having(col("a").gt(1)).group_by([col("k")]).apply(f, schema);
// after
lf.group_by([col("k")]).agg([col("a").sum()]).filter(col("a_sum").gt(1)); Defensive patterns
Strategy: validation
Validate before calling
// before calling apply, ensure no having predicates are set on the GroupBy // in polars API terms: avoid combining .having(...) with .apply(); prefer expression aggregations
Try / catch
// this is a plan-construction panic, not a runtime Result — restructure code instead catch_unwind is not recommended; assert !predicates.is_empty() before apply in debug builds
Prevention
- Prefer expression-based group_by().agg() + filter over group-wise apply.
- If custom logic is needed, do per-group filtering inside the callback.
- Review lazy plans for having predicates when migrating legacy apply code.
When it happens
Trigger: Building a lazy plan where GroupBy::apply (PlanCallback<DataFrame, DataFrame>) is called on a frame that already accumulated predicates via `having(...)` in polars-lazy (crates/polars-lazy/src/frame/mod.rs:2108).
Common situations: Combining SQL-style HAVING clauses (e.g. from the SQL interface or .having() API) with a group-wise apply/map for custom aggregation logic; migrating legacy apply-based code after adding filtering.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- activate feature dynamic_group_by
- activate feature dynamic_group_by
- groups are index not slices
- groups are slices not index
- Writing to JSON
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/e50610b37c900193.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-lazy/src/frame/mod.rs:2108
.collect::<Vec<_>>();
self.agg([all().as_expr().tail(n)]).explode_impl(
all() - by_name(keys.iter().cloned(), false, false),
ExplodeOptions {
empty_as_null: true,
keep_nulls: true,
},
true,
)
}
/// Apply a function over the groups as a new DataFrame.
///
/// **It is not recommended that you use this as materializing the DataFrame is very
/// expensive.**
pub fn apply(self, f: PlanCallback<DataFrame, DataFrame>, schema: SchemaRef) -> LazyFrame {
if !self.predicates.is_empty() {
panic!("not yet implemented: `apply` cannot be used with `having` predicates");
}
#[cfg(feature = "dynamic_group_by")]
let options = GroupbyOptions {
dynamic: self.dynamic_options,
rolling: self.rolling_options,
slice: None,
};
#[cfg(not(feature = "dynamic_group_by"))]
let options = GroupbyOptions { slice: None };
let lp = DslPlan::GroupBy {
input: Arc::new(self.logical_plan),
keys: self.keys,
predicates: vec![],
aggs: vec![],
apply: Some((f, schema)),View on GitHub (pinned to fe841f959e)