-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
ENH: Add custom descriptors (such as dtype, nunique, etc.) to Styler output #43894
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
Changes from all commits
9743099
8a0253e
74c418e
70535c5
2fbe569
7903723
6a2793c
d227914
7ca5002
f27f7ed
a1000a7
c44dcda
c4c9aaa
c22cf0d
0e0b46e
1f3bbec
021bc26
4ba3dff
7fee05d
baa3233
f4ad390
22b03e3
db214d8
771d056
24952ae
1d47d0f
566738d
230138a
b9ba9ea
ea2bba1
75de033
4c34bc2
68ee832
ca70491
7a1994e
91320f8
92c1941
aad0e16
61b24ed
d4c5715
aa0172f
84a814f
f06b727
131070f
a048122
4931f32
e9716f2
d983464
c08ef82
0f11a62
5c52957
17d181c
e5b4d3e
f98ba9f
0cc5502
c5b75cc
ac1384b
c948f11
afa8f10
60efbbb
9ef5b44
a02b4ac
6b51d88
814bace
33c35a4
91d8680
260d356
46856fb
a4dce4d
fefb420
f2c8f0e
a945da5
b3c0f96
f36d4fd
e78907f
cd085df
45f5879
6f64e80
1c5cf53
58aa894
bd2fe7d
02aeae5
5541396
beb2bc1
7d62bb3
6bfb5a9
9554f89
d8e11c8
4f935c4
7031897
0c034a6
6128ccb
bb8201e
44204e0
ebec1d6
c205c38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ | |
from pandas.io.formats.style_render import ( | ||
CSSProperties, | ||
CSSStyles, | ||
Descriptor, | ||
ExtFormatter, | ||
StylerRenderer, | ||
Subset, | ||
|
@@ -1435,6 +1436,7 @@ def _copy(self, deepcopy: bool = False) -> Styler: | |
] | ||
deep = [ # nested lists or dicts | ||
"css", | ||
"descriptors", | ||
"_display_funcs", | ||
"_display_funcs_index", | ||
"_display_funcs_columns", | ||
|
@@ -1977,6 +1979,9 @@ def export(self) -> dict[str, Any]: | |
|
||
Can be applied to a second Styler with ``Styler.use``. | ||
|
||
.. versionchanged:: 1.5.0 | ||
Adds ``descriptors`` to the exported items. | ||
|
||
Returns | ||
------- | ||
styles : dict | ||
|
@@ -1998,6 +2003,7 @@ def export(self) -> dict[str, Any]: | |
- Whether axes and names are hidden from the display, if unambiguous. | ||
- Table attributes | ||
- Table styles | ||
- Descriptors | ||
|
||
The following attributes are considered data dependent and therefore not | ||
exported: | ||
|
@@ -2027,6 +2033,7 @@ def export(self) -> dict[str, Any]: | |
"hide_index_names": self.hide_index_names, | ||
"hide_column_names": self.hide_column_names, | ||
"css": copy.copy(self.css), | ||
"descriptors": copy.copy(self.descriptors), | ||
} | ||
|
||
def use(self, styles: dict[str, Any]) -> Styler: | ||
|
@@ -2035,6 +2042,9 @@ def use(self, styles: dict[str, Any]) -> Styler: | |
|
||
Possibly uses styles from ``Styler.export``. | ||
|
||
.. versionchanged:: 1.5.0 | ||
Adds ``descriptors`` to the used items. | ||
|
||
Parameters | ||
---------- | ||
styles : dict(str, Any) | ||
|
@@ -2052,6 +2062,8 @@ def use(self, styles: dict[str, Any]) -> Styler: | |
- "hide_index_names": whether index names are hidden. | ||
- "hide_column_names": whether column header names are hidden. | ||
- "css": the css class names used. | ||
- "descriptors": list of descriptors, typically added with | ||
``set_descriptors``. | ||
|
||
Returns | ||
------- | ||
|
@@ -2094,6 +2106,8 @@ def use(self, styles: dict[str, Any]) -> Styler: | |
self.hide_column_names = styles.get("hide_column_names", False) | ||
if styles.get("css"): | ||
self.css = styles.get("css") # type: ignore[assignment] | ||
if styles.get("descriptors"): | ||
self.set_descriptors(styles.get("descriptors")) | ||
return self | ||
|
||
def set_uuid(self, uuid: str) -> Styler: | ||
|
@@ -2352,7 +2366,10 @@ def set_table_styles( | |
"row_trim": "row_trim", | ||
"level": "level", | ||
"data": "data", | ||
"blank": "blank} | ||
"blank": "blank", | ||
"descriptor": "descriptor", | ||
"descriptor_name": "descriptor_name", | ||
"descriptor_value": "descriptor_value"} | ||
|
||
Examples | ||
-------- | ||
|
@@ -2423,6 +2440,50 @@ def set_table_styles( | |
self.table_styles = table_styles | ||
return self | ||
|
||
def set_descriptors( | ||
self, descriptors: list[Descriptor | tuple[str, Descriptor]] | None = None | ||
) -> Styler: | ||
""" | ||
Add header-level calculations to the output which describes the data. | ||
|
||
.. versionadded:: 1.5.0 | ||
|
||
Parameters | ||
---------- | ||
descriptors : list of str, callables or 2-tuples of str and callable | ||
If a string is given must be a valid Series method, e.g. "mean" invokes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you some additional details about the callable. Specifically it must (or should) return a scalar quantity. Is the callable allowed to throw and exception? If so, what happens? I would be good to clarify this. |
||
Series.mean(). | ||
|
||
If a callable is given must accept a Series and return a scalar. | ||
|
||
If a 2-tuple, must be a string used as the name of the row and a | ||
callable or string as above. | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
Examples | ||
-------- | ||
|
||
>>> df = DataFrame([[1, 2], [3, 4]], columns=["A", "B"]) | ||
>>> def udf_func(s): | ||
... return s.mean() | ||
>>> styler = df.style.set_descriptors([ | ||
... "mean", | ||
... Series.mean, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The screen show shows no lable on this line? Was it not possible to have |
||
... ("my-text", "mean"), | ||
... ("my-text2", Series.mean), | ||
... ("my-func", lambda s: s.sum()/2), | ||
... lambda s: s.sum()/2, | ||
... udf_func, | ||
... ]) # doctest: +SKIP | ||
|
||
.. figure:: ../../_static/style/des_mean.png | ||
""" | ||
self.descriptors = descriptors if descriptors is not None else [] | ||
return self | ||
|
||
def set_na_rep(self, na_rep: str) -> StylerRenderer: | ||
""" | ||
Set the missing data representation on a ``Styler``. | ||
|
Uh oh!
There was an error while loading. Please reload this page.