Skip to content

pandas Styler has no .columns attr defined #1102

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

Closed
thatlittleboy opened this issue Jan 29, 2025 · 6 comments · Fixed by #1135
Closed

pandas Styler has no .columns attr defined #1102

thatlittleboy opened this issue Jan 29, 2025 · 6 comments · Fixed by #1135
Labels
good first issue Styler conditional formatting using DataFrame.style

Comments

@thatlittleboy
Copy link

Describe the bug

pandas Styler has no .columns attr defined in the stubs.

To Reproduce

  1. Minimal example, save in a file named t.py
from typing import reveal_type
import pandas as pd
from pandas.io.formats import style as pd_style


def f(s: pd_style.Styler) -> pd_style.Styler:
    print(s.columns)
    fmt = "{:.2%}"
    return s.format(fmt)


df = pd.DataFrame({"a": [1, 2, 3]})
df2 = df.style.pipe(f)
reveal_type(df2)
  1. Run with mypy mypy t.py
  2. error message
t.py:7: error: "Styler" has no attribute "columns"  [attr-defined]
t.py:14: note: Revealed type is "pandas.io.formats.style.Styler"
Found 1 error in 1 file (checked 1 source file)

Please complete the following information:

  • OS: MacOS
  • OS Version Sonoma 14.4.1
  • python version 3.12
  • version of type checker mypy 1.14.1 (compiled: yes)
  • version of installed pandas-stubs 2.2.3.241126

Additional context

the StyleRenderer base class has columns defined in __init__.

https://github.com/pandas-dev/pandas/blob/8973c551895c2cd3619cadf554362e802b27e02a/pandas/io/formats/style_render.py#L100

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 29, 2025

Issue here is that Styler.columns is not documented in pandas, and the stubs only supports what is documented.

I've created pandas-dev/pandas#60815 . Once that is resolved, then a PR is welcome to fix this here.

@Dr-Irv Dr-Irv added Styler conditional formatting using DataFrame.style Docs pandas_docs For issues where there is a conflict in behavior with pandas docs and stubs that needs resolution and removed Docs labels Jan 29, 2025
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 21, 2025

I've created pandas-dev/pandas#60815 . Once that is resolved, then a PR is welcome to fix this here.

This is now fixed in pandas, so PR with test welcome.

@Dr-Irv Dr-Irv added good first issue and removed pandas_docs For issues where there is a conflict in behavior with pandas docs and stubs that needs resolution labels Feb 21, 2025
@Rishab260
Copy link
Contributor

take

@Rishab260
Copy link
Contributor

Hi @Dr-Irv , could you please specify the actionable items for this?

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 24, 2025

Hi @Dr-Irv , could you please specify the actionable items for this?

You'd have to add index and columns as properties in the stubs for Styler in /io/formats/style.pyi and then add tests in tests/test_styler.py

@Rishab260
Copy link
Contributor

Hi @Dr-Irv , could you please specify the actionable items for this?

You'd have to add index and columns as properties in the stubs for Styler in /io/formats/style.pyi and then add tests in tests/test_styler.py

Got it, thanks @Dr-Irv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants