Skip to content

BUG: In Multilevel.dtypes when level name is not specified it gives wrong output #38580

Closed
@gurukiran07

Description

@gurukiran07
  • I have checked that this issue has not already been reported.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


There was a recent PR to add MultiIndex.dtypes #37073 but this only works when each level is named.

@cache_readonly
def dtypes(self) -> "Series":
"""
Return the dtypes as a Series for the underlying MultiIndex
"""
from pandas import Series
return Series({level.name: level.dtype for level in self.levels})

If level has no name then, level.name would be None.

Example where it fails:

idx_multitype = pd.MultiIndex.from_product(
     [[1, 2, 3], ["a", "b", "c"], pd.date_range("20200101", periods=2, tz="UTC")],
 )
{level.name: level.dtype for level in idx_multitype.levels}
# {None: datetime64[ns, UTC]}

When level is unamed i.e None may be we can add level_0, level_1 etc. this would be consistent with API too(reset_index would add level_x).

@jreback
@arw2019

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions