pydata / pydata/xarray

TypeError on DataArray.stack() if any of the dimensions to be stacked has a MultiIndex

Open
#1,554 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

contrib-help-wanted topic-error reporting
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

If I call .stack() on a DataArray, and any of the dimensions to be stacked already has a MultiIndex on it, I get an error:

TypeError: Must pass list-like as `names`.

Maybe it doesn't make sense for .stack() to work on an existing MultiIndex, but that should probably be documented, and perhaps throw a more informative error.

Here's the full trace:

Traceback (most recent call last)
<ipython-input-6-ce81ac38d71e> in <module>()
----> 1 xr_one_flat_stacked = xr_one_flat.stack(alphanumeric=("letters", "numbers"))
      2 xr_one_flat_stacked

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/xarray/core/dataarray.pyc in stack(self, **dimensions)
   1027         DataArray.unstack
   1028         """
-> 1029         ds = self._to_temp_dataset().stack(**dimensions)
   1030         return self._from_temp_dataset(ds)
   1031 

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/xarray/core/dataset.pyc in stack(self, **dimensions)
   1832         result = self
   1833         for new_dim, dims in dimensions.items():
-> 1834             result = result._stack_once(dims, new_dim)
   1835         return result
   1836 

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/xarray/core/dataset.pyc in _stack_once(self, dims, new_dim)
   1801                       else level
   1802                       for level in levels]
-> 1803         idx = utils.multiindex_from_product_levels(levels, names=dims)
   1804         variables[new_dim] = IndexVariable(new_dim, idx)
   1805 

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/xarray/core/utils.pyc in multiindex_from_product_levels(levels, names)
     79                               indexing='ij')
     80     labels = [x.ravel() for x in labels_mesh]
---> 81     return pd.MultiIndex(levels, labels, sortorder=0, names=names)
     82 
     83 

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/pandas/indexes/multi.pyc in __new__(cls, levels, labels, sortorder, names, copy, verify_integrity, _set_identity, name, **kwargs)
    103         if names is not None:
    104             # handles name validation
--> 105             result._set_names(names)
    106 
    107         if sortorder is not None:

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/pandas/indexes/multi.pyc in _set_names(self, names, level, validate)
    505         # set the name
    506         for l, name in zip(level, names):
--> 507             self.levels[l].rename(name, inplace=True)
    508 
    509     names = property(fset=_set_names, fget=_get_names,

/Users/jjpr/anaconda/envs/dldata/lib/python2.7/site-packages/pandas/indexes/base.pyc in set_names(self, names, level, inplace)
    997 
    998         if not is_list_like(names) and level is None and self.nlevels > 1:
--> 999             raise TypeError("Must pass list-like as `names`.")
   1000 
   1001         if not is_list_like(names):

TypeError: Must pass list-like as `names`.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce DataArray.stack() with a dimension that already has a MultiIndex, then inspect the stack path in xarray/core/dataarray.py and xarray/core/dataset.py, especially _stack_once and the utility call shown in the trace. The issue leaves the desired behavior open; determine whether stacking should be supported or rejected clearly, then cover the chosen behavior with a regression test and documentation or an informative error.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.