Closed ldouteau closed 3 weeks ago
MultiIndex
is a subclass of Index
, so the result for index
is technically correct. From a static typing perspective, we can't track the type of the index that is inside of a DataFrame
or even the type of Index
backing the columns
. We've chosen the most common values, so that most people don't have to cast
the result. If you know that df.index
or df.columns
is a MultiIndex
, you will have to cast
the result.
Part of the issue here is that if you have a DataFrame
, you can call set_index()
, and that could make the index of the DF either a regular Index
or a MultiIndex
. Tracking that with static typing doesn't seem possible.
I'm closing this, but am willing to reopen if you have suggestions for handling this.
MultiIndex
is a subclass ofIndex
Got it, it missed that. Then the typing for df.index
is ok, but df.columns
should use Index
instead of Index[str]
If you know that
df.index
ordf.columns
is aMultiIndex
, you will have tocast
the result.
That's what i ended up doing, even though i don't like to add those lines. It's quite sensitive to API changes IMO
Got it, it missed that. Then the typing for
df.index
is ok, butdf.columns
should useIndex
instead ofIndex[str]
I think we used to have that, but that meant there were certain cases where you'd have to do an inconvenient cast
. For example, if you did something like df[df.columns[0]]
, you'd get a typing error, because df.columns[0]
is a Scalar
Hi,
Describe the bug The type returned by
DataFrame.index
andDataFrame.columns
is, respectively,Index
andIndex[str]
. This is invalid when the dataframe uses MultiIndex for their index and/or columnsTo Reproduce 1.
idx
andcols
, doesn't match the expected multiindexPlease complete the following information:
Additional context Properties implemented in https://github.com/pandas-dev/pandas-stubs/blob/b246fcff196b70e995a0c61ffa3ab78a64c07e7d/pandas-stubs/core/frame.pyi#L1519 and https://github.com/pandas-dev/pandas-stubs/blob/b246fcff196b70e995a0c61ffa3ab78a64c07e7d/pandas-stubs/core/frame.pyi#L1533
Getters should be updated too I guess, not sure if this should be a specific issue