From 110c85778a7dd000634618660699ade3148f6d97 Mon Sep 17 00:00:00 2001 From: keewis Date: Sat, 6 Feb 2021 23:02:30 +0100 Subject: [PATCH 1/2] temporarily pin dask (#4873) --- ci/requirements/environment-windows.yml | 2 +- ci/requirements/environment.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/requirements/environment-windows.yml b/ci/requirements/environment-windows.yml index 6de2bc8dc64..9455ef2f127 100644 --- a/ci/requirements/environment-windows.yml +++ b/ci/requirements/environment-windows.yml @@ -8,7 +8,7 @@ dependencies: # - cdms2 # Not available on Windows # - cfgrib # Causes Python interpreter crash on Windows: https://github.com/pydata/xarray/pull/3340 - cftime - - dask + - dask<2021.02.0 - distributed - h5netcdf - h5py=2 diff --git a/ci/requirements/environment.yml b/ci/requirements/environment.yml index 0f59d9570c8..7261b5b6954 100644 --- a/ci/requirements/environment.yml +++ b/ci/requirements/environment.yml @@ -9,7 +9,7 @@ dependencies: - cdms2 - cfgrib - cftime - - dask + - dask<2021.02.0 - distributed - h5netcdf - h5py=2 From ec7f628bf38b37df213fe3b5ad68d3f70824b864 Mon Sep 17 00:00:00 2001 From: Mathias Hauser Date: Sun, 7 Feb 2021 22:57:33 +0100 Subject: [PATCH 2/2] fix da.pad example for numpy 1.20 (#4865) --- xarray/core/dataarray.py | 23 +++++++++++------------ xarray/tests/test_dataarray.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 0155cdc4e19..540230766a5 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -3851,9 +3851,8 @@ def pad( Notes ----- - By default when ``mode="constant"`` and ``constant_values=None``, integer types will be - promoted to ``float`` and padded with ``np.nan``. To avoid type promotion - specify ``constant_values=np.nan`` + For ``mode="constant"`` and ``constant_values=None``, integer types will be + promoted to ``float`` and padded with ``np.nan``. Examples -------- @@ -3880,16 +3879,16 @@ def pad( * x (x) float64 nan 0.0 1.0 nan * y (y) int64 10 20 30 40 z (x) float64 nan 100.0 200.0 nan - >>> da.pad(x=1, constant_values=np.nan) + + Careful, ``constant_values`` are coerced to the data type of the array which may + lead to a loss of precision: + + >>> da.pad(x=1, constant_values=1.23456789) - array([[-9223372036854775808, -9223372036854775808, -9223372036854775808, - -9223372036854775808], - [ 0, 1, 2, - 3], - [ 10, 11, 12, - 13], - [-9223372036854775808, -9223372036854775808, -9223372036854775808, - -9223372036854775808]]) + array([[ 1, 1, 1, 1], + [ 0, 1, 2, 3], + [10, 11, 12, 13], + [ 1, 1, 1, 1]]) Coordinates: * x (x) float64 nan 0.0 1.0 nan * y (y) int64 10 20 30 40 diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index fc84687511e..8d599c7a715 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -4472,6 +4472,26 @@ def test_pad_constant(self): assert actual.shape == (7, 4, 5) assert_identical(actual, expected) + ar = xr.DataArray([9], dims="x") + + actual = ar.pad(x=1) + expected = xr.DataArray([np.NaN, 9, np.NaN], dims="x") + assert_identical(actual, expected) + + actual = ar.pad(x=1, constant_values=1.23456) + expected = xr.DataArray([1, 9, 1], dims="x") + assert_identical(actual, expected) + + if LooseVersion(np.__version__) >= "1.20": + with pytest.raises(ValueError, match="cannot convert float NaN to integer"): + ar.pad(x=1, constant_values=np.NaN) + else: + actual = ar.pad(x=1, constant_values=np.NaN) + expected = xr.DataArray( + [-9223372036854775808, 9, -9223372036854775808], dims="x" + ) + assert_identical(actual, expected) + def test_pad_coords(self): ar = DataArray( np.arange(3 * 4 * 5).reshape(3, 4, 5),