Skip to content

Add having or filter to GroupBy #23290

@borchero

Description

@borchero

Description

I recently came across the need to filter groups by some condition and was surprised to find that polars does not natively support this.

Currently, one has to do something like the following to filter for groups that satisfy a particular property:

df
  .group_by("col")
  .agg(at_least_two=pl.len() >= 2)
  .filter("at_least_two")
  .drop("at_least_two")

This would be much more ergonomic if there was a SQL-like having construct:

df
  .group_by("col")
  .having(pl.len() >= 2)

While I personally like having as it distinguishes the context from the filter method, pandas calls such a method filter (docs).

Would you be open to adding something like this to polars?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-apiArea: changes to the public APIacceptedReady for implementationenhancementNew feature or an improvement of an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions