Skip to content

gt_plt_bar_stack not working with polars DataFrame #167

@danilogalisteu

Description

@danilogalisteu

Prework

Description

gt_plt_bar_stack fails when using a simple polars DataFrame. The "column containing lists of numeric values" usually has a data type that is not a simple list, causing a TypeError on a boolean test inside _make_bar_stack_svg.

TypeError: the truth value of a Series is ambiguous

Reproducible example

  • Post a minimal reproducible example (MRE) so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the Style Guide for Python Code.

An example is presented below and available for running on GoogleColab. Different options for DataFrame creation are commented out for testing. The code was adapted from the original example.

import pandas as pd
import polars as pl
import gt_extras as gte
from great_tables import GT


df_dict = {
    "x": ["Example A", "Example B", "Example C"], 
    "col": [
        [10, 40, 50],
        [30, 30, 40],
        [50, 20, 30],
    ],
}

## NOT WORKING
df = pl.DataFrame(df_dict)
# df = pl.DataFrame(df_dict).to_pandas()

## WORKING
# df = pd.DataFrame(df_dict)
# df = pd.DataFrame.from_dict(pl.DataFrame(df_dict).to_dict(as_series=False))
# df = pl.DataFrame(df_dict).to_pandas().assign(col=lambda x: x["col"].apply(lambda v: v.tolist()))

GT(df).pipe(
    gte.gt_plt_bar_stack,
    column="col",
    palette=["red", "grey", "black"],
    labels=["Group 1", "Group 2", "Group 3"],
    width=200,
)

Expected result

The expected result is the simple bar stack plot seen in the original example.

Development environment

  • Operating System: Linux (Ubuntu 24.04 and GoogleColab notebook)
  • great-tables Version: 0.20.0
  • gt_extras Version: 0.0.8
  • pandas Version: 2.2.2
  • polars Version: 1.25.2

Additional context

I first spotted the issue when creating a column for gt_plt_bar_stack using polars.concat_list. I found that the resulting type was not a simple list even after converting with .to_pandas().

ADDED: In my testing, the polars column type is 'list[i64]' which is not the same as a Python list and, when converted to pandas, the value type ends up being 'numpy.ndarray'. Both of these types fail the check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions