Skip to content

Commit

Permalink
docs: Update .transform_filter() docstring
Browse files Browse the repository at this point in the history
- Builds on the style introdcued for `alt.when`
- Shows a few specific kinds of predicates - due to the prior doc listing 5

#3657
  • Loading branch information
dangotbanned committed Nov 4, 2024
1 parent be63d4e commit 7a0cc42
Showing 1 changed file with 81 additions and 14 deletions.
95 changes: 81 additions & 14 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2987,8 +2987,6 @@ def transform_extent(
"""
return self._add_transform(core.ExtentTransform(extent=extent, param=param))

# TODO: Update docstring
# # E.g. {'not': alt.FieldRangePredicate(field='year', range=[1950, 1960])}
def transform_filter(
self,
predicate: Optional[_PredicateType] = Undefined,
Expand All @@ -2997,22 +2995,91 @@ def transform_filter(
**constraints: _FieldEqualType,
) -> Self:
"""
Add a :class:`FilterTransform` to the schema.
Add a :class:`FilterTransform` to the spec.
The resulting predicate is an ``&`` reduction over ``predicate`` and optional ``*``, ``**``, arguments.
Parameters
----------
filter : a filter expression or :class:`PredicateComposition`
The `filter` property must be one of the predicate definitions:
(1) a string or alt.expr expression
(2) a range predicate
(3) a selection predicate
(4) a logical operand combining (1)-(3)
(5) a Selection object
predicate
A selection or test predicate. ``str`` input will be treated as a test operand.
*more_predicates
Additional predicates, restricted to types supporting ``&``.
empty
For selection parameters, the predicate of empty selections returns ``True`` by default.
Override this behavior, with ``empty=False``.
Returns
-------
self : Chart object
returns chart to allow for chaining
.. note::
When ``predicate`` is a ``Parameter`` that is used more than once,
``self.transform_filter(..., empty=...)`` provides granular control for each occurrence.
**constraints
Specify `Field Equal Predicate`_'s.
Shortcut for ``alt.datum.field_name == value``, see examples for usage.
Warns
-----
AltairDeprecationWarning
If called using ``filter`` as a keyword argument.
See Also
--------
alt.when : Uses a similar syntax for defining conditional values.
Notes
-----
- Directly inspired by the syntax used in `polars.DataFrame.filter`_.
.. _Field Equal Predicate:
https://vega.github.io/vega-lite/docs/predicate.html#equal-predicate
.. _polars.DataFrame.filter:
https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.filter.html
Examples
--------
Setting up a common chart::
import altair as alt
from altair import datum
from vega_datasets import data
source = data.population.url
chart = (
alt.Chart(source)
.mark_line()
.encode(
x="age:O",
y="sum(people):Q",
color=alt.Color("year:O").legend(symbolType="square"),
)
)
chart
Singular predicates can be expressed via ``datum``::
chart.transform_filter(datum.year <= 1980)
We can also use parameter selections directly::
selection = alt.selection_point(encodings=["color"], bind="legend")
chart.transform_filter(selection).add_params(selection)
Or using field predicates::
between_1950_60 = alt.FieldRangePredicate(field="year", range=[1950, 1960])
chart.transform_filter(between_1950_60) | chart.transform_filter(~between_1950_60)
Predicates can be composed together using logical operands::
chart.transform_filter(between_1950_60 | (datum.year == 1850))
Predicates passed as positional arguments will be reduced with ``&``::
chart.transform_filter(datum.year > 1980, datum.age != 90)
Using keyword-argument ``constraints`` can simplify compositions like::
verbose_composition = chart.transform_filter((datum.year == 2000) & (datum.sex == 1))
chart.transform_filter(year=2000, sex=1)
"""
if depr_filter := constraints.pop("filter", None):
utils.deprecated_warn(
Expand Down

0 comments on commit 7a0cc42

Please sign in to comment.