Skip to content

Commit 065ea6a

Browse files
Benoit Bovyshoyer
Benoit Bovy
authored andcommitted
Fix variable copy with multi-index (#881)
Fixes GH769
1 parent 918945c commit 065ea6a

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Bug fixes
7171
This fixes issue :issue:`665`.
7272
`Filipe Fernandes <https://github.com/ocefpaf>`_.
7373

74+
- ``Variable.copy(deep=True)`` no longer converts MultiIndex into a base Index
75+
(:issue:`769`). By `Benoit Bovy <https://github.com/benbovy>`_.
76+
7477
.. _whats-new.0.7.2:
7578

7679
v0.7.2 (13 March 2016)

xarray/core/variable.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ def copy(self, deep=True):
411411
If `deep=True`, the data array is loaded into memory and copied onto
412412
the new object. Dimensions, attributes and encodings are always copied.
413413
"""
414-
if deep and not isinstance(self.data, dask_array_type):
414+
if (deep and not isinstance(self.data, dask_array_type)
415+
and not isinstance(self._data, PandasIndexAdapter)):
416+
# pandas.Index objects are immutable
415417
# dask arrays don't have a copy method
416418
# https://github.com/blaze/dask/issues/911
417419
data = self.data.copy()

xarray/test/test_variable.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@ def test_copy(self):
385385
source_ndarray(w.values))
386386
self.assertVariableIdentical(v, copy(v))
387387

388+
def test_copy_index(self):
389+
midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]],
390+
names=('one', 'two', 'three'))
391+
v = self.cls('x', midx)
392+
for deep in [True, False]:
393+
w = v.copy(deep=deep)
394+
self.assertIsInstance(w._data, PandasIndexAdapter)
395+
self.assertIs(v._data.array, w._data.array)
396+
388397
def test_real_and_imag(self):
389398
v = self.cls('x', np.arange(3) - 1j * np.arange(3), {'foo': 'bar'})
390399
expected_re = self.cls('x', np.arange(3), {'foo': 'bar'})

0 commit comments

Comments
 (0)