Skip to content

TYPING: df.columns.levels raises mypy error on MultiIndex for columns #36307

Closed
@hweecat

Description

@hweecat

Observed in PR #36305

When adding check on inferred type for each level in MultiIndex for columns, error message pops up in CI (Typing validation with mypy):

pandas/io/parquet.py:59: error: "Index" has no attribute "levels"; maybe "nlevels"? [attr-defined]

I understand that single-level Indexes do not have the "levels" attribute while MultiIndexes have the "levels" attribute. It seems that mypy static type checking may have inferred "df.columns" as Index type by default, hence flagging the error when using "df.columns.levels" in the code change.

Would it make sense to add a "levels" attribute to "Index" for consistency with MultiIndex, or are there any possible workarounds for this mypy error?

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