From 101570787a5920efb6cb426701292b858c91b50b Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sat, 16 Nov 2019 13:09:38 -0500 Subject: [PATCH 1/6] Select first element of each index --- doc/whats-new.rst | 5 ++++- xarray/core/combine.py | 2 +- xarray/tests/test_combine.py | 23 ++++++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6bf495713fe..0fcb1db2784 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -113,7 +113,10 @@ Bug fixes (:issue:`3402`). By `Deepak Cherian `_ - Allow appending datetime and bool data variables to zarr stores. (:issue:`3480`). By `Akihiro Matsukawa `_. - +- Fix :py:meth:`xarray.combine_by_coords` when combining cftime coordinates + which span long time intervals (:issue:`3535`). By `Spencer Clark + `_. + Documentation ~~~~~~~~~~~~~ - Fix leap year condition in example (http://xarray.pydata.org/en/stable/examples/monthly-means.html) diff --git a/xarray/core/combine.py b/xarray/core/combine.py index 3308dcef285..c4557216053 100644 --- a/xarray/core/combine.py +++ b/xarray/core/combine.py @@ -88,7 +88,7 @@ def _infer_concat_order_from_coords(datasets): # with the same value have the same coord values throughout. if any(index.size == 0 for index in indexes): raise ValueError("Cannot handle size zero dimensions") - first_items = pd.Index([index.take([0]) for index in indexes]) + first_items = pd.Index([index[0] for index in indexes]) # Sort datasets along dim # We want rank but with identical elements given identical diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index cd26e7fb60b..caac4d2926c 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -22,7 +22,7 @@ _new_tile_id, ) -from . import assert_equal, assert_identical, raises_regex +from . import assert_equal, assert_identical, raises_regex, requires_cftime from .test_dataset import create_test_data @@ -877,3 +877,24 @@ def test_auto_combine_without_coords(self): objs = [Dataset({"foo": ("x", [0])}), Dataset({"foo": ("x", [1])})] with pytest.warns(FutureWarning, match="supplied do not have global"): auto_combine(objs) + + +@requires_cftime +def test_combine_by_coords_distant_cftime_dates(): + import cftime + + time_1 = [cftime.DatetimeGregorian(4500, 12, 31)] + time_2 = [cftime.DatetimeGregorian(4600, 12, 31)] + time_3 = [cftime.DatetimeGregorian(5100, 12, 31)] + + da_1 = DataArray([0], dims=['time'], coords=[time_1], name="a").to_dataset() + da_2 = DataArray([1], dims=['time'], coords=[time_2], name="a").to_dataset() + da_3 = DataArray([2], dims=['time'], coords=[time_3], name="a").to_dataset() + + result = combine_by_coords([da_1, da_2, da_3]) + + expected_time = np.concatenate([time_1, time_2, time_3]) + expected = DataArray( + [0, 1, 2], dims=['time'], coords=[expected_time], + name='a').to_dataset() + assert_identical(result, expected) From 48ac1a885e2913e6418a105df7964e08e518c9e4 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sat, 16 Nov 2019 13:10:54 -0500 Subject: [PATCH 2/6] black --- xarray/tests/test_combine.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index caac4d2926c..5358382b2b0 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -887,14 +887,14 @@ def test_combine_by_coords_distant_cftime_dates(): time_2 = [cftime.DatetimeGregorian(4600, 12, 31)] time_3 = [cftime.DatetimeGregorian(5100, 12, 31)] - da_1 = DataArray([0], dims=['time'], coords=[time_1], name="a").to_dataset() - da_2 = DataArray([1], dims=['time'], coords=[time_2], name="a").to_dataset() - da_3 = DataArray([2], dims=['time'], coords=[time_3], name="a").to_dataset() + da_1 = DataArray([0], dims=["time"], coords=[time_1], name="a").to_dataset() + da_2 = DataArray([1], dims=["time"], coords=[time_2], name="a").to_dataset() + da_3 = DataArray([2], dims=["time"], coords=[time_3], name="a").to_dataset() result = combine_by_coords([da_1, da_2, da_3]) expected_time = np.concatenate([time_1, time_2, time_3]) expected = DataArray( - [0, 1, 2], dims=['time'], coords=[expected_time], - name='a').to_dataset() + [0, 1, 2], dims=["time"], coords=[expected_time], name="a" + ).to_dataset() assert_identical(result, expected) From d3e32cdef88c22b902f28987456e364288f0c6d5 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sat, 16 Nov 2019 13:16:26 -0500 Subject: [PATCH 3/6] Add comment to test --- xarray/tests/test_combine.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index 5358382b2b0..a29fe0190cf 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -881,6 +881,7 @@ def test_auto_combine_without_coords(self): @requires_cftime def test_combine_by_coords_distant_cftime_dates(): + # Regression test for https://github.com/pydata/xarray/issues/3535 import cftime time_1 = [cftime.DatetimeGregorian(4500, 12, 31)] From a79675ef24ca2de7bd6906f5744b1cb2bcb326c5 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Thu, 21 Nov 2019 17:54:19 -0500 Subject: [PATCH 4/6] Move what's new entry to unreleased section --- doc/whats-new.rst | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index abb1fa57657..ca93404f292 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -29,6 +29,9 @@ New Features Bug fixes ~~~~~~~~~ +- Fix :py:meth:`xarray.combine_by_coords` when combining cftime coordinates + which span long time intervals (:issue:`3535`). By `Spencer Clark + `_. Documentation @@ -141,11 +144,6 @@ Bug fixes (:issue:`3402`). By `Deepak Cherian `_ - Allow appending datetime and bool data variables to zarr stores. (:issue:`3480`). By `Akihiro Matsukawa `_. -- Fix :py:meth:`xarray.combine_by_coords` when combining cftime coordinates - which span long time intervals (:issue:`3535`). By `Spencer Clark - `_. - - (:issue:`3480`). By `Akihiro Matsukawa `_. - Add support for numpy >=1.18 (); bugfix mean() on datetime64 arrays on dask backend (:issue:`3409`, :pull:`3537`). By `Guido Imperiale `_. - Add support for pandas >=0.26 (:issue:`3440`). From d584c5be58580c063a37980cb106e83d4c3599b8 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Thu, 21 Nov 2019 17:55:39 -0500 Subject: [PATCH 5/6] Fix link in what's new --- doc/whats-new.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ca93404f292..f2282fd0257 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -31,7 +31,7 @@ Bug fixes ~~~~~~~~~ - Fix :py:meth:`xarray.combine_by_coords` when combining cftime coordinates which span long time intervals (:issue:`3535`). By `Spencer Clark - `_. + `_. Documentation @@ -143,7 +143,7 @@ Bug fixes - Fix :py:meth:`GroupBy.reduce` when reducing over multiple dimensions. (:issue:`3402`). By `Deepak Cherian `_ - Allow appending datetime and bool data variables to zarr stores. - (:issue:`3480`). By `Akihiro Matsukawa `_. + (:issue:`3480`). By `Akihiro Matsukawa `_. - Add support for numpy >=1.18 (); bugfix mean() on datetime64 arrays on dask backend (:issue:`3409`, :pull:`3537`). By `Guido Imperiale `_. - Add support for pandas >=0.26 (:issue:`3440`). From ed43f21deac39108e0a986ebb2cb0648a0e1c78c Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sat, 7 Dec 2019 07:07:03 -0500 Subject: [PATCH 6/6] Remove newline after what's new entry --- doc/whats-new.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 00236f8e0f8..0a3406c3ebe 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,7 +37,6 @@ Bug fixes - Fix :py:meth:`xarray.combine_by_coords` when combining cftime coordinates which span long time intervals (:issue:`3535`). By `Spencer Clark `_. - - Fix plotting with transposed 2D non-dimensional coordinates. (:issue:`3138`, :pull:`3441`) By `Deepak Cherian `_. - Fix issue with Dask-backed datasets raising a ``KeyError`` on some computations involving ``map_blocks`` (:pull:`3598`)