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(python): Add DataFrame.style namespace #16809

Merged
merged 12 commits into from
Jun 13, 2024

Conversation

machow
Copy link
Contributor

@machow machow commented Jun 7, 2024

Based on conversation with @MarcoGorelli (cc @rich-iannone), this PR adds a DataFrame.style property, which returns a great_tables.GT object. This allows a polars DataFrame to quickly be customized for presentation.

The 3 big pieces Great Tables focuses on are structure, formatting, and style--which are emphasized in the Polars user guide. Polars expressions ended up really shaping the Great Tables API, so we hope a lot of the approach (e.g. using polars selectors to specify columns) will feel natural to folks!

Happy to make any changes, do anything useful :)

Demo

Here is the full example added to the user guide:

import polars as pl
import polars.selectors as cs
from great_tables import GT, loc, style

path = "docs/data/iris.csv"

df = (
    pl.scan_csv(path)
    .group_by("species")
    .agg(cs.starts_with("petal").mean().round(3))
    .collect()
)

(
    df.style.tab_header(
        title="Iris Data", subtitle="Mean measurement values per species"
    )

    # structure ----
    .tab_stub(rowname_col="species")
    .cols_label(petal_length="Length", petal_width="Width")
    .tab_spanner("Petal", cs.starts_with("petal"))

    # format ----
    .fmt_number("petal_width", decimals=2)

    # style ----
    .tab_style(
        style.fill("yellow"),
        loc.body(
            rows=pl.col("petal_length") == pl.col("petal_length").max(),
        ),
    )
)
image

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels Jun 7, 2024
@ritchie46
Copy link
Member

@MarcoGorelli Could you review this one? Seems very similar to the plotting one. Ideally I like the same non-stable guarantees and ofcourse lazy imports.

Thank you @machow for the PR.

Copy link
Collaborator

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Personally, I love this. I read recently someone on Reddit write

pandas still has a lot of features that aren't present in polars, table styling being the one I use the most

and if there's such an easy to get native Polars styling, I'm all for it.

The tables look really nice, and being able to pass expressions and selectors in is the icing the cake


could you run make pre-commit please?

py-polars/pyproject.toml Show resolved Hide resolved
@machow
Copy link
Contributor Author

machow commented Jun 7, 2024

I'm noticing now that polars supports python >= 3.8. Great Tables currently supports python >= 3.9 (we've loosely been following NEP 29). I'm happy to do whatever seems useful here (as I recall, restoring python 3.8 support to Great Tables is largely an issue of tweaking type annotations)

@machow machow requested a review from MarcoGorelli June 7, 2024 17:48
Co-authored-by: Marco Edward Gorelli <marcogorelli@protonmail.com>
Copy link

codecov bot commented Jun 7, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 81.42%. Comparing base (f4549f1) to head (96d94ea).
Report is 57 commits behind head on main.

Files Patch % Lines
py-polars/polars/dataframe/frame.py 57.14% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16809      +/-   ##
==========================================
+ Coverage   81.33%   81.42%   +0.09%     
==========================================
  Files        1424     1425       +1     
  Lines      187172   187973     +801     
  Branches     2697     2708      +11     
==========================================
+ Hits       152235   153060     +825     
+ Misses      34441    34415      -26     
- Partials      496      498       +2     

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

@machow
Copy link
Contributor Author

machow commented Jun 11, 2024

Alright--it looks like merging in the python>=3.9 in requirements.txt did the trick! Happy to do anything else useful!

Copy link
Collaborator

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Amazing

Granted, I'm a bit biased, because I did suggest that Michael do this, but I am really impressed with GreatTables:

  • they support Polars directly (no conversion to other dataframes necessary)
  • they accept Polars expressions and selectors as part of their API, making it feel really natural to users
  • as mentioned earlier, some users complain about lack of styling in Polars
  • their tables just look really good
  • it's low-maintenance on the Polars side. And if it doesn't work out for some reason, it's marked as unstable anyway. But my expectation is that people will love it

So, I'm really in favour

@MarcoGorelli
Copy link
Collaborator

(you are missing a note in docs/user-guide/installation.md though)

@machow
Copy link
Contributor Author

machow commented Jun 11, 2024

Ah thanks for the quick review, and catching the installation page! I did a lot of grepping for hvplot and pydantic, but the word "plot" slipped under the radar 😓. Added a style note to installation

Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

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

Thank you @machow for the PR and thanks for the review @MarcoGorelli!

@ritchie46 ritchie46 merged commit 8965a68 into pola-rs:main Jun 13, 2024
17 checks passed
@stinodego stinodego changed the title feat(python): add style namespace (which defers to Great Tables) feat(python): Add DataFrame.style namespace Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants