Skip to content

Raise NotImplementedError when attempting to save a MultiIndex to disk. #1713

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

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Bug fixes
DataArray constructor (:issue:`1709`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

- Raise ``NotImplementedError`` when attempting to save a MultiIndex to a
netCDF file (:issue:`1547`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

Testing
~~~~~~~

Expand Down
11 changes: 10 additions & 1 deletion xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from .core import duck_array_ops, indexing, ops, utils
from .core.formatting import format_timestamp, first_n_items, last_item
from .core.variable import as_variable, Variable
from .core.variable import as_variable, IndexVariable, Variable
from .core.pycompat import iteritems, OrderedDict, PY3, basestring


Expand Down Expand Up @@ -832,6 +832,15 @@ def _infer_dtype(array, name=None):
def ensure_dtype_not_object(var, name=None):
# TODO: move this from conventions to backends? (it's not CF related)
if var.dtype.kind == 'O':
if (isinstance(var, IndexVariable) and
isinstance(var.to_index(), pd.MultiIndex)):
raise NotImplementedError(
'variable {!r} is a MultiIndex, which cannot yet be '
'serialized to netCDF files '
'(https://github.com/pydata/xarray/issues/1077). Use '
'reset_index() to convert MultiIndex levels into coordinate '
'variables instead.'.format(name))

dims, data, attrs, encoding = _var_as_tuple(var)
missing = pd.isnull(data)
if missing.any():
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,13 @@ def test_append_overwrite_values(self):
def test_vectorized_indexing(self):
self._test_vectorized_indexing(vindex_support=False)

def test_multiindex_not_implemented(self):
ds = (Dataset(coords={'y': ('x', [1, 2]), 'z': ('x', ['a', 'b'])})
.set_index(x=['y', 'z']))
with raises_regex(NotImplementedError, 'MultiIndex'):
with self.roundtrip(ds):
pass


_counter = itertools.count()

Expand Down