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

Feat: accounting notation for fmt_number(), fmt_percent(), fmt_integer() and fmt_currency() #513

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

rich-iannone
Copy link
Member

This introduces the accounting= arg to fmt_number(), fmt_percent(), fmt_integer() and fmt_currency(), allowing for accounting notation (e.g., "-343.27" -> "(343.27)". This new functionality also extends to the same formatters in the vals module.

Here is an example table demonstrating usage:

from great_tables import GT
import polars as pl

df = pl.DataFrame(
    {"number": [-1.2, 23.6], "percent": [-0.0523, 0.363], "integer": [2323, -23213], "currency": [-24334.23, 7323.253]}
).with_columns(number_acc = pl.col("number"), percent_acc = pl.col("percent"), integer_acc = pl.col("integer"), currency_acc = pl.col("currency"))

(
    GT(df)
    .fmt_number(columns="number")
    .fmt_percent(columns="percent")
    .fmt_integer(columns="integer")
    .fmt_currency(columns="currency")
    .fmt_number(columns="number_acc", accounting=True)
    .fmt_percent(columns="percent_acc", accounting=True)
    .fmt_integer(columns="integer_acc", accounting=True)
    .fmt_currency(columns="currency_acc", accounting=True)
    .tab_spanner(label="Default formatting", columns=[0, 1, 2, 3])
    .tab_spanner(label="Accounting notation", columns=[4, 5, 6, 7])
)
image

Fixes: #278

Copy link

codecov bot commented Nov 20, 2024

Codecov Report

Attention: Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.81%. Comparing base (925fa41) to head (dd05ed7).

Files with missing lines Patch % Lines
great_tables/_formats.py 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #513      +/-   ##
==========================================
+ Coverage   89.79%   89.81%   +0.01%     
==========================================
  Files          45       45              
  Lines        5321     5331      +10     
==========================================
+ Hits         4778     4788      +10     
  Misses        543      543              

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

@github-actions github-actions bot temporarily deployed to pr-513 November 20, 2024 16:03 Destroyed
@rich-iannone rich-iannone marked this pull request as ready for review November 20, 2024 16:05
@github-actions github-actions bot temporarily deployed to pr-513 November 20, 2024 16:08 Destroyed
@machow machow self-requested a review November 22, 2024 21:13
Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

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

I left one suggestion for a minor tweak to the docstring language, but feel free to take or leave it! (not the language is repeated in several places)

@@ -108,6 +109,9 @@ def val_fmt_number(
The `use_seps` option allows for the use of digit group separators. The type of digit group
separator is set by `sep_mark` and overridden if a locale ID is provided to `locale`. This
setting is `True` by default.
accounting
An option to use accounting style for values. Normally, negative values will be shown with a
Copy link
Collaborator

Choose a reason for hiding this comment

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

WDYT of changing these docstrings to something like "Whether to use accounting style, which wraps negative numbers in parentheses instead of using a minus sign."

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, now done!

<tr>
<td class="gt_row gt_right">−1.20</td>
<td class="gt_row gt_right">−5.23%</td>
<td class="gt_row gt_right">2,323</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems like this is not testing that fmt_integer uses accounting format on negative numbers

Copy link
Member Author

Choose a reason for hiding this comment

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

The first four columns of this table are formatted but with accounting=False (minus signs persist) , the last four have accounting=True so we do get the negative values in parens there.

@github-actions github-actions bot temporarily deployed to pr-513 November 26, 2024 16:39 Destroyed
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.

Add ability to format as accounting values (in fmt_*() methods where it makes sense)
2 participants