great-tables icon indicating copy to clipboard operation
great-tables copied to clipboard

feat: add `tab_spanner_delim()` method

Open jrycw opened this issue 9 months ago • 6 comments

Fix issue: #582.

Hello team:

This PR sets up a scaffold for tab_spanner_delim().

Basic Logic:

  1. Use str.split or str.rsplit to split column names, corresponding to delimi= and limit= parameters.
  2. Iterate through selected columns, splitting each name and recording its level and root_col (original column).
  3. Process the recorded info:
    • If level != 0, call tab_spanner().
    • If level == 0, call cols_label().

Example:

import polars as pl
from great_tables import GT

data = {
    "province.NL_ZH.pop": [1, 2, 3],
    "province.NL_ZH.gdp": [4, 5, 6],
    "province.NL_NH.pop": [7, 8, 9],
    "province.NL_NH.gdp": [10, 11, 12],
}

gt = GT(pl.DataFrame(data))
gt.tab_spanner_delim()

image

gt.tab_spanner_delim(limit=1)

image

gt.tab_spanner_delim(split="first", limit=1)

image

Remaining Tasks:

  • [ ] The reverse= logic is not implemented—need the team's help to clarify the expected behavior.
  • [ ] Docs are ported from {gt} but need refinement.
  • [ ] Tests are missing since the logic isn't fully settled yet.

jrycw avatar Feb 12 '25 16:02 jrycw