Skip to content

Subclassing pandas DataFrame results in mypy misinterpreting indexers return #855

Closed
@Rhapso10

Description

@Rhapso10

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions