forked from pola-rs/polars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): add style namespace (which defers to Great Tables) (pol…
…a-rs#16809) Co-authored-by: Marco Edward Gorelli <[email protected]>
- Loading branch information
Showing
11 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
# --8<-- [start:dataframe] | ||
import polars as pl | ||
import polars.selectors as cs | ||
|
||
path = "docs/data/iris.csv" | ||
|
||
df = ( | ||
pl.scan_csv(path) | ||
.group_by("species") | ||
.agg(cs.starts_with("petal").mean().round(3)) | ||
.collect() | ||
) | ||
print(df) | ||
# --8<-- [end:dataframe] | ||
|
||
# --8<-- [start:structure-header] | ||
df.style.tab_header(title="Iris Data", subtitle="Mean measurement values per species") | ||
# --8<-- [end:structure-header] | ||
|
||
# --8<-- [start:structure-header-out] | ||
print( | ||
df.style.tab_header( | ||
title="Iris Data", subtitle="Mean measurement values per species" | ||
).as_raw_html() | ||
) | ||
# --8<-- [end:structure-header-out] | ||
|
||
|
||
# --8<-- [start:structure-stub] | ||
df.style.tab_stub(rowname_col="species") | ||
# --8<-- [end:structure-stub] | ||
|
||
# --8<-- [start:structure-stub-out] | ||
print(df.style.tab_stub(rowname_col="species").as_raw_html()) | ||
# --8<-- [end:structure-stub-out] | ||
|
||
# --8<-- [start:structure-spanner] | ||
( | ||
df.style.tab_spanner("Petal", cs.starts_with("petal")).cols_label( | ||
petal_length="Length", petal_width="Width" | ||
) | ||
) | ||
# --8<-- [end:structure-spanner] | ||
|
||
# --8<-- [start:structure-spanner-out] | ||
print( | ||
df.style.tab_spanner("Petal", cs.starts_with("petal")) | ||
.cols_label(petal_length="Length", petal_width="Width") | ||
.as_raw_html() | ||
) | ||
# --8<-- [end:structure-spanner-out] | ||
|
||
# --8<-- [start:format-number] | ||
df.style.fmt_number("petal_width", decimals=1) | ||
# --8<-- [end:format-number] | ||
|
||
|
||
# --8<-- [start:format-number-out] | ||
print(df.style.fmt_number("petal_width", decimals=1).as_raw_html()) | ||
# --8<-- [end:format-number-out] | ||
|
||
|
||
# --8<-- [start:style-simple] | ||
from great_tables import loc, style | ||
|
||
df.style.tab_style( | ||
style.fill("yellow"), | ||
loc.body( | ||
rows=pl.col("petal_length") == pl.col("petal_length").max(), | ||
), | ||
) | ||
# --8<-- [end:style-simple] | ||
|
||
# --8<-- [start:style-simple-out] | ||
from great_tables import loc, style | ||
|
||
print( | ||
df.style.tab_style( | ||
style.fill("yellow"), | ||
loc.body( | ||
rows=pl.col("petal_length") == pl.col("petal_length").max(), | ||
), | ||
).as_raw_html() | ||
) | ||
# --8<-- [end:style-simple-out] | ||
|
||
|
||
# --8<-- [start:style-bold-column] | ||
from great_tables import loc, style | ||
|
||
df.style.tab_style( | ||
style.text(weight="bold"), | ||
loc.body(columns="species"), | ||
) | ||
# --8<-- [end:style-bold-column] | ||
|
||
# --8<-- [start:style-bold-column-out] | ||
from great_tables import loc, style | ||
|
||
print( | ||
df.style.tab_style( | ||
style.text(weight="bold"), | ||
loc.body(columns="species"), | ||
).as_raw_html() | ||
) | ||
# --8<-- [end:style-bold-column-out] | ||
|
||
# --8<-- [start:full-example] | ||
from great_tables import loc, style | ||
|
||
( | ||
df.style.tab_header( | ||
title="Iris Data", subtitle="Mean measurement values per species" | ||
) | ||
.tab_stub(rowname_col="species") | ||
.cols_label(petal_length="Length", petal_width="Width") | ||
.tab_spanner("Petal", cs.starts_with("petal")) | ||
.fmt_number("petal_width", decimals=2) | ||
.tab_style( | ||
style.fill("yellow"), | ||
loc.body( | ||
rows=pl.col("petal_length") == pl.col("petal_length").max(), | ||
), | ||
) | ||
) | ||
# --8<-- [end:full-example] | ||
|
||
# --8<-- [start:full-example-out] | ||
from great_tables import loc, style | ||
|
||
print( | ||
df.style.tab_header( | ||
title="Iris Data", subtitle="Mean measurement values per species" | ||
) | ||
.tab_stub(rowname_col="species") | ||
.cols_label(petal_length="Length", petal_width="Width") | ||
.tab_spanner("Petal", cs.starts_with("petal")) | ||
.fmt_number("petal_width", decimals=2) | ||
.tab_style( | ||
style.fill("yellow"), | ||
loc.body( | ||
rows=pl.col("petal_length") == pl.col("petal_length").max(), | ||
), | ||
) | ||
.tab_style( | ||
style.text(weight="bold"), | ||
loc.body(columns="species"), | ||
) | ||
.as_raw_html() | ||
) | ||
# --8<-- [end:full-example-out] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Styling | ||
|
||
Data in a Polars `DataFrame` can be styled for presentation use the `DataFrame.style` property. This returns a `GT` object from [Great Tables](https://posit-dev.github.io/great-tables/articles/intro.html), which enables structuring, formatting, and styling for table display. | ||
|
||
{{code_block('user-guide/misc/styling','dataframe',[])}} | ||
|
||
```python exec="on" result="text" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:dataframe" | ||
``` | ||
|
||
## Structure: add header title | ||
|
||
{{code_block('user-guide/misc/styling','structure-header',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:structure-header-out" | ||
``` | ||
|
||
## Structure: add row stub | ||
|
||
{{code_block('user-guide/misc/styling','structure-stub',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:structure-stub-out" | ||
``` | ||
|
||
## Structure: add column spanner | ||
|
||
{{code_block('user-guide/misc/styling','structure-spanner',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:structure-spanner-out" | ||
``` | ||
|
||
## Format: limit decimal places | ||
|
||
{{code_block('user-guide/misc/styling','format-number',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:format-number-out" | ||
``` | ||
|
||
## Style: highlight max row | ||
|
||
{{code_block('user-guide/misc/styling','style-simple',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:style-simple-out" | ||
``` | ||
|
||
## Style: bold species column | ||
|
||
{{code_block('user-guide/misc/styling','style-bold-column',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:style-bold-column-out" | ||
``` | ||
|
||
## Full example | ||
|
||
{{code_block('user-guide/misc/styling','full-example',[])}} | ||
|
||
```python exec="on" session="user-guide/misc/styling" | ||
--8<-- "python/user-guide/misc/styling.py:full-example-out" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
===== | ||
Style | ||
===== | ||
|
||
.. currentmodule:: polars | ||
|
||
.. autoproperty:: DataFrame.style |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.