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

Support dates in widgets such as the min/max values in sliders #3711

Open
joelostblom opened this issue Dec 11, 2024 · 2 comments
Open

Support dates in widgets such as the min/max values in sliders #3711

joelostblom opened this issue Dec 11, 2024 · 2 comments
Labels
enhancement vega: vega-lite Requires upstream action in `vega-lite`

Comments

@joelostblom
Copy link
Contributor

joelostblom commented Dec 11, 2024

What is your suggestion?

I was playing around with the much improved date support from #3653 and noticed that the min/max parameters in sliders don't accept date values:

import altair as alt
import pandas as pd
import numpy as np
from datetime import date

datelist = pd.date_range(end='2024-10-08', periods=100).to_pydatetime()

rand = np.random.RandomState(42)

df = pd.DataFrame({
    'xval': datelist,
    'yval': rand.randn(100).cumsum(),
})

slider = alt.binding_range(name='cutoff:', min=min(datelist), max=max(datelist))
selector = alt.selection_point(
    name="SelectorName",
    fields=['cutoff'],
    bind=slider,
    value=[{"cutoff": date(2020, 5, 5)}]
)

alt.Chart(df).mark_point().encode(
    x='xval',
    y='yval',
    opacity=alt.condition(
        'toDate(datum.xval) < SelectorName.cutoff[0]',
        alt.value(1), alt.value(0)
    )
).add_params(
    selector
)
SchemaValidationError: Multiple errors were found.

Error 1: '{'year': 2024, 'month': 10, 'date': 8}' is an invalid value for `max`. Valid values are of type 'number'.

Error 2: '{'year': 2024, 'month': 7, 'date': 1}' is an invalid value for `min`. Valid values are of type 'number'.

It would be convenient if these worked with dates instead of just timstamps. If I don't include min and max at all, the value of the date displays as expected initially (although formatting is a bit verbose, maybe that should be configurable or at least limit to the same level of detail as the underlying date if possible), but disappears when the slider is moved (since the slider range is the default 1 to 100).

Image

Have you considered any alternative solutions?

I don't think there is an alternative unless we want to convert everything to timestamps again as in this SO answer.

@dangotbanned
Copy link
Member

dangotbanned commented Dec 11, 2024

@joelostblom you might be able to get this working by triggering the Expression conversion instead of SchemaBase?
I'm not 100% sure what that would look like for your example though

We might be limited here by vega-lite though, since that SchemaValidationError expects an int

@dangotbanned
Copy link
Member

Minimal Repro

https://altair-viz.github.io/user_guide/generated/api/altair.binding_range.html#altair.binding_range

import datetime as dt
import altair as alt

# SchemaBase
alt.binding_range(min=dt.date(2000, 1, 1))
# SchemaValidationError: '{'year': 2000, 'month': 1, 'date': 1}' is an invalid value for `min`. Valid values are of type 'number'.

# Expression
alt.binding_range(min=alt.expr.parseFloat(dt.date(2000, 1, 1)))
# SchemaValidationError: 'parseFloat(datetime(2000,0,1))' is an invalid value for `min`. Valid values are of type 'number'.

@dangotbanned dangotbanned added the vega: vega-lite Requires upstream action in `vega-lite` label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement vega: vega-lite Requires upstream action in `vega-lite`
Projects
None yet
Development

No branches or pull requests

2 participants