Skip to content

DataArray.to_series() creates a MultiIndex even for 1D data #1548

Closed
@shoyer

Description

@shoyer

xref #1542

This appears when using the development version of pandas:

In [7]: import pandas as pd

In [8]: pd.Series(range(5))
Out[8]:
0    0
1    1
2    2
3    3
4    4
dtype: int64

In [9]: pd.Series(range(5)).to_xarray().to_series()
Out[9]:
index
0        0
1        1
2        2
3        3
4        4
dtype: int64

In [10]: pd.Series(range(5)).to_xarray().to_series().index
Out[10]:
MultiIndex(levels=[[0, 1, 2, 3, 4]],
           labels=[[0, 1, 2, 3, 4]],
           names=['index'])

I think the problem boils down to these lines:

def to_index(self, ordered_dims=None):
"""Convert all index coordinates into a :py:class:`pandas.MultiIndex`
"""
if ordered_dims is None:
ordered_dims = self.dims
indexes = [self._data.get_index(k) for k in ordered_dims]
return pd.MultiIndex.from_product(indexes, names=list(ordered_dims))

We really want a normal Index for the coordinates from 1D data, not a MultiIndex. MultiIndex.from_product used to return different types depending on how it was called, but in pandas-dev/pandas#17236 it was fixed to always return a MultiIndex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions