Skip to content

Commit cd5f15d

Browse files
authored
PERF: make Styler default formatter arguments statics instead of repeated dynamic (#40425)
1 parent ed529a1 commit cd5f15d

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ Deprecations
367367
- Deprecated :meth:`core.window.ewm.ExponentialMovingWindow.vol` (:issue:`39220`)
368368
- 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`)
369369
- 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`)
370-
- 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`)
370+
- 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`)
371371
- 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`)
372372

373373
.. ---------------------------------------------------------------------------

pandas/io/formats/style.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,10 @@ def __init__(
196196
self.cell_context: Dict[str, Any] = {}
197197
self._todo: List[Tuple[Callable, Tuple, Dict]] = []
198198
self.tooltips: Optional[_Tooltips] = None
199+
def_precision = get_option("display.precision")
199200
self._display_funcs: DefaultDict[ # maps (row, col) -> formatting function
200201
Tuple[int, int], Callable[[Any], str]
201-
] = defaultdict(lambda: partial(_default_formatter, precision=None))
202+
] = defaultdict(lambda: partial(_default_formatter, precision=def_precision))
202203
self.precision = precision # can be removed on set_precision depr cycle
203204
self.na_rep = na_rep # can be removed on set_na_rep depr cycle
204205
self.format(formatter=None, precision=precision, na_rep=na_rep)
@@ -2127,24 +2128,22 @@ def _get_level_lengths(index, hidden_elements=None):
21272128
return non_zero_lengths
21282129

21292130

2130-
def _default_formatter(x: Any, precision: Optional[int] = None) -> Any:
2131+
def _default_formatter(x: Any, precision: int) -> Any:
21312132
"""
21322133
Format the display of a value
21332134
21342135
Parameters
21352136
----------
21362137
x : Any
21372138
Input variable to be formatted
2138-
precision : Int, optional
2139+
precision : Int
21392140
Floating point precision used if ``x`` is float or complex.
21402141
21412142
Returns
21422143
-------
21432144
value : Any
21442145
Matches input type, or string if input is float or complex.
21452146
"""
2146-
if precision is None:
2147-
precision = get_option("display.precision")
21482147
if isinstance(x, (float, complex)):
21492148
return f"{x:.{precision}f}"
21502149
return x
@@ -2165,6 +2164,7 @@ def _maybe_wrap_formatter(
21652164
elif callable(formatter):
21662165
formatter_func = formatter
21672166
elif formatter is None:
2167+
precision = get_option("display.precision") if precision is None else precision
21682168
formatter_func = partial(_default_formatter, precision=precision)
21692169
else:
21702170
raise TypeError(f"'formatter' expected str or callable, got {type(formatter)}")

0 commit comments

Comments
 (0)