From 68cb6cc42e52fe5422c1c72b73ebe675dccb2dd0 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Tue, 11 Jun 2019 12:23:19 -0400 Subject: [PATCH 1/3] Pandas deprecation --- xarray/tests/test_dataarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 3580155781a..c1cf74a7f86 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1763,7 +1763,7 @@ def test_stack_unstack(self): # test GH3000 a = orig[:0, :1].stack(dim=('x', 'y')).dim.to_index() b = pd.MultiIndex(levels=[pd.Int64Index([]), pd.Int64Index([0])], - labels=[[], []], names=['x', 'y']) + codes=[[], []], names=['x', 'y']) pd.util.testing.assert_index_equal(a, b) actual = orig.stack(z=['x', 'y']).unstack('z').drop(['x', 'y']) From a316f4b50f39ab99a72f6eeb427de4be8f4748c4 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Tue, 11 Jun 2019 13:17:12 -0400 Subject: [PATCH 2/3] allow for older versions of pandas --- xarray/tests/test_dataarray.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index c1cf74a7f86..1e831e362e5 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1762,8 +1762,12 @@ def test_stack_unstack(self): # test GH3000 a = orig[:0, :1].stack(dim=('x', 'y')).dim.to_index() - b = pd.MultiIndex(levels=[pd.Int64Index([]), pd.Int64Index([0])], - codes=[[], []], names=['x', 'y']) + if pd.__version__ < '0.24.0': + b = pd.MultiIndex(levels=[pd.Int64Index([]), pd.Int64Index([0])], + labels=[[], []], names=['x', 'y']) + else: + b = pd.MultiIndex(levels=[pd.Int64Index([]), pd.Int64Index([0])], + codes=[[], []], names=['x', 'y']) pd.util.testing.assert_index_equal(a, b) actual = orig.stack(z=['x', 'y']).unstack('z').drop(['x', 'y']) From 8b31e868893b6f7a39536194dd3fbc6e2fd96a93 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Tue, 11 Jun 2019 13:18:37 -0400 Subject: [PATCH 3/3] update docs --- xarray/core/dataarray.py | 4 ++-- xarray/core/pdcompat.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 6491bcc1b4a..094b8615880 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -1351,7 +1351,7 @@ def stack(self, dimensions=None, **dimensions_kwargs): >>> stacked = arr.stack(z=('x', 'y')) >>> stacked.indexes['z'] MultiIndex(levels=[['a', 'b'], [0, 1, 2]], - labels=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], + codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], names=['x', 'y']) See also @@ -1394,7 +1394,7 @@ def unstack(self, dim=None): >>> stacked = arr.stack(z=('x', 'y')) >>> stacked.indexes['z'] MultiIndex(levels=[['a', 'b'], [0, 1, 2]], - labels=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], + codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]], names=['x', 'y']) >>> roundtripped = stacked.unstack() >>> arr.identical(roundtripped) diff --git a/xarray/core/pdcompat.py b/xarray/core/pdcompat.py index f76634c65aa..7e2b0bbf6c4 100644 --- a/xarray/core/pdcompat.py +++ b/xarray/core/pdcompat.py @@ -57,15 +57,15 @@ def remove_unused_levels(self): -------- >>> i = pd.MultiIndex.from_product([range(2), list('ab')]) MultiIndex(levels=[[0, 1], ['a', 'b']], - labels=[[0, 0, 1, 1], [0, 1, 0, 1]]) + codes=[[0, 0, 1, 1], [0, 1, 0, 1]]) >>> i[2:] MultiIndex(levels=[[0, 1], ['a', 'b']], - labels=[[1, 1], [0, 1]]) + codes=[[1, 1], [0, 1]]) The 0 from the first level is not represented and can be removed >>> i[2:].remove_unused_levels() MultiIndex(levels=[[1], ['a', 'b']], - labels=[[0, 0], [0, 1]]) + codes=[[0, 0], [0, 1]]) """ import pandas.core.algorithms as algos