Skip to content

Add stub for Styler.map_index #905

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 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions pandas-stubs/io/formats/style.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ class Styler(StylerRenderer):
level: Level | list[Level] | None = ...,
**kwargs: Any,
) -> Styler: ...
def map_index(
self,
func: Callable[[Series], npt.NDArray[np.str_] | list[str] | Series[str]],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Based on the docs, I think this should be Callable[[Scalar], str]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I updated to Callable[[Scalar], str | None] to match the docs

axis: Axis = ...,
level: Level | list[Level] | None = ...,
**kwargs,
) -> Styler: ...
def set_table_attributes(self, attributes: str) -> Styler: ...
def export(self) -> StyleExportDict: ...
def use(self, styles: StyleExportDict) -> Styler: ...
Expand Down
12 changes: 12 additions & 0 deletions tests/test_styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ def f1(s: Series) -> Series[str]:
check(assert_type(DF.style.apply_index(f1), Styler), Styler)


def test_map_index() -> None:
def f(s: Series) -> npt.NDArray[np.str_]:
return np.asarray(s, dtype=np.str_)

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

def f1(s: Series) -> Series[str]:
return Series(s, dtype=str)

check(assert_type(DF.style.map_index(f1), Styler), Styler)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The tests should be modified to something similar to what you see in the docs for map_index()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to show the first example from the docs. The second one showed the following error

    def f1(s: Scalar):
        return "background-color: yellow;" if "x" in s else None
                                              ~~~~~~~~
Unsupported right operand type for in ("str | bytes | date | datetime | timedelta | datetime64 | timedelta64 | bool | int | float | Timestamp | Timedelta | complex")



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

Expand Down