Skip to content

Index.name and rename_axis have inconsistent types #1832

Description

@Dr-Irv

Describe the bug

If you grab the name of an Index as Index.name, you should be able to use that value in rename_axis

To Reproduce

import pandas as pd

def indexchg(df: pd.DataFrame) -> pd.DataFrame:
    idx_name = df.index.name
    result = df.set_index("foo")
    result = result.reset_index().rename_axis(idx_name)
    return result

error from pyright

indexasym.py
  c:\Code\pandas-stubs\play\indexasym.py:6:38 - error: Argument of type "Hashable | None" cannot be assigned to parameter "mapper" of type "Scalar | ListLike | None" in function "rename_axis"
    Type "Hashable | None" is not assignable to type "Scalar | ListLike | None"
      Type "Hashable" is not assignable to type "Scalar | ListLike | None"
        "Hashable" is not assignable to "str"
        "Hashable" is not assignable to "bytes"
        "Hashable" is not assignable to "date"
        "Hashable" is not assignable to "datetime"
        "Hashable" is not assignable to "timedelta"
        "Hashable" is not assignable to "datetime64[Any]"
    ... (reportArgumentType)

System specifications

  • OS and its version: Windows 11
  • python version: 3.12
  • type checker and its version: pyright 1.1.411
  • version of installed pandas-stubs: version on main commit 2b7f9f41f103cfec140888f4dae19724c83ea580

Additional context

I'm not sure if the right way to handle this is to make Index.name return Scalar | None or to modify rename_axis() to accept Hashable . Open to either.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BlockedRequires external dependency before progressingIndexRelated to the Index class or subclasses

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions