Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spanners_print_matrix now excludes selected cols that are in the stub #49

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ wide_pops = (
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").map_dict(region_to_country).alias("region"))
.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)
)
Expand Down
6 changes: 5 additions & 1 deletion great_tables/_spanners.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,11 @@ def spanners_print_matrix(

for span_ii, span in enumerate(crnt_spans):
for var in span.vars:
label_matrix[span.spanner_level][var] = spanner_reprs[span_ii]
# This if clause skips spanned columns that are not in the
# boxhead vars we are planning to use (e.g. not in the visible ones
# or in the stub).
if var in label_matrix[span.spanner_level]:
label_matrix[span.spanner_level][var] = spanner_reprs[span_ii]

# reverse order , so if you were to print it out, level 0 would appear on the bottom
label_matrix.reverse()
Expand Down
11 changes: 11 additions & 0 deletions tests/test_spanners.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ def test_spanners_print_matrix_arg_include_hidden(spanners, boxhead):
]


def test_spanners_print_matrix_exclude_stub():
"""spanners_print_matrix omits a selected column if it's in the stub."""
info = SpannerInfo(spanner_id="a", spanner_level=0, vars=["x", "y"], built="A")
spanners = Spanners([info])
boxh = Boxhead([ColInfo(var="x"), ColInfo(var="y", type=ColInfoTypeEnum.stub)])

mat, vars = spanners_print_matrix(spanners, boxh, omit_columns_row=True)
assert vars == ["x"]
assert mat == [{"x": "A"}]


def test_empty_spanner_matrix():
mat, vars = empty_spanner_matrix(["a", "b"], omit_columns_row=False)

Expand Down