Skip to content
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

Subclassing pandas DataFrame results in mypy misinterpreting indexers return #855

Open
Rhapso10 opened this issue Jan 23, 2024 · 1 comment

Comments

@Rhapso10
Copy link

Describe the bug

When subclassing pandas DataFrame by subclassing _constructor, type hints indicate that any return from a loc, iloc, etc... indexing is a pandas DataFrame not the subclass.
To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
import pandas as pd 


class MyClass(pd.DataFrame):
    @property
    def _constructor(self) -> 'type[MyClass]':
        return MyClass

df = MyClass({'a': [1, 2, 3]})
df2 = df.iloc[1:2]
reveal_type(df2)
  1. Indicate which type checker you are using (mypy or pyright).

I used mypy.

  1. Show the error message received from that type checker while checking your example.
image
  • OS: Linux
  • OS Version: Debian GNU/Linux 10
  • python version: 3.10.13
  • version of type checker : mypy==1.8.0
  • version of installed pandas-stubs: 2.1.4.231227
@twoertwein
Copy link
Member

We should probably return Self more often: after double checking that pandas actually returns the sub-class, for example, _consructor returns DataFrame unless a sub-class overwrites it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants