Skip to content

Commit b7d85d6

Browse files
authored
REF: axis in [...] becomes _get_axis_number(axis) in Styler (#43078)
1 parent f5d1cac commit b7d85d6

File tree

3 files changed

+20
-34
lines changed

3 files changed

+20
-34
lines changed

pandas/io/formats/style.py

+17-31
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def _update_ctx(self, attrs: DataFrame) -> None:
10121012
i, j = self.index.get_loc(rn), self.columns.get_loc(cn)
10131013
self.ctx[(i, j)].extend(css_list)
10141014

1015-
def _update_ctx_header(self, attrs: DataFrame, axis: str) -> None:
1015+
def _update_ctx_header(self, attrs: DataFrame, axis: int) -> None:
10161016
"""
10171017
Update the state of the ``Styler`` for header cells.
10181018
@@ -1025,15 +1025,15 @@ def _update_ctx_header(self, attrs: DataFrame, axis: str) -> None:
10251025
integer index.
10261026
Whitespace shouldn't matter and the final trailing ';' shouldn't
10271027
matter.
1028-
axis : str
1028+
axis : int
10291029
Identifies whether the ctx object being updated is the index or columns
10301030
"""
10311031
for j in attrs.columns:
10321032
for i, c in attrs[[j]].itertuples():
10331033
if not c:
10341034
continue
10351035
css_list = maybe_convert_css_to_tuples(c)
1036-
if axis == "index":
1036+
if axis == 0:
10371037
self.ctx_index[(i, j)].extend(css_list)
10381038
else:
10391039
self.ctx_columns[(j, i)].extend(css_list)
@@ -1253,14 +1253,8 @@ def _apply_index(
12531253
method: str = "apply",
12541254
**kwargs,
12551255
) -> Styler:
1256-
if axis in [0, "index"]:
1257-
obj, axis = self.index, "index"
1258-
elif axis in [1, "columns"]:
1259-
obj, axis = self.columns, "columns"
1260-
else:
1261-
raise ValueError(
1262-
f"`axis` must be one of 0, 1, 'index', 'columns', got {axis}"
1263-
)
1256+
axis = self.data._get_axis_number(axis)
1257+
obj = self.index if axis == 0 else self.columns
12641258

12651259
levels_ = _refactor_levels(level, obj)
12661260
data = DataFrame(obj.to_list()).loc[:, levels_]
@@ -1709,14 +1703,9 @@ def set_sticky(
17091703
17101704
may produce strange behaviour due to CSS controls with missing elements.
17111705
"""
1712-
if axis in [0, "index"]:
1713-
axis, obj = 0, self.data.index
1714-
pixel_size = 75 if not pixel_size else pixel_size
1715-
elif axis in [1, "columns"]:
1716-
axis, obj = 1, self.data.columns
1717-
pixel_size = 25 if not pixel_size else pixel_size
1718-
else:
1719-
raise ValueError("`axis` must be one of {0, 1, 'index', 'columns'}")
1706+
axis = self.data._get_axis_number(axis)
1707+
obj = self.data.index if axis == 0 else self.data.columns
1708+
pixel_size = (75 if axis == 0 else 25) if not pixel_size else pixel_size
17201709

17211710
props = "position:sticky; background-color:white;"
17221711
if not isinstance(obj, pd.MultiIndex):
@@ -1901,10 +1890,9 @@ def set_table_styles(
19011890
more details.
19021891
"""
19031892
if isinstance(table_styles, dict):
1904-
if axis in [0, "index"]:
1905-
obj, idf = self.data.columns, ".col"
1906-
else:
1907-
obj, idf = self.data.index, ".row"
1893+
axis = self.data._get_axis_number(axis)
1894+
obj = self.data.index if axis == 1 else self.data.columns
1895+
idf = ".row" if axis == 1 else ".col"
19081896

19091897
table_styles = [
19101898
{
@@ -2869,15 +2857,13 @@ def highlight_quantile(
28692857
# after quantile is found along axis, e.g. along rows,
28702858
# applying the calculated quantile to alternate axis, e.g. to each column
28712859
kwargs = {"q": [q_left, q_right], "interpolation": interpolation}
2872-
if axis in [0, "index"]:
2873-
q = data.quantile(axis=axis, numeric_only=False, **kwargs)
2874-
axis_apply: int | None = 1
2875-
elif axis in [1, "columns"]:
2876-
q = data.quantile(axis=axis, numeric_only=False, **kwargs)
2877-
axis_apply = 0
2878-
else: # axis is None
2860+
if axis is None:
28792861
q = Series(data.to_numpy().ravel()).quantile(**kwargs)
2880-
axis_apply = None
2862+
axis_apply: int | None = None
2863+
else:
2864+
axis = self.data._get_axis_number(axis)
2865+
q = data.quantile(axis=axis, numeric_only=False, **kwargs)
2866+
axis_apply = 1 - axis
28812867

28822868
if props is None:
28832869
props = f"background-color: {color};"

pandas/tests/io/formats/style/test_html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def test_sticky_levels(styler_mi, index, columns):
374374

375375

376376
def test_sticky_raises(styler):
377-
with pytest.raises(ValueError, match="`axis` must be"):
377+
with pytest.raises(ValueError, match="No axis named bad for object type DataFrame"):
378378
styler.set_sticky(axis="bad")
379379

380380

pandas/tests/io/formats/style/test_style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ def test_apply_map_header_mi(mi_styler, method, axis):
345345

346346
def test_apply_map_header_raises(mi_styler):
347347
# GH 41893
348-
with pytest.raises(ValueError, match="`axis` must be one of 0, 1, 'index', 'col"):
349-
mi_styler.applymap_index(lambda v: "attr: val;", axis="bad-axis")._compute()
348+
with pytest.raises(ValueError, match="No axis named bad for object type DataFrame"):
349+
mi_styler.applymap_index(lambda v: "attr: val;", axis="bad")._compute()
350350

351351

352352
class TestStyler:

0 commit comments

Comments
 (0)