{"record":{"id":"e50610b37c900193","repo":"pola-rs/polars","slug":"not-yet-implemented-apply-cannot-be-used-with","errorCode":null,"errorMessage":"not yet implemented: `apply` cannot be used with `having` predicates","messagePattern":"not yet implemented: `apply` cannot be used with `having` predicates","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-lazy/src/frame/mod.rs","lineNumber":2108,"sourceCode":"            .collect::<Vec<_>>();\n\n        self.agg([all().as_expr().tail(n)]).explode_impl(\n            all() - by_name(keys.iter().cloned(), false, false),\n            ExplodeOptions {\n                empty_as_null: true,\n                keep_nulls: true,\n            },\n            true,\n        )\n    }\n\n    /// Apply a function over the groups as a new DataFrame.\n    ///\n    /// **It is not recommended that you use this as materializing the DataFrame is very\n    /// expensive.**\n    pub fn apply(self, f: PlanCallback<DataFrame, DataFrame>, schema: SchemaRef) -> LazyFrame {\n        if !self.predicates.is_empty() {\n            panic!(\"not yet implemented: `apply` cannot be used with `having` predicates\");\n        }\n\n        #[cfg(feature = \"dynamic_group_by\")]\n        let options = GroupbyOptions {\n            dynamic: self.dynamic_options,\n            rolling: self.rolling_options,\n            slice: None,\n        };\n\n        #[cfg(not(feature = \"dynamic_group_by\"))]\n        let options = GroupbyOptions { slice: None };\n\n        let lp = DslPlan::GroupBy {\n            input: Arc::new(self.logical_plan),\n            keys: self.keys,\n            predicates: vec![],\n            aggs: vec![],\n            apply: Some((f, schema)),","sourceCodeStart":2090,"sourceCodeEnd":2126,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-lazy/src/frame/mod.rs#L2090-L2126","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before\nlf.clone().having(col(\"a\").gt(1)).group_by([col(\"k\")]).apply(f, schema);\n// after\nlf.group_by([col(\"k\")]).agg([col(\"a\").sum()]).filter(col(\"a_sum\").gt(1));","handlingStrategy":"validation","validationCode":"// before calling apply, ensure no having predicates are set on the GroupBy\n// in polars API terms: avoid combining .having(...) with .apply(); prefer expression aggregations","typeGuard":null,"tryCatchPattern":"// this is a plan-construction panic, not a runtime Result — restructure code instead\ncatch_unwind is not recommended; assert !predicates.is_empty() before apply in debug builds","preventionTips":["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."],"tags":["polars-lazy","group-by","not-implemented","panic"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}