Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fix to combine_by_coords to allow for the combination of CFTimeIndexes separated by large time intervals #3543

Merged
merged 8 commits into from
Dec 7, 2019
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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
<https://github.com/spencerkclark>`_.
- Fix plotting with transposed 2D non-dimensional coordinates. (:issue:`3138`, :pull:`3441`)
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix issue with Dask-backed datasets raising a ``KeyError`` on some computations involving ``map_blocks`` (:pull:`3598`)
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -877,3 +877,25 @@ 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():
# Regression test for https://github.com/pydata/xarray/issues/3535
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)