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(python): When setting write_excel column totals, don't forget to include any row-total cols #18042

Merged
merged 5 commits into from
Aug 8, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Aug 4, 2024

When setting column_totals=True we should account for any columns created via row_totals (we used to1, and it seems this went missing somewhere in a refactor).

Also:

  • The row_totals column(s) dtype (int/float) should be determined by the constant columns; if all are int, the total should also be int, not float.
  • Default numeric value formats now better-match explicit table style colours.

Example

import polars as pl

df = pl.DataFrame({
    "id": ["aaa", "bbb", "ccc", "ddd", "eee"],
    "q1": [100.0, 55.5, -20.0, 0.5, 35.0],
    "q2": [30.5, -10.25, 15.0, 60.0, 20.5],
    "q3": [-50, 0, 40, 80, 80],
    "q4": [75, 55, 25, -10, -55],
})

df.write_excel(
    "testing.xlsx",
    table_style = "Table Style Light 2",
    sparklines = {"trend": ["q1", "q2", "q3", "q4"]},
    row_totals = {
        # add semiannual row total columns
        "h1": ("q1","q2"),
        "h2": ("q3","q4"),
    },
    column_totals = True,
    autofit = True,
)

Output:

  • Column totals now exist for "h1" and "h2".
  • The "h1" totals are float, "h2" totals are integer.
  • Numeric values match the table style colour (and string value colour).
Screenshot 2024-08-04 at 23 23 38

Footnotes

  1. https://github.com/pola-rs/polars/pull/7751

@github-actions github-actions bot added fix Bug fix python Related to Python Polars labels Aug 4, 2024
@alexander-beedie alexander-beedie added the A-io-excel Area: reading/writing Excel files label Aug 4, 2024
Copy link

codecov bot commented Aug 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.39%. Comparing base (ca6d46c) to head (a9e4cec).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18042      +/-   ##
==========================================
- Coverage   80.39%   80.39%   -0.01%     
==========================================
  Files        1496     1496              
  Lines      197480   197489       +9     
  Branches     2820     2821       +1     
==========================================
- Hits       158764   158763       -1     
- Misses      38194    38205      +11     
+ Partials      522      521       -1     

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

@alexander-beedie alexander-beedie force-pushed the xl-row-column-totals-fix branch 2 times, most recently from 75a5667 to a6695a9 Compare August 5, 2024 12:08
@ritchie46 ritchie46 merged commit 4dabab6 into pola-rs:main Aug 8, 2024
29 checks passed
@alexander-beedie alexander-beedie deleted the xl-row-column-totals-fix branch August 8, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-excel Area: reading/writing Excel files fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants