great-tables
great-tables copied to clipboard
feat: implement row selector `every_n_row()`
Hello team,
I would like to propose adding a row selector feature that allows users to select rows at every n interval. This could be particularly useful for styling tables. For example, alternating row colors could be achieved like this:
from great_tables import GT, every_n_row, style, loc
from great_tables.data import exibble
(
GT(exibble)
.tab_style(style=style.fill("lightgray"), locations=loc.body(rows=every_n_row(2)))
.tab_style(style=style.fill("darkgray"), locations=loc.body(rows=every_n_row(2, 1)))
.opt_stylize(color="cyan", style=6)
)
You can see my draft implementation here.
The core idea is to insert a check before verifying whether expr is callable — specifically, checking if expr is an instance of GTRowSelector. This ensures compatibility across different dataframe types. Otherwise, we might need to refactor the logic in eval_transform() for Polars, since it currently expects a Polars expression.
If the team supports this idea, I’d be happy to submit a PR.
One thing to note: the current draft does not work with pyarrow, and I haven’t yet identified the reason.