Skip to content

Typing of DataFrame index and columns miss multiindex #994

Closed
@ldouteau

Description

@ldouteau

Hi,

Describe the bug
The type returned by DataFrame.index and DataFrame.columns is, respectively, Index and Index[str]. This is invalid when the dataframe uses MultiIndex for their index and/or columns

To Reproduce
1.

import numpy as np
import pandas as pd

df = pd.DataFrame(
    np.arange(6).reshape((3, 2)),
    index=pd.MultiIndex.from_product((("i",), ("j1", "j2", "j3"))),
    columns=pd.MultiIndex.from_product((("a",), ("b1", "b2"))),
)
idx = df.index  # type show as Index
cols = df.columns  # type shown as Index[str]
  1. pyright version 1.1.373, commit ee424479
  2. None. Just check the type revealed for idx and cols, doesn't match the expected multiindex

Please complete the following information:

  • Windows 11
  • python version 3.12.5
  • pyright version 1.1.373
  • pandas-stubs Version: 2.2.2.240807

Additional context
Properties implemented in

def columns(self) -> Index[str]: ...
and
def index(self) -> Index: ...

Getters should be updated too I guess, not sure if this should be a specific issue

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