-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
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
Merged
TomAugspurger merged 14 commits into pandas-dev:master from immaxchen:styler-builtin-shortcut-for-missing-values-formatting-gh28358 Nov 25, 2019
Merged
Changes from 1 commit
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
c42de40 Add built-in funcion for Styler to format the text displayed for miss…
immaxchen 01632ce Add Styler.format_null into user_guide and reference Doc
immaxchen 53b0843 Revised to change implementation to integrate with the original Style…
immaxchen 7a5dd65 resolve merge conflicts
immaxchen da3cb43 add more tests for styling with NA values
immaxchen bdfff98 Merge remote-tracking branch 'upstream/master' into styler-builtin-sh…
immaxchen b86bdc6 revision based on the requested changes
immaxchen a1e9a9e add tests, enhance doc string and formatter wrapping
immaxchen def71c9 Merge remote-tracking branch 'upstream/master' into styler-builtin-sh…
immaxchen af396b1 resolve merge conflict
immaxchen 3d4cfd0 add type-hint and xfail test
immaxchen bd99db9 revise test using pytest.raises
immaxchen 346eee6 Merge remote-tracking branch 'upstream/master' into styler-builtin-sh…
immaxchen 7935359 using py36 syntax for annotations
immaxchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add tests, enhance doc string and formatter wrapping
- Loading branch information
commit a1e9a9ef20b052a7278ce8a327bcea02d77442ac
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| | @@ -71,7 +71,7 @@ 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 | ||
| na_rep : str, optional | ||
| Representation for missing values. | ||
| If ``na_rep`` is None, no special formatting is applied | ||
| | ||
| | @@ -436,7 +436,7 @@ def format(self, formatter, subset=None, na_rep=None): | |
| subset : IndexSlice | ||
| An argument to ``DataFrame.loc`` that restricts which elements | ||
| ``formatter`` is applied to. | ||
| na_rep : str or None, default None | ||
| na_rep : str, optional | ||
| Representation for missing values. | ||
| If ``na_rep`` is None, no special formatting is applied | ||
| | ||
| | @@ -484,16 +484,14 @@ def format(self, formatter, subset=None, na_rep=None): | |
| if is_dict_like(formatter): | ||
| 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_formatter = _maybe_wrap_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) | ||
| formatter = _maybe_wrap_formatter(formatter, na_rep) | ||
| locs = product(*(row_locs, col_locs)) | ||
| for i, j in locs: | ||
| self._display_funcs[(i, j)] = formatter | ||
| | @@ -1507,24 +1505,22 @@ def _get_level_lengths(index, hidden_elements=None): | |
| return non_zero_lengths | ||
| | ||
| | ||
| def _maybe_wrap_formatter(formatter): | ||
| def _maybe_wrap_formatter(formatter, na_rep): | ||
| if is_string_like(formatter): | ||
| return lambda x: formatter.format(x) | ||
| formatter_func = lambda x: formatter.format(x) | ||
| elif callable(formatter): | ||
| return formatter | ||
| formatter_func = formatter | ||
| else: | ||
| msg = ( | ||
| "Expected a template string or callable, got {formatter} " | ||
| "instead".format(formatter=formatter) | ||
| ) | ||
| raise TypeError(msg) | ||
| | ||
| | ||
| def _maybe_wrap_na_formatter(formatter, na_rep): | ||
| if na_rep is None: | ||
| return formatter | ||
| return formatter_func | ||
| elif is_string_like(na_rep): | ||
| return lambda x: na_rep if pd.isna(x) else formatter(x) | ||
| return lambda x: na_rep if pd.isna(x) else formatter_func(x) | ||
| else: | ||
| msg = "Expected a string, got {na_rep} instead".format(na_rep=na_rep) | ||
| Member 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. Can you add a test case that hits this? | ||
| raise TypeError(msg) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.
and here