vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.36k stars 793 forks source link

tooltip throws error for Categorical variable #2879

Closed jbloom closed 1 year ago

jbloom commented 1 year ago

The following code used to work in recent versions of altair including the current in-development branch

But by commit f8912bad75d4247ab7 this code throws an error.

The problem appears to be that specifying a variable for a tooltip without a type throws an error if the variable in a pandas.Categorical.

Specifically, this code:

import altair as alt
import pandas as pd

df = (
    pd.DataFrame({"x": [1, 2], "y": [1, 2], "note": ["a", "b"]})
    .assign(note=lambda x: pd.Categorical(x["note"], ordered=True))
)

alt.Chart(df).encode(x="x", y="y", tooltip=["x", "y", "note"]).mark_point()

Now throws an error.

The error is:

---------------------------------------------------------------------------
SchemaValidationError                     Traceback (most recent call last)
File /fh/fast/bloom_j/software/miniconda3/envs/dms-vep-pipeline/lib/python3.11/site-packages/altair/vegalite/v5/api.py:2194, in Chart.to_dict(self, *args, **kwargs)
   2192     copy.data = core.InlineData(values=[{}])
   2193     return super(Chart, copy).to_dict(*args, **kwargs)
-> 2194 return super().to_dict(*args, **kwargs)

File /fh/fast/bloom_j/software/miniconda3/envs/dms-vep-pipeline/lib/python3.11/site-packages/altair/vegalite/v5/api.py:559, in TopLevelMixin.to_dict(self, *args, **kwargs)
    556 context["top_level"] = False
    557 kwargs["context"] = context
--> 559 dct = super(TopLevelMixin, copy).to_dict(*args, **kwargs)
    561 # TODO: following entries are added after validation. Should they be validated?
    562 if is_top_level:
    563     # since this is top-level we add $schema if it's missing

File /fh/fast/bloom_j/software/miniconda3/envs/dms-vep-pipeline/lib/python3.11/site-packages/altair/utils/schemapi.py:422, in SchemaBase.to_dict(self, validate, ignore, context)
    420         self.validate(result)
    421     except jsonschema.ValidationError as err:
--> 422         raise SchemaValidationError(self, err)
    423 return result

SchemaValidationError: Invalid specification

        altair.vegalite.v5.api.Chart->0, validating 'type'

        [{'field': 'x', 'type': 'quantitative'}, {'field': 'y', 'type': 'quantitative'}, {'field': 'note', 'type': 'ordinal', 'sort': ['a', 'b']}] is not of type 'object'
        [{'field': 'x', 'type': 'quantitative'}, {'field': 'y', 'type': 'quantitative'}, {'field': 'note', 'type': 'ordinal', 'sort': ['a', 'b']}] is not of type 'object'
        Additional properties are not allowed ('sort' was unexpected)
        [{'field': 'x', 'type': 'quantitative'}, {'field': 'y', 'type': 'quantitative'}, {'field': 'note', 'type': 'ordinal', 'sort': ['a', 'b']}] is not of type 'null'
mattijn commented 1 year ago

Thanks for raising @jbloom! That commit and corresponding PR you refer to appears to be documentation only.

It is maybe because of #2522? Unintended side-effect @joelostblom?

jbloom commented 1 year ago

Sorry, I didn't mean that commit caused the problem (I haven't tried to trace back). Just that I am using the code base at that commit and getting the problem. Previously I was using a commit from ~year earlier and didn't have the problem.

mattijn commented 1 year ago

No problem. Thanks for clarifying!