Skip to content
Open
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
12 changes: 10 additions & 2 deletions pandas-stubs/io/formats/style.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class _SeriesFunc(Protocol):
self, series: Series, /, *args: Any, **kwargs: Any
) -> list[Any] | Series: ...

class _SeriesStrFunc(Protocol):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I tried on my end and this protocol thing works but is a bit of overkill for what you are trying to do here. Turns out since args and kwargs here are just Any, you can drop it in exchange for:
Callable[Concatenate[Series, ...]], list[str] | np_ndarray_str | Series[str]]
This will make it a lot cleaner and simpler to understand.

def __call__(
self, series: Series[str], /, *args: Any, **kwargs: Any
) -> list[str] | Series[str]: ...

class _DataFrameFunc(Protocol):
def __call__(
self, series: DataFrame, /, *args: Any, **kwargs: Any
Expand Down Expand Up @@ -277,14 +282,17 @@ class Styler(StylerRenderer):
) -> Styler: ...
def apply_index(
self,
func: Callable[[Series], list[str] | np_ndarray_str | Series[str]],
func: (
_SeriesStrFunc
| Callable[[Series], list[str] | np_ndarray_str | Series[str]]
),
axis: Axis = ...,
level: Level | list[Level] | None = ...,
**kwargs: Any,
) -> Styler: ...
def map_index(
self,
func: Callable[[Scalar], str | None],
func: _MapCallable | Callable[[Scalar], str | None],
axis: Axis = ...,
level: Level | list[Level] | None = ...,
**kwargs: Any,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,26 @@ def f1(s: Series) -> Series[str]:

check(assert_type(DF.style.apply_index(f1), Styler), Styler)

# GH 1723
def highlight_odd(index: pd.Series, /, color: str) -> list[str]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need for the /, it should work without it since it is hardly a usecase you see in production code.

return [f"color: {color}" if x % 2 else "" for x in index]

check(
assert_type(DF.style.apply_index(highlight_odd, color="purple"), Styler), Styler
)


def test_map_index() -> None:
def f(s: Scalar) -> str | None:
return "background-color: yellow;" if s == "B" else None

check(assert_type(DF.style.map_index(f), Styler), Styler)

def f1(s: Scalar, /, color: str) -> str | None:
return f"background-color: {color};" if s == "b" else None

check(assert_type(DF.style.map_index(f1, color="pink"), Styler), Styler)


def test_background_gradient() -> None:
check(assert_type(DF.style.background_gradient(), Styler), Styler)
Expand Down