From e39db37fa2144e0afd87fa20c168c31995f6a70c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 14 Mar 2021 09:46:06 +0100 Subject: [PATCH 1/4] PERF: avoid default formatter repeating lookup options --- pandas/io/formats/style.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index cc5f3164385cb..78888b248a9be 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -198,7 +198,11 @@ def __init__( self.tooltips: Optional[_Tooltips] = None self._display_funcs: DefaultDict[ # maps (row, col) -> formatting function Tuple[int, int], Callable[[Any], str] - ] = defaultdict(lambda: partial(_default_formatter, precision=None)) + ] = defaultdict( + lambda: partial( + _default_formatter, precision=get_option("display.precision") + ) + ) self.precision = precision # can be removed on set_precision depr cycle self.na_rep = na_rep # can be removed on set_na_rep depr cycle self.format(formatter=None, precision=precision, na_rep=na_rep) @@ -2127,7 +2131,7 @@ def _get_level_lengths(index, hidden_elements=None): return non_zero_lengths -def _default_formatter(x: Any, precision: Optional[int] = None) -> Any: +def _default_formatter(x: Any, precision: int) -> Any: """ Format the display of a value @@ -2143,8 +2147,6 @@ def _default_formatter(x: Any, precision: Optional[int] = None) -> Any: value : Any Matches input type, or string if input is float or complex. """ - if precision is None: - precision = get_option("display.precision") if isinstance(x, (float, complex)): return f"{x:.{precision}f}" return x @@ -2165,6 +2167,7 @@ def _maybe_wrap_formatter( elif callable(formatter): formatter_func = formatter elif formatter is None: + precision = get_option("display.precision") if precision is None else precision formatter_func = partial(_default_formatter, precision=precision) else: raise TypeError(f"'formatter' expected str or callable, got {type(formatter)}") From f7877758cf4e3fbddfd52a59bdd5c243d1223fac Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 14 Mar 2021 10:00:57 +0100 Subject: [PATCH 2/4] PERF: avoid default formatter repeating lookup options --- pandas/io/formats/style.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 78888b248a9be..4f0c3663737f2 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -196,13 +196,10 @@ def __init__( self.cell_context: Dict[str, Any] = {} self._todo: List[Tuple[Callable, Tuple, Dict]] = [] self.tooltips: Optional[_Tooltips] = None + def_precision = get_option("display.precision") self._display_funcs: DefaultDict[ # maps (row, col) -> formatting function Tuple[int, int], Callable[[Any], str] - ] = defaultdict( - lambda: partial( - _default_formatter, precision=get_option("display.precision") - ) - ) + ] = defaultdict(lambda: partial(_default_formatter, precision=def_precision)) self.precision = precision # can be removed on set_precision depr cycle self.na_rep = na_rep # can be removed on set_na_rep depr cycle self.format(formatter=None, precision=precision, na_rep=na_rep) From c73d841e0c580688a59a2077e0342272851fdd8a Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 14 Mar 2021 10:13:11 +0100 Subject: [PATCH 3/4] doc chg --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 4f0c3663737f2..2ede8789b164e 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -2136,7 +2136,7 @@ def _default_formatter(x: Any, precision: int) -> Any: ---------- x : Any Input variable to be formatted - precision : Int, optional + precision : Int Floating point precision used if ``x`` is float or complex. Returns From 109c9987768e42b7a0aa2697d31e98fdc53854e7 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sun, 14 Mar 2021 10:14:34 +0100 Subject: [PATCH 4/4] whats new --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 56a5412d4ecfc..3acd9f3548702 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -367,7 +367,7 @@ Deprecations - Deprecated :meth:`core.window.ewm.ExponentialMovingWindow.vol` (:issue:`39220`) - Using ``.astype`` to convert between ``datetime64[ns]`` dtype and :class:`DatetimeTZDtype` is deprecated and will raise in a future version, use ``obj.tz_localize`` or ``obj.dt.tz_localize`` instead (:issue:`38622`) - Deprecated casting ``datetime.date`` objects to ``datetime64`` when used as ``fill_value`` in :meth:`DataFrame.unstack`, :meth:`DataFrame.shift`, :meth:`Series.shift`, and :meth:`DataFrame.reindex`, pass ``pd.Timestamp(dateobj)`` instead (:issue:`39767`) -- Deprecated :meth:`.Styler.set_na_rep` and :meth:`.Styler.set_precision` in favour of :meth:`.Styler.format` with ``na_rep`` and ``precision`` as existing and new input arguments respectively (:issue:`40134`) +- Deprecated :meth:`.Styler.set_na_rep` and :meth:`.Styler.set_precision` in favour of :meth:`.Styler.format` with ``na_rep`` and ``precision`` as existing and new input arguments respectively (:issue:`40134`, :issue:`40425`) - Deprecated allowing partial failure in :meth:`Series.transform` and :meth:`DataFrame.transform` when ``func`` is list-like or dict-like; will raise if any function fails on a column in a future version (:issue:`40211`) .. ---------------------------------------------------------------------------