-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
ENH: Add built-in function for Styler to format the text displayed for missing values #29118
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 5 commits
c42de40
01632ce
53b0843
7a5dd65
da3cb43
bdfff98
b86bdc6
a1e9a9e
def71c9
af396b1
3d4cfd0
bd99db9
346eee6
7935359
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -71,6 +71,9 @@ class Styler: | |||||
| The ``id`` takes the form ``T_<uuid>_row<num_row>_col<num_col>`` | ||||||
| where ``<uuid>`` is the unique identifier, ``<num_row>`` is the row | ||||||
| number and ``<num_col>`` is the column number. | ||||||
| na_rep : str or None, default None | ||||||
| Representation for missing values. | ||||||
| If ``na_rep`` is None, no special formatting is applied | ||||||
|
|
||||||
| Attributes | ||||||
| ---------- | ||||||
|
|
@@ -126,6 +129,7 @@ def __init__( | |||||
| caption=None, | ||||||
| table_attributes=None, | ||||||
| cell_ids=True, | ||||||
| na_rep=None, | ||||||
|
||||||
| ): | ||||||
| self.ctx = defaultdict(list) | ||||||
| self._todo = [] | ||||||
|
|
@@ -151,11 +155,14 @@ def __init__( | |||||
| self.hidden_index = False | ||||||
| self.hidden_columns = [] | ||||||
| self.cell_ids = cell_ids | ||||||
| self.na_rep = na_rep | ||||||
|
|
||||||
| # display_funcs maps (row, col) -> formatting function | ||||||
|
|
||||||
| def default_display_func(x): | ||||||
| if is_float(x): | ||||||
| if self.na_rep is not None and pd.isna(x): | ||||||
| return self.na_rep | ||||||
| elif is_float(x): | ||||||
| display_format = "{0:.{precision}f}".format(x, precision=self.precision) | ||||||
| return display_format | ||||||
| else: | ||||||
|
|
@@ -416,16 +423,20 @@ def format_attr(pair): | |||||
| table_attributes=table_attr, | ||||||
| ) | ||||||
|
|
||||||
| def format(self, formatter, subset=None): | ||||||
| def format(self, formatter=None, subset=None, na_rep=None): | ||||||
|
||||||
| """ | ||||||
| Format the text display value of cells. | ||||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| formatter : str, callable, or dict | ||||||
| formatter : str, callable, dict or None | ||||||
| If ``formatter`` is None, the default formatter is used | ||||||
| subset : IndexSlice | ||||||
| An argument to ``DataFrame.loc`` that restricts which elements | ||||||
| ``formatter`` is applied to. | ||||||
| na_rep : str or None, default None | ||||||
| Representation for missing values. | ||||||
| If ``na_rep`` is None, no special formatting is applied | ||||||
|
|
||||||
| Returns | ||||||
| ------- | ||||||
|
|
@@ -451,6 +462,9 @@ def format(self, formatter, subset=None): | |||||
| >>> df['c'] = ['a', 'b', 'c', 'd'] | ||||||
| >>> df.style.format({'c': str.upper}) | ||||||
| """ | ||||||
| if formatter is None: | ||||||
| formatter = self._display_funcs.default_factory() | ||||||
|
|
||||||
| if subset is None: | ||||||
| row_locs = range(len(self.data)) | ||||||
| col_locs = range(len(self.data.columns)) | ||||||
|
|
@@ -467,15 +481,17 @@ def format(self, formatter, subset=None): | |||||
| for col, col_formatter in formatter.items(): | ||||||
| # formatter must be callable, so '{}' are converted to lambdas | ||||||
| col_formatter = _maybe_wrap_formatter(col_formatter) | ||||||
| col_formatter = _maybe_wrap_na_formatter(col_formatter, na_rep) | ||||||
| col_num = self.data.columns.get_indexer_for([col])[0] | ||||||
|
|
||||||
| for row_num in row_locs: | ||||||
| self._display_funcs[(row_num, col_num)] = col_formatter | ||||||
| else: | ||||||
| # single scalar to format all cells with | ||||||
| formatter = _maybe_wrap_formatter(formatter) | ||||||
| formatter = _maybe_wrap_na_formatter(formatter, na_rep) | ||||||
| locs = product(*(row_locs, col_locs)) | ||||||
| for i, j in locs: | ||||||
| formatter = _maybe_wrap_formatter(formatter) | ||||||
| self._display_funcs[(i, j)] = formatter | ||||||
| return self | ||||||
|
|
||||||
|
|
@@ -553,6 +569,7 @@ def _copy(self, deepcopy=False): | |||||
| caption=self.caption, | ||||||
| uuid=self.uuid, | ||||||
| table_styles=self.table_styles, | ||||||
| na_rep=self.na_rep, | ||||||
| ) | ||||||
| if deepcopy: | ||||||
| styler.ctx = copy.deepcopy(self.ctx) | ||||||
|
|
@@ -891,6 +908,23 @@ def set_table_styles(self, table_styles): | |||||
| self.table_styles = table_styles | ||||||
| return self | ||||||
|
|
||||||
| def set_na_rep(self, na_rep): | ||||||
|
||||||
| def set_na_rep(self, na_rep): | |
| def set_na_rep(self, na_rep: str) -> "Styler": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem! will do.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this related to na_rep?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually not related. 😂
Just making it consistent with highlight_min and highlight_max, also for completeness of missing values styling and formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice idea but would prefer if you take this out then. We try to keep PRs to changing one thing at a time as it helps speed up review process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can always do as a follow up PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that is a great advice!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be folded into _maybe_wrap_formatter? I'm finding the multiple layers of wrapping hard to follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, all done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test case that hits this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be
str, optional.