Skip to content

TYP: define subset arg in Styler #41433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
CSSProperties,
CSSStyles,
StylerRenderer,
Subset,
Tooltips,
maybe_convert_css_to_tuples,
non_reducing_slice,
Expand Down Expand Up @@ -545,7 +546,7 @@ def _apply(
self,
func: Callable[..., Styler],
axis: Axis | None = 0,
subset=None,
subset: Subset | None = None,
**kwargs,
) -> Styler:
subset = slice(None) if subset is None else subset
Expand Down Expand Up @@ -590,7 +591,7 @@ def apply(
self,
func: Callable[..., Styler],
axis: Axis | None = 0,
subset=None,
subset: Subset | None = None,
**kwargs,
) -> Styler:
"""
Expand Down Expand Up @@ -651,7 +652,9 @@ def apply(
)
return self

def _applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
def _applymap(
self, func: Callable, subset: Subset | None = None, **kwargs
) -> Styler:
func = partial(func, **kwargs) # applymap doesn't take kwargs?
if subset is None:
subset = pd.IndexSlice[:]
Expand All @@ -660,7 +663,9 @@ def _applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
self._update_ctx(result)
return self

def applymap(self, func: Callable, subset=None, **kwargs) -> Styler:
def applymap(
self, func: Callable, subset: Subset | None = None, **kwargs
) -> Styler:
"""
Apply a CSS-styling function elementwise.

Expand Down Expand Up @@ -707,7 +712,7 @@ def where(
cond: Callable,
value: str,
other: str | None = None,
subset=None,
subset: Subset | None = None,
**kwargs,
) -> Styler:
"""
Expand Down Expand Up @@ -1061,7 +1066,7 @@ def hide_index(self) -> Styler:
self.hidden_index = True
return self

def hide_columns(self, subset) -> Styler:
def hide_columns(self, subset: Subset) -> Styler:
"""
Hide columns from rendering.

Expand Down Expand Up @@ -1093,7 +1098,7 @@ def background_gradient(
low: float = 0,
high: float = 0,
axis: Axis | None = 0,
subset=None,
subset: Subset | None = None,
text_color_threshold: float = 0.408,
vmin: float | None = None,
vmax: float | None = None,
Expand Down Expand Up @@ -1239,7 +1244,7 @@ def background_gradient(
)
return self

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

def bar(
self,
subset=None,
subset: Subset | None = None,
axis: Axis | None = 0,
color="#d65f5f",
width: float = 100,
Expand Down Expand Up @@ -1417,7 +1422,7 @@ def bar(
def highlight_null(
self,
null_color: str = "red",
subset: IndexLabel | None = None,
subset: Subset | None = None,
props: str | None = None,
) -> Styler:
"""
Expand Down Expand Up @@ -1462,7 +1467,7 @@ def f(data: DataFrame, props: str) -> np.ndarray:

def highlight_max(
self,
subset: IndexLabel | None = None,
subset: Subset | None = None,
color: str = "yellow",
axis: Axis | None = 0,
props: str | None = None,
Expand Down Expand Up @@ -1511,7 +1516,7 @@ def f(data: FrameOrSeries, props: str) -> np.ndarray:

def highlight_min(
self,
subset: IndexLabel | None = None,
subset: Subset | None = None,
color: str = "yellow",
axis: Axis | None = 0,
props: str | None = None,
Expand Down Expand Up @@ -1560,7 +1565,7 @@ def f(data: FrameOrSeries, props: str) -> np.ndarray:

def highlight_between(
self,
subset: IndexLabel | None = None,
subset: Subset | None = None,
color: str = "yellow",
axis: Axis | None = 0,
left: Scalar | Sequence | None = None,
Expand Down Expand Up @@ -1667,7 +1672,7 @@ def highlight_between(

def highlight_quantile(
self,
subset: IndexLabel | None = None,
subset: Subset | None = None,
color: str = "yellow",
axis: Axis | None = 0,
q_left: float = 0.0,
Expand Down
9 changes: 6 additions & 3 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class CSSDict(TypedDict):


CSSStyles = List[CSSDict]
Subset = Union[slice, Sequence, Index]


class StylerRenderer:
Expand Down Expand Up @@ -402,7 +403,7 @@ def _translate_body(
def format(
self,
formatter: ExtFormatter | None = None,
subset: slice | Sequence[Any] | None = None,
subset: Subset | None = None,
na_rep: str | None = None,
precision: int | None = None,
decimal: str = ".",
Expand Down Expand Up @@ -772,7 +773,7 @@ def _maybe_wrap_formatter(
return lambda x: na_rep if isna(x) else func_2(x)


def non_reducing_slice(slice_):
def non_reducing_slice(slice_: Subset):
"""
Ensure that a slice doesn't reduce to a Series or Scalar.

Expand Down Expand Up @@ -809,7 +810,9 @@ def pred(part) -> bool:
# slice(a, b, c)
slice_ = [slice_] # to tuplize later
else:
slice_ = [part if pred(part) else [part] for part in slice_]
# error: Item "slice" of "Union[slice, Sequence[Any]]" has no attribute
# "__iter__" (not iterable) -> is specifically list_like in conditional
slice_ = [p if pred(p) else [p] for p in slice_] # type: ignore[union-attr]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you return this directly it should work (as you are re-assigning to a different type)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it wasnt the reassignment it was the fact that slice_ is not always considered to be iterable inside the loop comprehension. Albeit in this clause we have already checked that it is list_like so is iterable here...

return tuple(slice_)


Expand Down