Skip to content

DataFrame.index not reliably typed using Index[Any] #1548

@adamjstewart

Description

@adamjstewart

Describe the bug

DataFrame.index is currently typed as Index[Any]. As a result, certain statically typed valid code is flagged as invalid by mypy. I would argue that Any would be better until the index can be reliably typed.

To Reproduce

For the following code:

import pandas as pd

data = {'col1': [1, 2], 'col2': [3, 4]}
index = pd.interval_range(start=0, end=2)
df = pd.DataFrame(data, index)
print(df.index.left)
print(df.index.right)

mypy complains with:

test.py:6: error: "Index[Any]" has no attribute "left"  [attr-defined]
test.py:7: error: "Index[Any]" has no attribute "right"  [attr-defined]
Found 2 errors in 1 file (checked 1 source file)

This is because I am using an IntervalIndex, not a generic Index.

Please complete the following information:

  • OS: macOS
  • OS Version: 26.1
  • python version: 3.13.8
  • version of type checker: 1.19.0
  • version of installed pandas-stubs: 2.3.2.250926

Additional context

The issue of DataFrame.index type hints was also raised in pandas-dev/pandas#36708. This issue also impacts geopandas: python/typeshed#15111.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DataFrameDataFrame data structureIntervalInterval data type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions