Skip to content

Commit a515bdd

Browse files
authored
cln: reorganise vars (#39642)
Co-authored-by: JHM Darbyshire (iMac) <attack68@users.noreply.github.com>
1 parent 2521fab commit a515bdd

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

pandas/io/formats/style.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,53 +157,40 @@ def __init__(
157157
na_rep: Optional[str] = None,
158158
uuid_len: int = 5,
159159
):
160-
self.ctx: DefaultDict[Tuple[int, int], List[str]] = defaultdict(list)
161-
self._todo: List[Tuple[Callable, Tuple, Dict]] = []
162-
160+
# validate ordered args
163161
if not isinstance(data, (pd.Series, pd.DataFrame)):
164162
raise TypeError("``data`` must be a Series or DataFrame")
165163
if data.ndim == 1:
166164
data = data.to_frame()
167165
if not data.index.is_unique or not data.columns.is_unique:
168166
raise ValueError("style is not supported for non-unique indices.")
169-
170-
self.data = data
171-
self.index = data.index
172-
self.columns = data.columns
173-
167+
assert isinstance(data, DataFrame)
168+
self.data: DataFrame = data
169+
self.index: pd.Index = data.index
170+
self.columns: pd.Index = data.columns
171+
if precision is None:
172+
precision = get_option("display.precision")
173+
self.precision = precision
174+
self.table_styles = table_styles
174175
if not isinstance(uuid_len, int) or not uuid_len >= 0:
175176
raise TypeError("``uuid_len`` must be an integer in range [0, 32].")
176177
self.uuid_len = min(32, uuid_len)
177178
self.uuid = (uuid or uuid4().hex[: self.uuid_len]) + "_"
178-
self.table_styles = table_styles
179179
self.caption = caption
180-
if precision is None:
181-
precision = get_option("display.precision")
182-
self.precision = precision
183180
self.table_attributes = table_attributes
184-
self.hidden_index = False
185-
self.hidden_columns: Sequence[int] = []
186181
self.cell_ids = cell_ids
187182
self.na_rep = na_rep
188183

189-
self.tooltips: Optional[_Tooltips] = None
190-
184+
# assign additional default vars
185+
self.hidden_index: bool = False
186+
self.hidden_columns: Sequence[int] = []
187+
self.ctx: DefaultDict[Tuple[int, int], List[str]] = defaultdict(list)
191188
self.cell_context: Dict[str, Any] = {}
192-
193-
# display_funcs maps (row, col) -> formatting function
194-
195-
def default_display_func(x):
196-
if self.na_rep is not None and pd.isna(x):
197-
return self.na_rep
198-
elif is_float(x):
199-
display_format = f"{x:.{self.precision}f}"
200-
return display_format
201-
else:
202-
return x
203-
204-
self._display_funcs: DefaultDict[
189+
self._todo: List[Tuple[Callable, Tuple, Dict]] = []
190+
self.tooltips: Optional[_Tooltips] = None
191+
self._display_funcs: DefaultDict[ # maps (row, col) -> formatting function
205192
Tuple[int, int], Callable[[Any], str]
206-
] = defaultdict(lambda: default_display_func)
193+
] = defaultdict(lambda: self._default_display_func)
207194

208195
def _repr_html_(self) -> str:
209196
"""
@@ -224,6 +211,15 @@ def _init_tooltips(self):
224211
if self.tooltips is None:
225212
self.tooltips = _Tooltips()
226213

214+
def _default_display_func(self, x):
215+
if self.na_rep is not None and pd.isna(x):
216+
return self.na_rep
217+
elif is_float(x):
218+
display_format = f"{x:.{self.precision}f}"
219+
return display_format
220+
else:
221+
return x
222+
227223
def set_tooltips(self, ttips: DataFrame) -> Styler:
228224
"""
229225
Add string based tooltips that will appear in the `Styler` HTML result. These

0 commit comments

Comments
 (0)