Skip to content

Commit

Permalink
Pandas labels deprecation (#3016)
Browse files Browse the repository at this point in the history
* Pandas deprecation

* allow for older versions of pandas

* update docs
  • Loading branch information
max-sixty authored and shoyer committed Jun 11, 2019
1 parent f3efb5b commit fda6056
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions xarray/core/pdcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])],
labels=[[], []], 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'])
Expand Down

0 comments on commit fda6056

Please sign in to comment.