Skip to content

bar charts & stack = True with null values silently drop data #3931

@saucoide

Description

@saucoide

What happened?

When creating a stacked bar chart in a marimo notebook, if some of the rows contains nulls, the results are very unexpected:

import marimo as mo
import altair as alt
import polars as pl


df = pl.from_dict(
    {
        "Name": [
            "chevrolet chevelle malibu",
            "buick skylark 320",
            "plymouth satellite",
            "amc rebel sst",
            "ford torino",
        ],
        "Cylinders": ["2c", "2c", "4c", "4c", "6c"],
        "Horsepower": [None, 165, 150, 150, 140],
    }
)
df

for stack in (True, False):
    _chart = (
        alt.Chart(df)
        .mark_bar()
        .encode(
            x=alt.X(field="Cylinders", type="nominal"),
            y=alt.Y(
                field="Horsepower", type="quantitative", stack=stack, aggregate="sum"
            ),
            color=alt.Color(field="Name", type="nominal"),
        )
    )
    mo.output.append(_chart)
Image

when stack=True, i'm guessing it attempts to add the None to the value, but instead of failing, it turns everything into None

What would you like to happen instead?

either an exception or more gracefully ignoring the None entries, but probably an exception feels more correct?

Which version of Altair are you using?

6.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triageBug report needs maintainer response

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions