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 min max issue #2923 #2924

Merged
merged 9 commits into from
Jun 27, 2019
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Bug fixes
By `Mayeul d'Avezac <https://github.com/mdavezac>`_.
- Return correct count for scalar datetime64 arrays (:issue:`2770`)
By `Dan Nowacki <https://github.com/dnowacki-usgs>`_.
- Fixed max, min exception when applied to a multiIndex (:issue:`2923`)
By `Ian Castleden <https://github.com/arabidopsis>`_
- A deep copy deep-copies the coords (:issue:`1463`)
By `Martin Pletcher <https://github.com/pletchm>`_.
- Increased support for `missing_value` (:issue:`2871`)
Expand Down
8 changes: 5 additions & 3 deletions xarray/core/nanops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from . import dtypes, nputils
from . import dtypes, nputils, utils
from .duck_array_ops import (
_dask_or_eager_func, count, fillna, isnull, where_method)
from .pycompat import dask_array_type
Expand Down Expand Up @@ -64,8 +64,10 @@ def _nan_minmax_object(func, fill_value, value, axis=None, **kwargs):
filled_value = fillna(value, fill_value)
data = getattr(np, func)(filled_value, axis=axis, **kwargs)
if not hasattr(data, 'dtype'): # scalar case
data = dtypes.fill_value(value.dtype) if valid_count == 0 else data
return np.array(data, dtype=value.dtype)
data = fill_value if valid_count == 0 else data
# we've computed a single min, max value of type object.
# don't let np.array turn a tuple back into an array
return utils.to_0d_object_array(data)
return where_method(data, valid_count != 0)


Expand Down
16 changes: 16 additions & 0 deletions xarray/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ def test_asarray_tuplesafe(self):
assert res[0] == (0,)
assert res[1] == (1,)

def test_stacked_multiindex_min_max(self):
data = np.random.randn(3, 23, 4)
da = DataArray(
data, name="value",
dims=["replicate", "rsample", "exp"],
coords=dict(
replicate=[0, 1, 2],
exp=["a", "b", "c", "d"],
rsample=list(range(23))
),
)
da2 = da.stack(sample=("replicate", "rsample"))
s = da2.sample
assert_array_equal(da2.loc['a', s.max()], data[2, 22, 0])
assert_array_equal(da2.loc['b', s.min()], data[0, 0, 1])

def test_convert_label_indexer(self):
# TODO: add tests that aren't just for edge cases
index = pd.Index([1, 2, 3])
Expand Down