Skip to content

Fix variable copy with multi-index #881

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 2 commits into from
Jun 16, 2016
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ Bug fixes
This fixes issue :issue:`665`.
`Filipe Fernandes <https://github.com/ocefpaf>`_.

- ``Variable.copy(deep=True)`` no longer converts MultiIndex into a base Index
(:issue:`769`). By `Benoit Bovy <https://github.com/benbovy>`_.

.. _whats-new.0.7.2:

v0.7.2 (13 March 2016)
Expand Down
4 changes: 3 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ def copy(self, deep=True):
If `deep=True`, the data array is loaded into memory and copied onto
the new object. Dimensions, attributes and encodings are always copied.
"""
if deep and not isinstance(self.data, dask_array_type):
if (deep and not isinstance(self.data, dask_array_type)
and not isinstance(self._data, PandasIndexAdapter)):
# pandas.Index objects are immutable
# dask arrays don't have a copy method
# https://github.com/blaze/dask/issues/911
data = self.data.copy()
Expand Down
9 changes: 9 additions & 0 deletions xarray/test/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ def test_copy(self):
source_ndarray(w.values))
self.assertVariableIdentical(v, copy(v))

def test_copy_index(self):
midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]],
names=('one', 'two', 'three'))
v = self.cls('x', midx)
for deep in [True, False]:
w = v.copy(deep=deep)
self.assertIsInstance(w._data, PandasIndexAdapter)
self.assertIs(v._data.array, w._data.array)

def test_real_and_imag(self):
v = self.cls('x', np.arange(3) - 1j * np.arange(3), {'foo': 'bar'})
expected_re = self.cls('x', np.arange(3), {'foo': 'bar'})
Expand Down