8000 DEPR: `Styler.set_na_rep` and `.set_precision` in favour of `.format(na_rep='x', precision=3)` by attack68 · Pull Request #40134 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DEPR: Styler.set_na_rep and .set_precision in favour of .format(na_rep='x', precision=3) #40134

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
merged 43 commits into from
Mar 5, 2021
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
971fb40
bug: formatter overwrites na_rep
attack68 Feb 25, 2021
6f573ee
bug: formatter overwrites na_rep
attack68 Feb 25, 2021
f50f894
bug: formatter overwrites na_rep
attack68 Feb 25, 2021
47c4021
bug: formatter overwrites na_rep
attack68 Feb 25, 2021
7371e44
bug: formatter overwrites na_rep
attack68 Feb 25, 2021
82474b1
group tests
attack68 Feb 25, 2021
9a9a39b
revert small chg
attack68 Feb 25, 2021
f72b335
docs
attack68 Feb 25, 2021
0792393
test
attack68 Feb 25, 2021
7bb1171
mypy
attack68 Feb 25, 2021
0d67562
mypy
attack68 Feb 25, 2021
384916d
easy compare
attack68 Feb 25, 2021
76a44ae
easy compare
attack68 Feb 25, 2021
e3d4daf
easy compare
attack68 Feb 25, 2021
63532af
Merge remote-tracking branch 'upstream/master' into fix_formatter_na_rep
attack68 Feb 27, 2021
7bc66eb
deprecate set_na_rep() and set_precision() in favour of format()
attack68 Feb 28, 2021
518405c
edit
attack68 Feb 28, 2021
f3b38ae
edit
attack68 Feb 28, 2021
ae88c55
edit
attack68 Feb 28, 2021
859125f
edit
attack68 Feb 28, 2021
c34083e
docs
attack68 Feb 28, 2021
60a25ff
docs
attack68 Feb 28, 2021
7a64724
same var names
attack68 Mar 1, 2021
4c68cc9
same var names
attack68 Mar 1, 2021
c385a62
same var names
attack68 Mar 1, 2021
61e367a
doc examples
attack68 Mar 1, 2021
c664118
Merge remote-tracking branch 'upstream/master' into depr_format_na_rep
attack68 Mar 1, 2021
a880754
doc examples
attack68 Mar 1, 2021
ed5b13a
typing
attack68 Mar 1, 2021
27fb8d5
typing
attack68 Mar 1, 2021
88fc84d
Merge remote-tracking branch 'upstream/master' into depr_format_na_rep
attack68 Mar 2, 2021
bf7f9fc
req changes
attack68 Mar 3, 2021
892df77
req changes
attack68 Mar 3, 2021
b63220b
tests
attack68 Mar 3, 2021
fbe8bb2
whats new
attack68 Mar 3, 2021
302924c
req changes
attack68 Mar 4, 2021
43b8fa7
test clear
attack68 Mar 4, 2021
672f42c
Merge remote-tracking branch 'upstream/master' into depr_format_na_rep
attack68 Mar 5, 2021
482506c
Merge branch 'master' into depr_format_na_rep
jreback Mar 5, 2021
2c7d647
Update doc/source/whatsnew/v1.3.0.rst
attack68 Mar 5, 2021
aa51fe0
Update pandas/io/formats/style.py
attack68 Mar 5, 2021
d26d761
Update pandas/io/formats/style.py
attack68 Mar 5, 2021
94370d8
format docstring
attack68 Mar 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
easy compare
  • Loading branch information
attack68 committed Feb 25, 2021
commit 76a44ae19fff91cb38c371a6856258b4271433bf
68 changes: 34 additions & 34 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,40 +219,6 @@ def _init_tooltips(self):
def _default_display_func(self, x):
return self._maybe_wrap_formatter(formatter=None)(x)

def _default_formatter(self, x):
if isinstance(x, (float, complex)):
return f"{x:.{self.precision}f}"
return x

def _maybe_wrap_formatter(
self,
formatter: Optional[Union[Callable, str]] = None,
na_rep: Optional[str] = None,
) -> Callable:
"""
Allows formatters to be expressed as str, callable or None, where None returns
a default formatting function. wraps with na_rep where it is available.
"""
if isinstance(formatter, str):
func = lambda x: formatter.format(x)
elif callable(formatter):
func = formatter
elif formatter is None:
func = self._default_formatter
else:
raise TypeError(
f"'formatter' expected str or callable, got {type(formatter)}"
)

if na_rep is not None:
return lambda x: na_rep if pd.isna(x) else func(x)
else:
return (
lambda x: self.na_rep
if all((self.na_rep is not None, pd.isna(x)))
else func(x)
)

def set_tooltips(self, ttips: DataFrame) -> Styler:
"""
Add string based tooltips that will appear in the `Styler` HTML result. These
Expand Down Expand Up @@ -1341,6 +1307,40 @@ def hide_columns(self, subset) -> Styler:
self.hidden_columns = self.columns.get_indexer_for(hidden_df.columns)
return self

def _default_formatter(self, x):
if isinstance(x, (float, complex)):
return f"{x:.{self.precision}f}"
return x

def _maybe_wrap_formatter(
self,
formatter: Optional[Union[Callable, str]] = None,
na_rep: Optional[str] = None,
) -> Callable:
"""
Allows formatters to be expressed as str, callable or None, where None returns
a default formatting function. wraps with na_rep where it is available.
"""
if isinstance(formatter, str):
func = lambda x: formatter.format(x)
elif callable(formatter):
func = formatter
elif formatter is None:
func = self._default_formatter
else:
raise TypeError(
f"'formatter' expected str or callable, got {type(formatter)}"
)

if na_rep is not None:
return lambda x: na_rep if pd.isna(x) else func(x)
else:
return (
lambda x: self.na_rep
if all((self.na_rep is not None, pd.isna(x)))
else func(x)
)

# -----------------------------------------------------------------------
# A collection of "builtin" styles
# -----------------------------------------------------------------------
Expand Down
0