Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jjpr-mit opened this issue Sep 6, 2017 · 3 comments

Comments

@jjpr-mit
Copy link

jjpr-mit commented Sep 6, 2017

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`.
@shoyer
Copy link
Member

shoyer commented Sep 6, 2017

Thanks for the report.

I agree that a better error message would be helpful here, but I do think that stack can still be meaningfully if one of the components is a MultiIndex (e.g., by combining the existing levels with the new levels to be stacked)

@nbren12
Copy link
Contributor

nbren12 commented Sep 18, 2017

I have been playing around with the MultiIndexes as part of #1317, so I could take a stab at implementing some sort of stack which combines the levels etc.

@mschrimpf
Copy link

Any updates on this by any chance? (now that there seems to be progress on #1603)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants