mckinsey / vizro

Vizro is a toolkit for creating modular data visualization applications.
https://vizro.readthedocs.io/en/stable/
Apache License 2.0
2.72k stars 142 forks source link

Numerical values in selector with non-numerical column don't work #869

Open antonymilne opened 1 week ago

antonymilne commented 1 week ago

Which package?

vizro

Package version

0.1.26

Description

[!NOTE]
Not urgent, let's wait until after pydantic v2 to fix.

If a column contains only numerical values but is object type (as can happen when you load data from a file) then it's correctly considered a categorical variable so given selector vm.Dropdown by default. However, filtering on it won't work.

The root cause is:

  1. pydantic model has OptionsType = Union[list[StrictBool], list[float], list[str], list[date], list[OptionsDictType]] and casts options to float
  2. _filter_isin is then comparing series of objects to float, which doesn't return anything

This is a problem with at least Dropdown, Checklist, RadioItems and probably affects more than just numerical types.

Possible fixes:

How to Reproduce

PyCafe snippet

import vizro.models as vm
import pandas as pd
from vizro import Vizro

from vizro.tables import dash_ag_grid

df = pd.DataFrame({"my_column": ["1", "2", "3"]})
# Uncomment this line to see the bug
# df["my_column"] = df["my_column"].astype(int)

page = vm.Page(
    title="Bug",
    components=[vm.AgGrid(figure=dash_ag_grid(data_frame=df))],
    controls=[vm.Filter(column="my_column", selector=vm.RadioItems())],
)

dashboard = vm.Dashboard(pages=[page])

Vizro().build(dashboard).run()

Output

No response

Code of Conduct