posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.43k stars 48 forks source link

Polars selectors error with the `columns=` arg of `GT.cols_hide()` #313

Closed rich-iannone closed 2 months ago

rich-iannone commented 2 months ago

Polars selectors can be used in a wide range of GT methods within the columns= argument. While the fmt_*() and cols_*() methods have good support for this, using a Polars selector fails with cols_hide(). Here is some code that fails with this approach:

from great_tables import GT, exibble
import polars as pl
import polars.selectors as cs

exibble_pl = pl.from_pandas(exibble)

(
  GT(exibble_pl)
  .cols_hide(columns=cs.starts_with("date"))
)

Stack trace on my machine:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ...great-tables/playground.qmd:8
      3 import polars.selectors as cs
      5 exibble_pl = pl.from_pandas(exibble)
      7 (
----> 8   GT(exibble_pl)
      9   .cols_hide(columns=cs.starts_with("date"))
     10 )

File ...great-tables/great_tables/_spanners.py:470, in cols_hide(data, columns)
    468 if not len(sel_cols):
    469     raise Exception("No columns selected.")
--> 470 elif not all([col in vars for col in columns]):
    471     raise ValueError("All `columns` must exist and be visible in the input `data` table.")
    473 # New boxhead with hidden columns

TypeError: '_selector_proxy_' object is not iterable