Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support &, |, ~ on all ...Predicate classes #3668

Merged
merged 10 commits into from
Nov 3, 2024

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Nov 1, 2024

Resolves #695

Description

This PR enables the use of logical composition operators (&, |, ~) directly on all 12 of:

...Predicate classes

  • core.FieldEqualPredicate
  • core.FieldGTEPredicate
  • core.FieldGTPredicate
  • core.FieldLTEPredicate
  • core.FieldLTPredicate
  • core.FieldOneOfPredicate
  • core.FieldRangePredicate
  • core.FieldValidPredicate
  • core.LogicalAndPredicate
  • core.LogicalOrPredicate
  • core.LogicalNotPredicate
  • core.ParameterPredicate

It appears this functionality has been desired for some time, but never implemented fully.

api.SelectionPredicateComposition already contained the code required to get this working - this PR moves that to core.PredicateComposition.

The change is fully backwards compatible, turning the old class into an alias (1dc15b7)

This also provides a cleaner solution to (#3664 (comment)) - than any of the alternatives documented there

Example

import altair as alt
from vega_datasets import data

source = data.disasters()
columns_sorted = ["Drought", "Epidemic", "Earthquake", "Flood"]

base = (
    alt.Chart(source, height=200)
    .mark_line(interpolate="monotone")
    .encode(
        x=alt.X("Year:Q").axis(format="d"),
        color="Entity",
        column="Entity",
        y="sum(Deaths)",
    )
)

chart_old = base.transform_filter(
    {
        "and": [
            alt.FieldOneOfPredicate(field="Entity", oneOf=columns_sorted),
            alt.FieldRangePredicate(field="Year", range=[1900, 2000]),
        ]
    }
)

chart_new = base.transform_filter(
    alt.FieldOneOfPredicate(field="Entity", oneOf=columns_sorted)
    & alt.FieldRangePredicate(field="Year", range=[1900, 2000])
)
chart_old & chart_new
Output

predicate-composition-dunder

User Guide

image

Tasks

Other Future Work

Related

- Initial use-case is `SelectionPredicateComposition` -> `PredicateComposition`
- Something similar might be a good idea for `Expression` -> `Expr` in #3616
This functionality is now part of its parent `core.PredicateComposition`
…icateComposition`

- Ensures backwards compat if anyone was using this directly
- Existing tests not breaking is confirmation that this is safe
@dangotbanned dangotbanned marked this pull request as ready for review November 1, 2024 16:35
The original only demonstrated `~`,  but I've now included `&`.
It also shows mixing `datum` with `PredicateComposition`
doc/user_guide/transform/filter.rst Show resolved Hide resolved
@dangotbanned dangotbanned merged commit 64b2d33 into main Nov 3, 2024
21 checks passed
@dangotbanned dangotbanned deleted the predicate-composition-dunder branch November 3, 2024 20:56
dangotbanned added a commit that referenced this pull request Nov 3, 2024
dangotbanned added a commit to dangotbanned/altair that referenced this pull request Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better access to logical operand predicates
2 participants