Skip to content

MultiIndex method corrections #1436

@Dr-Irv

Description

@Dr-Irv

Describe the bug
The methods MultiIndex.swaplevel() and MultiIndex.union() produce incorrect results:

  • swaplevel() is missing the return type of Self
  • union should be overridden, as the union of 2 MultiIndex should be a MultiIndex, not an Index

To Reproduce

from typing import reveal_type
import pandas as pd

mi = pd.MultiIndex.from_product([["a", "b"], [1, 2]], names=["let", "num"])
miswap = mi.swaplevel(0, 1)
reveal_type(miswap)
mi2= pd.MultiIndex.from_product([["a", "b"], [3, 4]], names=["let", "num"])
miu = mi.union(mi2)
reveal_type(miu)

At runtime, this produces:

Runtime type is 'MultiIndex'
Runtime type is 'MultiIndex'

With pyright and the code currently in master, I get:

  mitest.py:6:13 - information: Type of "miswap" is "Unknown"
  mitest.py:9:13 - information: Type of "miu" is "Index[Any]"

Please complete the following information:

  • OS: Windows 11
  • OS Version: ??
  • python version 3.11
  • version of type checker pyright 1.1.406
  • version of installed pandas-stubs From commit #71b53992643d62c41d60c9903417ecc631d3e05e

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions