From 5b692e41e4389ac729c41d74a4c833b5b0f1bfbe Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Sat, 27 Apr 2019 23:46:32 +0800 Subject: [PATCH 1/7] fix multiIndex min max issue --- xarray/core/nanops.py | 6 +++++- xarray/tests/test_indexing.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/xarray/core/nanops.py b/xarray/core/nanops.py index babc1dd97e6..26abd11f824 100644 --- a/xarray/core/nanops.py +++ b/xarray/core/nanops.py @@ -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 @@ -65,6 +65,10 @@ def _nan_minmax_object(func, fill_value, value, axis=None, **kwargs): 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 + # we've computed a single min, max value. don't let np.array turn + # a tuple back into an array + if isinstance(data, tuple): + return utils.to_0d_object_array(data) return np.array(data, dtype=value.dtype) return where_method(data, valid_count != 0) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index 14b79c71ca4..d6123ffb65c 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -43,6 +43,19 @@ 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]) From ff9c08a574df082df045dfcbd0e7c8b181a50920 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Sun, 28 Apr 2019 00:02:00 +0800 Subject: [PATCH 2/7] flake8 changes --- xarray/tests/test_indexing.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index d6123ffb65c..2b1478e3977 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -46,14 +46,17 @@ def test_asarray_tuplesafe(self): def test_stacked_multiindex_min_max(self): data = np.random.randn(3, 23, 4) da = DataArray( - data, name = "value", dims = ["replicate", "rsample", "exp"], + data, name="value", + dims=["replicate", "rsample", "exp"], coords=dict( - replicate=[0, 1, 2], exp=["a", "b", "c", "d"], rsample=list(range(23)) + replicate=[0, 1, 2], + sexp=["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['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): From 008d7b060013d5014c5d93c812ed25c12ee95ae1 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Sun, 28 Apr 2019 01:33:29 +0800 Subject: [PATCH 3/7] ensure test actually works! --- xarray/tests/test_indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index 2b1478e3977..cdc005d2bf5 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -50,7 +50,7 @@ def test_stacked_multiindex_min_max(self): dims=["replicate", "rsample", "exp"], coords=dict( replicate=[0, 1, 2], - sexp=["a", "b", "c", "d"], + exp=["a", "b", "c", "d"], rsample=list(range(23)) ), ) From 28a1c726426ae81bceebf3869044b32bc5ec94c8 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Sun, 28 Apr 2019 08:44:17 +0800 Subject: [PATCH 4/7] bug fix issue #2923 --- doc/whats-new.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 27709a09e7a..1c119073d10 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -24,7 +24,7 @@ Enhancements - Character arrays' character dimension name decoding and encoding handled by ``var.encoding['char_dim_name']`` (:issue:`2895`) By `James McCreight `_. - + Bug fixes ~~~~~~~~~ @@ -32,6 +32,8 @@ Bug fixes By `Mayeul d'Avezac `_. - Return correct count for scalar datetime64 arrays (:issue:`2770`) By `Dan Nowacki `_. +- Fixed max, min exception when applied to a multiIndex (:issue:`2923`) + By `Ian Castleden `_ .. _whats-new.0.12.1: From 554fef35627ba7320fbc30af523c1b41d384b0b9 Mon Sep 17 00:00:00 2001 From: Ian Castleden Date: Sun, 28 Apr 2019 19:47:03 +0800 Subject: [PATCH 5/7] dtypes.fill_value doesn't exist. --- xarray/core/nanops.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/xarray/core/nanops.py b/xarray/core/nanops.py index 26abd11f824..65d310f2943 100644 --- a/xarray/core/nanops.py +++ b/xarray/core/nanops.py @@ -64,12 +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 - # we've computed a single min, max value. don't let np.array turn - # a tuple back into an array - if isinstance(data, tuple): - return utils.to_0d_object_array(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) From 9168e98d03efb4399d87730c2555339cd750d5f6 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Wed, 12 Jun 2019 09:36:05 -0600 Subject: [PATCH 6/7] lint fix --- xarray/core/nanops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/nanops.py b/xarray/core/nanops.py index 65d310f2943..06ab08e12fb 100644 --- a/xarray/core/nanops.py +++ b/xarray/core/nanops.py @@ -65,7 +65,7 @@ def _nan_minmax_object(func, fill_value, value, axis=None, **kwargs): data = getattr(np, func)(filled_value, axis=axis, **kwargs) if not hasattr(data, 'dtype'): # scalar case data = fill_value if valid_count == 0 else data - # we've computed a single min, max value of type object. + # 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) From a1009f69a29c05f96d92ca380c585a3b0130b263 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Thu, 27 Jun 2019 16:06:16 +0000 Subject: [PATCH 7/7] Update whats-new.rst --- doc/whats-new.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 7a152a3f500..d7a9554fd40 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -79,8 +79,6 @@ Bug fixes By `Dan Nowacki `_. - Fixed max, min exception when applied to a multiIndex (:issue:`2923`) By `Ian Castleden `_ -- Fix facetgrid colormap bug when ``extend=True``. (:issue:`2932`) - By `Deepak Cherian `_. - Increased support for `missing_value` (:issue:`2871`)