Skip to content

Fix: Modify the group labels when render_formats() is called#769

Merged
rich-iannone merged 7 commits into
mainfrom
fix-group-label-fmt
Feb 13, 2026
Merged

Fix: Modify the group labels when render_formats() is called#769
rich-iannone merged 7 commits into
mainfrom
fix-group-label-fmt

Conversation

@juleswg23

Copy link
Copy Markdown
Contributor

Summary

Apply fmt() output to group labels in the stub.

This approach goes upstream of the approach in #755. It is admittedly, a little heavier-weight, but does still happen at render time. The advantage to this over #755 is that we are modifying the group label before the html rendering, so this will apply to latex tables. Additionally, it takes advantage of the stubbed out group_row.group_label attribute.

from great_tables import GT
from great_tables.data import countrypops
import polars as pl
import polars.selectors as cs

# Get vectors of 2-letter country codes for each region of Oceania
countries = {
    "[Australasia](http://www.google.com)": ["AU", "NZ"],
    "[Melanesia](http://www.google.com)": ["NC", "PG", "SB", "VU"],
    "[Micronesia](http://www.google.com)": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
    "[Polynesia](http://www.google.com)": ["PF", "WS", "TO", "TV"],
}

# Create a dictionary mapping region to country (e.g. AU -> Australasia)
region_to_country = {
    region: country for country, regions in countries.items() for region in regions
}

wide_pops = (
    pl.from_pandas(countrypops)
    .filter(
        pl.col("country_code_2").is_in(list(region_to_country))
        & pl.col("year").is_in([2000, 2010, 2020])
    )
    .with_columns(pl.col("country_code_2").replace(region_to_country).alias("region"))
    .pivot(index=["country_name", "region"], columns="year", values="population")
    .sort("2020", descending=True)
)

(
    GT(wide_pops, rowname_col="country_name", groupname_col="region")
    .fmt_markdown(columns=["region"])
    .tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020")
    .tab_spanner(label="Total Population", columns=cs.all())
    .fmt_integer()
)

Previously, this code block had the following output.

image

Now we see this output:

image

Related GitHub Issues and PRs

closes #761

We could also close #236 if this PR is merged.

Checklist

@codecov

codecov Bot commented Aug 28, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.36%. Comparing base (34888dd) to head (7934a49).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #769      +/-   ##
==========================================
+ Coverage   92.31%   92.36%   +0.05%     
==========================================
  Files          49       48       -1     
  Lines        6009     6037      +28     
==========================================
+ Hits         5547     5576      +29     
+ Misses        462      461       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@juleswg23

Copy link
Copy Markdown
Contributor Author

This PR is based on a conversation with @machow. I am somewhat indifferent to the approaches here and in #755, but it would be nice to go forward with at least one of these. Whichever makes sense to continue with, I'll commit a new test to.

@juleswg23 juleswg23 marked this pull request as ready for review August 28, 2025 19:54
@github-actions github-actions Bot temporarily deployed to pr-769 August 28, 2025 20:03 Destroyed
@machow

machow commented Feb 12, 2026

Copy link
Copy Markdown
Collaborator

This is great, thanks! Sorry for taking so long to get to the PR. @rich-iannone is onboard with helping pull the logic out of gt.py and adding a test!

@github-actions github-actions Bot temporarily deployed to pr-769 February 12, 2026 17:51 Destroyed
@github-actions github-actions Bot temporarily deployed to pr-769 February 13, 2026 01:27 Destroyed
@github-actions github-actions Bot temporarily deployed to pr-769 February 13, 2026 01:48 Destroyed

@machow machow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rich-iannone rich-iannone merged commit 1af68a3 into main Feb 13, 2026
14 checks passed
@rich-iannone rich-iannone deleted the fix-group-label-fmt branch February 13, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set group_info.group_label to the displayed group label fmt_markdown method doesn't seem to work on groupname_col

3 participants