From 45dfadd72b131f5564a30a2b44c0cec4004c1e01 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Thu, 29 Mar 2018 22:38:30 -0400 Subject: [PATCH 01/22] gitignore testmon --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9069cbebacc..05d57c06bb9 100644 --- a/.gitignore +++ b/.gitignore @@ -37,7 +37,7 @@ nosetests.xml .cache .ropeproject/ .tags* -.testmondata +.testmon* .pytest_cache # asv environments From a8fbe2bbffa4c424bef95fcda6ee7e33374287f4 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 01:00:10 -0400 Subject: [PATCH 02/22] initial isin implementation --- xarray/core/dataarray.py | 9 +++++++++ xarray/tests/test_dataarray.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 9ff631e7cfc..3d0452c9a29 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2113,6 +2113,15 @@ def rank(self, dim, pct=False, keep_attrs=False): ds = self._to_temp_dataset().rank(dim, pct=pct, keep_attrs=keep_attrs) return self._from_temp_dataset(ds) + def isin(self, test_elements): + from .computation import apply_ufunc + + return apply_ufunc( + np.isin, + self, + kwargs=dict(test_elements=test_elements), + ) + # priority most be higher than Variable to properly work with binary ufuncs ops.inject_all_ops_and_reduce_methods(DataArray, priority=60) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 3fd229cf394..32ab3a634cb 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -3327,6 +3327,14 @@ def da(request): [0, np.nan, 1, 2, np.nan, 3, 4, 5, np.nan, 6, 7], dims='time') + if request.param == 'repeating_ints': + return DataArray( + np.tile(np.arange(12), 5).reshape(5, 4, 3), + coords={'x': list('abc'), + 'y': list('defg')}, + dims=list('zyx') + ) + @pytest.fixture def da_dask(seed=123): @@ -3339,6 +3347,29 @@ def da_dask(seed=123): return da +@pytest.mark.parametrize('da', ('repeating_ints', ), indirect=True) +def test_isin(da): + + expected = DataArray( + np.asarray([[0, 0, 0], [1, 0, 0]]), + dims=list('yx'), + coords={'x': list('abc'), + 'y': list('de')}, + ).astype('bool') + + result = da.isin([3]).sel(y=list('de'), z=0) + assert_equal(result, expected) + + expected = DataArray( + np.asarray([[0, 0, 1], [1, 0, 0]]), + dims=list('yx'), + coords={'x': list('abc'), + 'y': list('de')}, + ).astype('bool') + result = da.isin([2, 3]).sel(y=list('de'), z=0) + assert_equal(result, expected) + + @pytest.mark.parametrize('da', (1, 2), indirect=True) def test_rolling_iter(da): From 267f02cdebf705efea43605115796b7703e844d0 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 14:57:51 -0400 Subject: [PATCH 03/22] gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 05d57c06bb9..70458f00648 100644 --- a/.gitignore +++ b/.gitignore @@ -51,10 +51,11 @@ nosetests.xml .project .pydevproject -# PyCharm and Vim +# IDEs .idea *.swp .DS_Store +.vscode/ # xarray specific doc/_build From 4411abe7d8cff1e109faccff637faf7e5ce0ccc9 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 14:59:12 -0400 Subject: [PATCH 04/22] dask --- xarray/core/dataarray.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 3d0452c9a29..ac80b98df0b 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2120,6 +2120,8 @@ def isin(self, test_elements): np.isin, self, kwargs=dict(test_elements=test_elements), + dask='parallelized', + output_types=[np.bool_], ) From bcaaaf7833108a3bc89f34c647b8d2584dde95c8 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 15:01:08 -0400 Subject: [PATCH 05/22] numpy version check not needed --- xarray/tests/test_variable.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index c4489f50246..722d1af14f7 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1422,8 +1422,6 @@ def test_reduce(self): with raises_regex(ValueError, 'cannot supply both'): v.mean(dim='x', axis=0) - @pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.10.0'), - reason='requires numpy version 1.10.0 or later') def test_quantile(self): v = Variable(['x', 'y'], self.d) for q in [0.25, [0.50], [0.25, 0.75]]: From ffa5e1d31d2d04b8f723440823aa114ca9a618eb Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 15:06:17 -0400 Subject: [PATCH 06/22] numpy version check for isin --- xarray/core/dataarray.py | 3 +++ xarray/tests/test_dataarray.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index ac80b98df0b..99b88f34197 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2,6 +2,7 @@ import functools import warnings +from distutils.version import LooseVersion import numpy as np import pandas as pd @@ -2114,6 +2115,8 @@ def rank(self, dim, pct=False, keep_attrs=False): return self._from_temp_dataset(ds) def isin(self, test_elements): + if LooseVersion(np.__version__) < LooseVersion('1.13.0'): + raise ImportError('isin requires numpy version 1.13.0 or later') from .computation import apply_ufunc return apply_ufunc( diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 32ab3a634cb..b065efb084c 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -3347,6 +3347,8 @@ def da_dask(seed=123): return da +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0'), + reason='requires numpy version 1.13.0 or later') @pytest.mark.parametrize('da', ('repeating_ints', ), indirect=True) def test_isin(da): From a183c4610c5f43aca7e303cbbc7fadef72bbb299 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 15:39:49 -0400 Subject: [PATCH 07/22] move to common --- xarray/core/common.py | 14 ++++++++++++++ xarray/core/dataarray.py | 13 ------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/xarray/core/common.py b/xarray/core/common.py index 337c1c51415..d5221aa7d71 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, division, print_function import warnings +from distutils.version import LooseVersion import numpy as np import pandas as pd @@ -744,6 +745,19 @@ def close(self): self._file_obj.close() self._file_obj = None + def isin(self, test_elements): + if LooseVersion(np.__version__) < LooseVersion('1.13.0'): + raise ImportError('isin requires numpy version 1.13.0 or later') + from .computation import apply_ufunc + + return apply_ufunc( + np.isin, + self, + kwargs=dict(test_elements=test_elements), + dask='parallelized', + output_dtypes=[np.bool_], + ) + def __enter__(self): return self diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 99b88f34197..056da4a79c7 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2114,19 +2114,6 @@ def rank(self, dim, pct=False, keep_attrs=False): ds = self._to_temp_dataset().rank(dim, pct=pct, keep_attrs=keep_attrs) return self._from_temp_dataset(ds) - def isin(self, test_elements): - if LooseVersion(np.__version__) < LooseVersion('1.13.0'): - raise ImportError('isin requires numpy version 1.13.0 or later') - from .computation import apply_ufunc - - return apply_ufunc( - np.isin, - self, - kwargs=dict(test_elements=test_elements), - dask='parallelized', - output_types=[np.bool_], - ) - # priority most be higher than Variable to properly work with binary ufuncs ops.inject_all_ops_and_reduce_methods(DataArray, priority=60) From 75493c2e9e1eb9b39a2222a19a7725afe1f009be Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 15:40:04 -0400 Subject: [PATCH 08/22] rename data_set to ds --- xarray/tests/test_dataset.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index b4ca14d2384..ce4f580a847 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4033,28 +4033,28 @@ def test_ipython_key_completion(self): @pytest.fixture() -def data_set(seed=None): +def ds(seed=None): return create_test_data(seed) -def test_dir_expected_attrs(data_set): +def test_dir_expected_attrs(ds): some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', 'dim2', 'numbers'} - result = dir(data_set) + result = dir(ds) assert set(result) >= some_expected_attrs -def test_dir_non_string(data_set): +def test_dir_non_string(ds): # add a numbered key to ensure this doesn't break dir - data_set[5] = 'foo' - result = dir(data_set) + ds[5] = 'foo' + result = dir(ds) assert not (5 in result) -def test_dir_unicode(data_set): - data_set[u'unicode'] = 'uni' - result = dir(data_set) +def test_dir_unicode(ds): + ds[u'unicode'] = 'uni' + result = dir(ds) assert u'unicode' in result From 4da73c62bfea0a6eefe2c5eddb3209f48ca79bcd Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 15:41:03 -0400 Subject: [PATCH 09/22] Revert "rename data_set to ds" This reverts commit 75493c2e9e1eb9b39a2222a19a7725afe1f009be. --- xarray/tests/test_dataset.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index ce4f580a847..b4ca14d2384 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4033,28 +4033,28 @@ def test_ipython_key_completion(self): @pytest.fixture() -def ds(seed=None): +def data_set(seed=None): return create_test_data(seed) -def test_dir_expected_attrs(ds): +def test_dir_expected_attrs(data_set): some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', 'dim2', 'numbers'} - result = dir(ds) + result = dir(data_set) assert set(result) >= some_expected_attrs -def test_dir_non_string(ds): +def test_dir_non_string(data_set): # add a numbered key to ensure this doesn't break dir - ds[5] = 'foo' - result = dir(ds) + data_set[5] = 'foo' + result = dir(data_set) assert not (5 in result) -def test_dir_unicode(ds): - ds[u'unicode'] = 'uni' - result = dir(ds) +def test_dir_unicode(data_set): + data_set[u'unicode'] = 'uni' + result = dir(data_set) assert u'unicode' in result From 6fb9c48fea4b061f48c522e16e0ac56e7289149d Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 17:33:06 -0400 Subject: [PATCH 10/22] 'expect' test for dataset --- xarray/tests/test_dataset.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index b4ca14d2384..5482325ef3a 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4032,9 +4032,32 @@ def test_ipython_key_completion(self): # Py.test tests -@pytest.fixture() -def data_set(seed=None): - return create_test_data(seed) +@pytest.fixture(params=[None]) +def data_set(request): + return create_test_data(request.param) + + +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0'), + reason='requires numpy version 1.13.0 or later') +@pytest.mark.parametrize('data_set', [1], indirect=True) +def test_isin(data_set): + expected = Dataset( + data_vars={ + 'var1': (('dim1',), [0, 1]), + 'var2': (('dim1',), [1, 1]), + 'var3': (('dim1',), [0, 1]), + } + ).astype('bool') + + result = (data_set + .round(0) + .isin([0]) + .sel(dim2=0, dim3='a') + .isel(dim1=[0, 1]) + .drop(['time', 'dim3', 'dim2', 'numbers']) + .squeeze() + ) + assert_equal(result, expected) def test_dir_expected_attrs(data_set): From 616db23cb721b405f03f90894da98a3235d2ba60 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 17:36:50 -0400 Subject: [PATCH 11/22] unneeded import --- xarray/core/dataarray.py | 1 - 1 file changed, 1 deletion(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 056da4a79c7..9ff631e7cfc 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2,7 +2,6 @@ import functools import warnings -from distutils.version import LooseVersion import numpy as np import pandas as pd From 60fbd5b88eabb78242c37537654e24e71b98dabf Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 17:52:21 -0400 Subject: [PATCH 12/22] formatting --- xarray/tests/test_dataset.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 5482325ef3a..8a7edc36336 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4049,14 +4049,15 @@ def test_isin(data_set): } ).astype('bool') - result = (data_set - .round(0) - .isin([0]) - .sel(dim2=0, dim3='a') - .isel(dim1=[0, 1]) - .drop(['time', 'dim3', 'dim2', 'numbers']) - .squeeze() - ) + result = ( + data_set + .round(0) + .isin([0]) + .sel(dim2=0, dim3='a') + .isel(dim1=[0, 1]) + .drop(['time', 'dim3', 'dim2', 'numbers']) + .squeeze() + ) assert_equal(result, expected) From 34dabe1627efc8350058659fde2358d9fab59338 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 19:33:34 -0400 Subject: [PATCH 13/22] docs --- doc/api.rst | 2 ++ doc/whats-new.rst | 7 ++++++- xarray/core/common.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/api.rst b/doc/api.rst index 8ee5d548892..9772ed6ba62 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -174,6 +174,7 @@ Computation :py:attr:`~Dataset.cumsum` :py:attr:`~Dataset.cumprod` :py:attr:`~Dataset.rank` +:py:attr:`~Dataset.isin` **Grouped operations**: :py:attr:`~core.groupby.DatasetGroupBy.assign` @@ -339,6 +340,7 @@ Computation :py:attr:`~DataArray.cumsum` :py:attr:`~DataArray.cumprod` :py:attr:`~DataArray.rank` +:py:attr:`~DataArray.isin` **Grouped operations**: :py:attr:`~core.groupby.DataArrayGroupBy.assign_coords` diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b28beb9e3b2..cf99bf9de72 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,7 +37,12 @@ Documentation Enhancements ~~~~~~~~~~~~ - - Some speed improvement to construct :py:class:`~xarray.DataArrayRolling` +- `~xarray.DataArray.isin` and `~xarray.Dataset.isin` methods, which tests each value + in the array for whether it is contained in the supplied list, returning a bool array. + Similar to the ``np.isin`` function. Requires NumPy >= 1.13 +By `Maximilian Roos ` + +- Some speed improvement to construct :py:class:`~xarray.DataArrayRolling` object (:issue:`1993`) By `Keisuke Fujii `_. diff --git a/xarray/core/common.py b/xarray/core/common.py index d5221aa7d71..7d68da5d270 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -746,6 +746,22 @@ def close(self): self._file_obj = None def isin(self, test_elements): + """Tests each value in the array for whether it is in the supplied list + Requires NumPy >= 1.13 + + Parameters + ---------- + element : array_like + Input array. + test_elements : array_like + The values against which to test each value of `element`. + This argument is flattened if it is an array or array_like. + See notes for behavior with non-array-like parameters. + + ------- + isin : same as object, bool + Has the same shape as object + """ if LooseVersion(np.__version__) < LooseVersion('1.13.0'): raise ImportError('isin requires numpy version 1.13.0 or later') from .computation import apply_ufunc From 0cc17529501759ddec0b526cbd5b72c85b35216c Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Fri, 30 Mar 2018 18:11:19 -0700 Subject: [PATCH 14/22] Raise an informative error message when converting Dataset -> np.ndarray Makes `np.asarray(dataset)` issue an informative error. Currently, `np.asarray(xr.Dataset({'x': 0}))` raises `KeyError: 0`, which makes no sense. --- doc/whats-new.rst | 3 +++ xarray/core/dataset.py | 6 ++++++ xarray/tests/test_dataset.py | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b28beb9e3b2..33d1e710f1d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -47,6 +47,9 @@ Bug fixes - Fixed labeled indexing with slice bounds given by xarray objects with datetime64 or timedelta64 dtypes (:issue:`1240`). By `Stephan Hoyer `_. +- Attempting to convert an xarray.Dataset into a numpy array now raises an + informative error message. + By `Stephan Hoyer `_. .. _whats-new.0.10.2: diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index e960d433f98..f28e7980b34 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -849,6 +849,12 @@ def __iter__(self): FutureWarning, stacklevel=2) return iter(self._variables) + def __array__(self, dtype=None): + raise TypeError('cannot directly convert an xarray.Dataset into a ' + 'numpy array. Instead, create an xarray.DataArray ' + 'first, either with indexing on the Dataset or by ' + 'invoking the `to_array()` method.') + @property def nbytes(self): return sum(v.nbytes for v in self.variables.values()) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index b4ca14d2384..826f50003fa 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -443,6 +443,11 @@ def test_properties(self): assert Dataset({'x': np.int64(1), 'y': np.float32([1, 2])}).nbytes == 16 + def test_asarray(self): + ds = Dataset({'x': 0}) + with raises_regex(TypeError, 'cannot directly convert'): + np.asarray(ds) + def test_get_index(self): ds = Dataset({'foo': (('x', 'y'), np.zeros((2, 3)))}, coords={'x': ['a', 'b']}) From 41d73b03be7c96c09b3135841aace8b0cbf22486 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 22:32:42 -0400 Subject: [PATCH 15/22] normal tests are better than a weird middle ground --- xarray/tests/test_dataset.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 8a7edc36336..3138a08b163 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4039,8 +4039,12 @@ def data_set(request): @pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0'), reason='requires numpy version 1.13.0 or later') -@pytest.mark.parametrize('data_set', [1], indirect=True) -def test_isin(data_set): +@pytest.mark.parametrize('test_elements', ( + [1, 2], + np.array([1, 2]), + DataArray([1, 2]) +)) +def test_isin(test_elements): expected = Dataset( data_vars={ 'var1': (('dim1',), [0, 1]), @@ -4049,15 +4053,14 @@ def test_isin(data_set): } ).astype('bool') - result = ( - data_set - .round(0) - .isin([0]) - .sel(dim2=0, dim3='a') - .isel(dim1=[0, 1]) - .drop(['time', 'dim3', 'dim2', 'numbers']) - .squeeze() - ) + result = Dataset( + data_vars={ + 'var1': (('dim1',), [0, 1]), + 'var2': (('dim1',), [1, 2]), + 'var3': (('dim1',), [0, 1]), + } + ).isin(test_elements) + assert_equal(result, expected) From 581b3c2da4afe0dad357c0ebb56b649daeffddde Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Fri, 30 Mar 2018 23:53:34 -0400 Subject: [PATCH 16/22] dask test --- xarray/tests/test_dataset.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 24be9fce3f9..695a719a4bd 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4047,7 +4047,8 @@ def data_set(request): @pytest.mark.parametrize('test_elements', ( [1, 2], np.array([1, 2]), - DataArray([1, 2]) + DataArray([1, 2]), + pytest.mark.xfail(Dataset({'x': [1, 2]})), )) def test_isin(test_elements): expected = Dataset( @@ -4069,6 +4070,35 @@ def test_isin(test_elements): assert_equal(result, expected) +@requires_dask +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0'), + reason='requires numpy version 1.13.0 or later') +@pytest.mark.parametrize('test_elements', ( + [1, 2], + np.array([1, 2]), + DataArray([1, 2]), + pytest.mark.xfail(Dataset({'x': [1, 2]})), +)) +def test_isin_dask(test_elements): + expected = Dataset( + data_vars={ + 'var1': (('dim1',), [0, 1]), + 'var2': (('dim1',), [1, 1]), + 'var3': (('dim1',), [0, 1]), + } + ).astype('bool') + + result = Dataset( + data_vars={ + 'var1': (('dim1',), [0, 1]), + 'var2': (('dim1',), [1, 2]), + 'var3': (('dim1',), [0, 1]), + } + ).chunk(1).isin(test_elements).compute() + + assert_equal(result, expected) + + def test_dir_expected_attrs(data_set): some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', From 0a244299c9663e6a5f9b69c96c9d87d6716dc27d Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 31 Mar 2018 00:05:04 -0400 Subject: [PATCH 17/22] grammar --- doc/whats-new.rst | 2 +- xarray/core/common.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 36d9e484ea7..3f01f48bb0f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,7 +37,7 @@ Documentation Enhancements ~~~~~~~~~~~~ -- `~xarray.DataArray.isin` and `~xarray.Dataset.isin` methods, which tests each value +- `~xarray.DataArray.isin` and `~xarray.Dataset.isin` methods, which test each value in the array for whether it is contained in the supplied list, returning a bool array. Similar to the ``np.isin`` function. Requires NumPy >= 1.13 By `Maximilian Roos ` diff --git a/xarray/core/common.py b/xarray/core/common.py index 7d68da5d270..5904dcb1274 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -755,8 +755,8 @@ def isin(self, test_elements): Input array. test_elements : array_like The values against which to test each value of `element`. - This argument is flattened if it is an array or array_like. - See notes for behavior with non-array-like parameters. + This argument is flattened if an array or array_like. + See numpy notes for behavior with non-array-like parameters. ------- isin : same as object, bool From f1768cd833b23948ca49089609cb7875d8dcbfa4 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 2 Apr 2018 20:20:21 -0400 Subject: [PATCH 18/22] try changing skip decorator ordering --- xarray/tests/test_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 695a719a4bd..597e7cdaca3 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4070,9 +4070,9 @@ def test_isin(test_elements): assert_equal(result, expected) -@requires_dask @pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0'), reason='requires numpy version 1.13.0 or later') +@requires_dask @pytest.mark.parametrize('test_elements', ( [1, 2], np.array([1, 2]), From 80557c541e25681f5e57b86446fcd34a5cba2357 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 2 Apr 2018 20:43:50 -0400 Subject: [PATCH 19/22] just use has_dask --- xarray/tests/test_dataset.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 597e7cdaca3..9bb61ca5e30 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -21,7 +21,7 @@ from . import ( InaccessibleArray, TestCase, UnexpectedDataAccess, assert_allclose, - assert_array_equal, assert_equal, assert_identical, raises_regex, + assert_array_equal, assert_equal, assert_identical, has_dask, raises_regex, requires_bottleneck, requires_dask, requires_scipy, source_ndarray) try: @@ -4070,9 +4070,9 @@ def test_isin(test_elements): assert_equal(result, expected) -@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0'), - reason='requires numpy version 1.13.0 or later') -@requires_dask +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') + or not has_dask, # noqa + reason='requires dask and numpy version 1.13.0 or later') @pytest.mark.parametrize('test_elements', ( [1, 2], np.array([1, 2]), From d9c99076a1c04862312b06791ba8a08a42cb61d7 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 2 Apr 2018 20:57:21 -0400 Subject: [PATCH 20/22] another noqa? --- xarray/tests/test_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 9bb61ca5e30..d12277bebe9 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4070,7 +4070,7 @@ def test_isin(test_elements): assert_equal(result, expected) -@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') # noqa or not has_dask, # noqa reason='requires dask and numpy version 1.13.0 or later') @pytest.mark.parametrize('test_elements', ( From dd0136df534b0d55f7e14c139d23905dc3971937 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Tue, 3 Apr 2018 01:55:22 -0400 Subject: [PATCH 21/22] flake for py3.4 --- xarray/tests/test_dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index d12277bebe9..2350ddd4f24 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4070,8 +4070,8 @@ def test_isin(test_elements): assert_equal(result, expected) -@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') # noqa - or not has_dask, # noqa +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') or # noqa + not has_dask, # noqa reason='requires dask and numpy version 1.13.0 or later') @pytest.mark.parametrize('test_elements', ( [1, 2], From 43532dfef1fa338eb7170b38ddddf32cc4ccca34 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Tue, 3 Apr 2018 02:01:39 -0400 Subject: [PATCH 22/22] flake --- xarray/tests/test_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 2350ddd4f24..a7b55735579 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -4070,7 +4070,7 @@ def test_isin(test_elements): assert_equal(result, expected) -@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') or # noqa +@pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.13.0') or # noqa not has_dask, # noqa reason='requires dask and numpy version 1.13.0 or later') @pytest.mark.parametrize('test_elements', (