Skip to content

Commit c729121

Browse files
arabidopsisdcherian
authored andcommitted
fix multiIndex min max issue pydata#2923 (pydata#2924)
* fix multiIndex min max issue * flake8 changes * ensure test actually works! * bug fix issue pydata#2923 * dtypes.fill_value doesn't exist. * lint fix * Update whats-new.rst
1 parent 9728e89 commit c729121

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ Bug fixes
107107
By `Mayeul d'Avezac <https://github.com/mdavezac>`_.
108108
- Return correct count for scalar datetime64 arrays (:issue:`2770`)
109109
By `Dan Nowacki <https://github.com/dnowacki-usgs>`_.
110+
- Fixed max, min exception when applied to a multiIndex (:issue:`2923`)
111+
By `Ian Castleden <https://github.com/arabidopsis>`_
110112
- A deep copy deep-copies the coords (:issue:`1463`)
111113
By `Martin Pletcher <https://github.com/pletchm>`_.
112114
- Increased support for `missing_value` (:issue:`2871`)

xarray/core/nanops.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22

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

7173

xarray/tests/test_indexing.py

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def test_asarray_tuplesafe(self):
4343
assert res[0] == (0,)
4444
assert res[1] == (1,)
4545

46+
def test_stacked_multiindex_min_max(self):
47+
data = np.random.randn(3, 23, 4)
48+
da = DataArray(
49+
data, name="value",
50+
dims=["replicate", "rsample", "exp"],
51+
coords=dict(
52+
replicate=[0, 1, 2],
53+
exp=["a", "b", "c", "d"],
54+
rsample=list(range(23))
55+
),
56+
)
57+
da2 = da.stack(sample=("replicate", "rsample"))
58+
s = da2.sample
59+
assert_array_equal(da2.loc['a', s.max()], data[2, 22, 0])
60+
assert_array_equal(da2.loc['b', s.min()], data[0, 0, 1])
61+
4662
def test_convert_label_indexer(self):
4763
# TODO: add tests that aren't just for edge cases
4864
index = pd.Index([1, 2, 3])

0 commit comments

Comments
 (0)