posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.42k stars 48 forks source link

Nanoplots compact formatting gets applied before removing exponent, causing error #334

Closed machow closed 1 month ago

machow commented 1 month ago
import polars as pl
from great_tables import GT

df = pl.DataFrame({"x": [[1, 2], [7045, 3]]})
GT(df).fmt_nanoplot("x", plot_type="bar")

raises this error:

image

This is because 7045 is converted to '7.04K', and then _remove_exponent() is trying to convert that to a decimal.

machow commented 1 month ago

Similarly this fails:

import polars as pl
from great_tables import GT

df = pl.DataFrame({"x": [[1, 2], [-704, 3]]})
GT(df).fmt_nanoplot("x", plot_type="bar")

~Because -704 has its minus sign converted (in the line above) from a hyphen (computer speak for minus) to the minus sign character. ~

edit: wait, it's because it's formatted to '-,704'

here's a minimal reprex:

from great_tables.vals import fmt_number

fmt_number(-704, n_sigfig=3, decimals=1, compact=True) # ['-,704']
rich-iannone commented 1 month ago

Fixed by https://github.com/posit-dev/great-tables/pull/335.