Skip to content

Commit bbe0883

Browse files
attack68JulianWgs
authored andcommitted
type subset (pandas-dev#41433)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent f85ad6a commit bbe0883

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

pandas/io/formats/style.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
CSSProperties,
4444
CSSStyles,
4545
StylerRenderer,
46+
Subset,
4647
Tooltips,
4748
maybe_convert_css_to_tuples,
4849
non_reducing_slice,
@@ -545,7 +546,7 @@ def _apply(
545546
self,
546547
func: Callable[..., Styler],
547548
axis: Axis | None = 0,
548-
subset=None,
549+
subset: Subset | None = None,
549550
**kwargs,
550551
) -> Styler:
551552
subset = slice(None) if subset is None else subset
@@ -590,7 +591,7 @@ def apply(
590591
self,
591592
func: Callable[..., Styler],
592593
axis: Axis | None = 0,
593-
subset=None,
594+
subset: Subset | None = None,
594595
**kwargs,
595596
) -> Styler:
596597
"""
@@ -651,7 +652,9 @@ def apply(
651652
)
652653
return self
653654

654-
def _applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
655+
def _applymap(
656+
self, func: Callable, subset: Subset | None = None, **kwargs
657+
) -> Styler:
655658
func = partial(func, **kwargs) # applymap doesn't take kwargs?
656659
if subset is None:
657660
subset = pd.IndexSlice[:]
@@ -660,7 +663,9 @@ def _applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
660663
self._update_ctx(result)
661664
return self
662665

663-
def applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
666+
def applymap(
667+
self, func: Callable, subset: Subset | None = None, **kwargs
668+
) -> Styler:
664669
"""
665670
Apply a CSS-styling function elementwise.
666671
@@ -707,7 +712,7 @@ def where(
707712
cond: Callable,
708713
value: str,
709714
other: str | None = None,
710-
subset=None,
715+
subset: Subset | None = None,
711716
**kwargs,
712717
) -> Styler:
713718
"""
@@ -1061,7 +1066,7 @@ def hide_index(self) -> Styler:
10611066
self.hidden_index = True
10621067
return self
10631068

1064-
def hide_columns(self, subset) -> Styler:
1069+
def hide_columns(self, subset: Subset) -> Styler:
10651070
"""
10661071
Hide columns from rendering.
10671072
@@ -1093,7 +1098,7 @@ def background_gradient(
10931098
low: float = 0,
10941099
high: float = 0,
10951100
axis: Axis | None = 0,
1096-
subset=None,
1101+
subset: Subset | None = None,
10971102
text_color_threshold: float = 0.408,
10981103
vmin: float | None = None,
10991104
vmax: float | None = None,
@@ -1239,7 +1244,7 @@ def background_gradient(
12391244
)
12401245
return self
12411246

1242-
def set_properties(self, subset=None, **kwargs) -> Styler:
1247+
def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
12431248
"""
12441249
Set defined CSS-properties to each ``<td>`` HTML element within the given
12451250
subset.
@@ -1331,7 +1336,7 @@ def css(x):
13311336

13321337
def bar(
13331338
self,
1334-
subset=None,
1339+
subset: Subset | None = None,
13351340
axis: Axis | None = 0,
13361341
color="#d65f5f",
13371342
width: float = 100,
@@ -1417,7 +1422,7 @@ def bar(
14171422
def highlight_null(
14181423
self,
14191424
null_color: str = "red",
1420-
subset: IndexLabel | None = None,
1425+
subset: Subset | None = None,
14211426
props: str | None = None,
14221427
) -> Styler:
14231428
"""
@@ -1462,7 +1467,7 @@ def f(data: DataFrame, props: str) -> np.ndarray:
14621467

14631468
def highlight_max(
14641469
self,
1465-
subset: IndexLabel | None = None,
1470+
subset: Subset | None = None,
14661471
color: str = "yellow",
14671472
axis: Axis | None = 0,
14681473
props: str | None = None,
@@ -1511,7 +1516,7 @@ def f(data: FrameOrSeries, props: str) -> np.ndarray:
15111516

15121517
def highlight_min(
15131518
self,
1514-
subset: IndexLabel | None = None,
1519+
subset: Subset | None = None,
15151520
color: str = "yellow",
15161521
axis: Axis | None = 0,
15171522
props: str | None = None,
@@ -1560,7 +1565,7 @@ def f(data: FrameOrSeries, props: str) -> np.ndarray:
15601565

15611566
def highlight_between(
15621567
self,
1563-
subset: IndexLabel | None = None,
1568+
subset: Subset | None = None,
15641569
color: str = "yellow",
15651570
axis: Axis | None = 0,
15661571
left: Scalar | Sequence | None = None,
@@ -1667,7 +1672,7 @@ def highlight_between(
16671672

16681673
def highlight_quantile(
16691674
self,
1670-
subset: IndexLabel | None = None,
1675+
subset: Subset | None = None,
16711676
color: str = "yellow",
16721677
axis: Axis | None = 0,
16731678
q_left: float = 0.0,

pandas/io/formats/style_render.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CSSDict(TypedDict):
5555

5656

5757
CSSStyles = List[CSSDict]
58+
Subset = Union[slice, Sequence, Index]
5859

5960

6061
class StylerRenderer:
@@ -402,7 +403,7 @@ def _translate_body(
402403
def format(
403404
self,
404405
formatter: ExtFormatter | None = None,
405-
subset: slice | Sequence[Any] | None = None,
406+
subset: Subset | None = None,
406407
na_rep: str | None = None,
407408
precision: int | None = None,
408409
decimal: str = ".",
@@ -772,7 +773,7 @@ def _maybe_wrap_formatter(
772773
return lambda x: na_rep if isna(x) else func_2(x)
773774

774775

775-
def non_reducing_slice(slice_):
776+
def non_reducing_slice(slice_: Subset):
776777
"""
777778
Ensure that a slice doesn't reduce to a Series or Scalar.
778779
@@ -809,7 +810,9 @@ def pred(part) -> bool:
809810
# slice(a, b, c)
810811
slice_ = [slice_] # to tuplize later
811812
else:
812-
slice_ = [part if pred(part) else [part] for part in slice_]
813+
# error: Item "slice" of "Union[slice, Sequence[Any]]" has no attribute
814+
# "__iter__" (not iterable) -> is specifically list_like in conditional
815+
slice_ = [p if pred(p) else [p] for p in slice_] # type: ignore[union-attr]
813816
return tuple(slice_)
814817

815818

0 commit comments

Comments
 (0)