Skip to content
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

Fix multiindex selection #2621

Merged
merged 8 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Bug fixes
By `Martin Raspaud <https://github.com/mraspaud>`_.
- Fix parsing of ``_Unsigned`` attribute set by OPENDAP servers. (:issue:`2583`).
By `Deepak Cherian <https://github.com/dcherian>`_

- Fix MultiIndex selection to update label and level (:issue:`2619`).
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

.. _whats-new.0.11.0:

Expand Down
8 changes: 7 additions & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from . import (
alignment, computation, duck_array_ops, formatting, groupby, indexing, ops,
resample, rolling, utils)
pdcompat, resample, rolling, utils)
from .. import conventions
from ..coding.cftimeindex import _parse_array_of_cftime_strings
from .alignment import align
Expand Down Expand Up @@ -2425,6 +2425,12 @@ def stack(self, dimensions=None, **dimensions_kwargs):

def _unstack_once(self, dim):
index = self.get_index(dim)
# GH2619. For MultiIndex, we need to call remove_unused.
if LooseVersion(pd.__version__) >= "0.20":
index = index.remove_unused_levels()
else: # for pandas 0.19
index = pdcompat.remove_unused_levels(index)

full_idx = pd.MultiIndex.from_product(index.levels, names=index.names)

# take a shortcut in case the MultiIndex was not modified.
Expand Down
5 changes: 4 additions & 1 deletion xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def convert_label_indexer(index, label, index_name='', method=None,
indexer, new_index = index.get_loc_level(
tuple(label.values()), level=tuple(label.keys()))

# GH2619. Raise a KeyError if nothing is chosen
if indexer.dtype.kind == 'b' and indexer.sum() == 0:
raise KeyError('{} not found'.format(label))

elif isinstance(label, tuple) and isinstance(index, pd.MultiIndex):
if _is_nested_tuple(label):
indexer = index.get_locs(label)
Expand All @@ -168,7 +172,6 @@ def convert_label_indexer(index, label, index_name='', method=None,
indexer, new_index = index.get_loc_level(
label, level=list(range(len(label)))
)

else:
label = (label if getattr(label, 'ndim', 1) > 1 # vectorized-indexing
else _asarray_tuplesafe(label))
Expand Down
79 changes: 79 additions & 0 deletions xarray/core/pdcompat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import numpy as np
import pandas as pd
import pandas.core.algorithms as algos
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do a local import here instead the function?

I'm a little nervous that some future version of pandas may drop this (private) module, which would then break imports even though this isn't actually used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Fixed.



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you copy the pandas license directly into this file, too? See coding/cftimesindex.py for an example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# for pandas 0.19
def remove_unused_levels(self):
"""
create a new MultiIndex from the current that removing
unused levels, meaning that they are not expressed in the labels
The resulting MultiIndex will have the same outward
appearance, meaning the same .values and ordering. It will also
be .equals() to the original.
.. versionadded:: 0.20.0
Returns
-------
MultiIndex
Examples
--------
>>> i = pd.MultiIndex.from_product([range(2), list('ab')])
MultiIndex(levels=[[0, 1], ['a', 'b']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
>>> i[2:]
MultiIndex(levels=[[0, 1], ['a', 'b']],
labels=[[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]])
"""

new_levels = []
new_labels = []

changed = False
for lev, lab in zip(self.levels, self.labels):

# Since few levels are typically unused, bincount() is more
# efficient than unique() - however it only accepts positive values
# (and drops order):
uniques = np.where(np.bincount(lab + 1) > 0)[0] - 1
has_na = int(len(uniques) and (uniques[0] == -1))

if len(uniques) != len(lev) + has_na:
# We have unused levels
changed = True

# Recalculate uniques, now preserving order.
# Can easily be cythonized by exploiting the already existing
# "uniques" and stop parsing "lab" when all items are found:
uniques = algos.unique(lab)
if has_na:
na_idx = np.where(uniques == -1)[0]
# Just ensure that -1 is in first position:
uniques[[0, na_idx[0]]] = uniques[[na_idx[0], 0]]

# labels get mapped from uniques to 0:len(uniques)
# -1 (if present) is mapped to last position
label_mapping = np.zeros(len(lev) + has_na)
# ... and reassigned value -1:
label_mapping[uniques] = np.arange(len(uniques)) - has_na

lab = label_mapping[lab]

# new levels are simple
lev = lev.take(uniques[has_na:])

new_levels.append(lev)
new_labels.append(lab)

result = self._shallow_copy()

if changed:
result._reset_identity()
result._set_levels(new_levels, validate=False)
result._set_labels(new_labels, validate=False)

return result
14 changes: 14 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,20 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False,
assert_identical(mdata.sel(x={'one': 'a', 'two': 1}),
mdata.sel(one='a', two=1))

def test_selection_multiindex(self):
# GH2619. For MultiIndex, we need to call remove_unused.
ds = xr.DataArray(np.arange(40).reshape(8, 5), dims=['x', 'y'],
coords={'x': np.arange(8), 'y': np.arange(5)})
ds = ds.stack(xy=['x', 'y'])
ds_isel = ds.isel(xy=ds['x'] < 4)
with pytest.raises(KeyError):
ds_isel.sel(x=5)

actual = ds_isel.unstack()
expected = ds.reset_index('xy').isel(xy=ds['x'] < 4)
expected = expected.set_index(xy=['x', 'y']).unstack()
assert_identical(expected, actual)

def test_virtual_default_coords(self):
array = DataArray(np.zeros((5,)), dims='x')
expected = DataArray(range(5), dims='x', name='x')
Expand Down