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 7 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

All modified and coverable lines are covered by tests ✅

Project coverage is 88.93%. Comparing base (336e688) to head (78d5327).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #513      +/-   ##
==========================================
+ Coverage   88.91%   88.93%   +0.02%     
==========================================
  Files          44       44              
  Lines        5212     5222      +10     
==========================================
+ Hits         4634     4644      +10     
  Misses        578      578              

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


🚨 Try these New Features:

@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
@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."

<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

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