-
Notifications
You must be signed in to change notification settings - Fork 111
Description
Prework
- Read and agree to the code of conduct and contributing guidelines.
- If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
Description
When running the gt_plt_bar_stack example from gt-extras docs with Polars, the gt_plt_bar_stack function throws a TypeError: the truth value of a Series is ambiguous but works correctly with Pandas data frames. While the issue is thrown when using gt-extras, I think it's possibly best addressed as an inconsistency between the _get_cell() implementations for Pandas vs. Polars in great-tables. Full error message and MRE below.
Show error message
File "/Users/kriarm/code/great-tables/great_tables/_export.py", line 452, in save
html_content = as_raw_html(self)
^^^^^^^^^^^^^^^^^
File "/Users/kriarm/code/great-tables/great_tables/_export.py", line 229, in as_raw_html
built_table = self._build_data(context="html")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/kriarm/code/great-tables/great_tables/gt.py", line 319, in _build_data
built = self._render_formats(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/kriarm/code/great-tables/great_tables/gt.py", line 312, in _render_formats
new_body.render_formats(self._tbl_data, self._formats, context)
File "/Users/kriarm/code/great-tables/great_tables/_gt_data.py", line 189, in render_formats
result = eval_func(_get_cell(data_tbl, row, col))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/kriarm/code/gt-extras/gt_extras/plotting.py", line 2030, in <lambda>
lambda x: _make_bar_stack_svg(
^^^^^^^^^^^^^^^^^^^^
File "/Users/kriarm/code/gt-extras/gt_extras/plotting.py", line 1941, in _make_bar_stack_svg
if not values:
^^^^^^
File "/Users/kriarm/project/gtextras_bug/.venv/lib/python3.12/site-packages/polars/series/series.py", line 666, in __bool__
raise TypeError(msg)
TypeError: the truth value of a Series is ambiguous
Here are some things you might want to try:
- instead of `if s`, use `if not s.is_empty()`
- instead of `s1 and s2`, use `s1 & s2`Problem: To my checking, when a cell entry is a container data type, PlDataFrame._get_cell() returns a pl.Series, but _make_bar_stack_svg expects list[float] and attempts to evaluate if not values on line 1944, which fails for Polars Series. In contrast, PdDataFrame._get_cell() passes a list[float] which works correctly.
Polars code returns a pl.Series (if cell entry is a container data type):
great-tables/great_tables/_tbl_data.py
Lines 221 to 224 in 140adee
| @_get_cell.register(PlDataFrame) | |
| def _(data: Any, row: int, column: str) -> Any: | |
| return data[column][row] | |
Pandas code returns a list of values:
great-tables/great_tables/_tbl_data.py
Lines 226 to 233 in 140adee
| @_get_cell.register(PdDataFrame) | |
| def _(data: Any, row: int, col: str) -> Any: | |
| col_ii = data.columns.get_loc(col) | |
| if not isinstance(col_ii, int): | |
| raise ValueError("Column named " + col + " matches multiple columns.") | |
| return data.iloc[row, col_ii] |
Possible fixes:
- Modify
gte.plotting._make_bar_stack_svgto acceptpl.Seriesasvalues, or - Update
PlDataFrame._get_cell()upstream in great-tables to return a list instead of a Series (matching Pandas behavior)
I was not entirely sure which approach is best suited. I'm happy to work on a PR to address this issue if that would be helpful!
Reproducible example
This is basically the example from gt-extras docs. If this code is placed into test_bug.py and ran as python -m test_bug it should give the error above. The error should go away if Pandas is used instead of Polars.
import polars as pl
from great_tables import GT
import gt_extras as gte
df = pl.DataFrame({
"x": ["Example A", "Example B", "Example C"],
"col": [
[10, 40, 50],
[30, 30, 40],
[50, 20, 30],
],
})
gt = (
GT(df)
.pipe(
gte.gt_plt_bar_stack,
column="col",
palette=["red", "grey", "black"],
labels=["Group 1", "Group 2", "Group 3"],
width=200,
)
)
gt.save(file="./plt_bar_stack_table_pl.png")Expected result
The table is rendered and saved to disk.
Development environment
- Operating System: macOS Sequoia 15.5 (M1 Mac)
- great_tables version: 0.18.1.dev13+g140adeea2.d20251006 (also reproduced with stable 0.18.0)
- gt-extras version: 0.0.9.dev28+g704e7b13f (also reproduced with stable 0.0.9)
- Polars: 1.34.0
- Pandas: 2.3.3
- Python: 3.12