pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.74k stars 17.95k forks source link

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

Closed gurukiran07 closed 3 years ago

gurukiran07 commented 3 years ago

There was a recent PR to add MultiIndex.dtypes #37073 but this only works when each level is named. https://github.com/pandas-dev/pandas/blob/03e1c899077d335a057a3f36c09645499638d417/pandas/core/indexes/multi.py#L703-L710

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

simonjayhawkins commented 3 years ago

Thanks @gurukiran07 for the report. PR to fix welcome.

gurukiran07 commented 3 years ago

@simonjayhawkins Made a PR #38582