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: support pl.UInt type in nanoplot #577

Merged
merged 3 commits into from
Jan 14, 2025
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
6 changes: 5 additions & 1 deletion great_tables/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4601,7 +4601,11 @@ def fmt_nanoplot(

col_class = str(column_d_type).lower()

if _str_detect(col_class, "int") or _str_detect(col_class, "float"):
if (
_str_detect(col_class, "int")
or _str_detect(col_class, "uint")
or _str_detect(col_class, "float")
):
scalar_vals = True
else:
scalar_vals = False
Expand Down
6 changes: 5 additions & 1 deletion great_tables/_gt_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,11 @@ def align_from_data(self, data: TblData) -> Self:
align.append("left")
elif col_class == "string":
align.append("left")
elif _str_detect(col_class, "int") or _str_detect(col_class, "float"):
elif (
_str_detect(col_class, "int")
or _str_detect(col_class, "uint")
or _str_detect(col_class, "float")
):
align.append("right")
elif _str_detect(col_class, "date"):
align.append("right")
Expand Down
5 changes: 4 additions & 1 deletion tests/test__boxhead.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def test_cols_align_columns_list_of_str():


def test_cols_align_pl_expr():
df = pl.DataFrame({"col1": [1, 2], "col2": [3.3, 4.4], "c": ["x", "y"]})
df = pl.DataFrame({"col1": [1, 2], "col2": [3.3, 4.4], "c": ["x", "y"]}).with_columns(
pl.col("col1").cast(pl.UInt8).alias("d")
)
table = gt.GT(df)

# Select columns by polars expressions
Expand All @@ -110,6 +112,7 @@ def test_cols_align_pl_expr():
"center", # manually assign
"center", # manually assign
"left", # `auto_align` for `str` is "left"
"right", # `auto_align` for `pl.UInt` is "right"
]


Expand Down
12 changes: 12 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,18 @@ def test_fmt_nanoplot_multi_vals_bar_ref_line_ref_area():
)


def test_fmt_nanoplot_accept_pl_uint():
"""
https://github.com/posit-dev/great-tables/issues/571
"""
single_vals_df = pl.DataFrame({"bars": [1, 2, 3, 5]})

# Call `as_raw_html()` to trigger `_generate_nanoplot()` and expose the rendering issue.
single_vals_df.select("bars").cast(pl.UInt32).style.fmt_nanoplot(
columns="bars", plot_type="bar"
).as_raw_html()


@pytest.mark.parametrize(
"plot_type",
[
Expand Down
Loading