Skip to content

Commit 1c446d3

Browse files
spencerkclarkdcherian
authored andcommitted
Minor fix to combine_by_coords to allow for the combination of CFTimeIndexes separated by large time intervals (#3543)
* Select first element of each index * black * Add comment to test * Move what's new entry to unreleased section * Fix link in what's new * Remove newline after what's new entry
1 parent cafcaee commit 1c446d3

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ New Features
3434

3535
Bug fixes
3636
~~~~~~~~~
37+
- Fix :py:meth:`xarray.combine_by_coords` when combining cftime coordinates
38+
which span long time intervals (:issue:`3535`). By `Spencer Clark
39+
<https://github.com/spencerkclark>`_.
3740
- Fix plotting with transposed 2D non-dimensional coordinates. (:issue:`3138`, :pull:`3441`)
3841
By `Deepak Cherian <https://github.com/dcherian>`_.
3942
- Fix issue with Dask-backed datasets raising a ``KeyError`` on some computations involving ``map_blocks`` (:pull:`3598`)

xarray/core/combine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _infer_concat_order_from_coords(datasets):
8888
# with the same value have the same coord values throughout.
8989
if any(index.size == 0 for index in indexes):
9090
raise ValueError("Cannot handle size zero dimensions")
91-
first_items = pd.Index([index.take([0]) for index in indexes])
91+
first_items = pd.Index([index[0] for index in indexes])
9292

9393
# Sort datasets along dim
9494
# We want rank but with identical elements given identical

xarray/tests/test_combine.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
_new_tile_id,
2323
)
2424

25-
from . import assert_equal, assert_identical, raises_regex
25+
from . import assert_equal, assert_identical, raises_regex, requires_cftime
2626
from .test_dataset import create_test_data
2727

2828

@@ -877,3 +877,25 @@ def test_auto_combine_without_coords(self):
877877
objs = [Dataset({"foo": ("x", [0])}), Dataset({"foo": ("x", [1])})]
878878
with pytest.warns(FutureWarning, match="supplied do not have global"):
879879
auto_combine(objs)
880+
881+
882+
@requires_cftime
883+
def test_combine_by_coords_distant_cftime_dates():
884+
# Regression test for https://github.com/pydata/xarray/issues/3535
885+
import cftime
886+
887+
time_1 = [cftime.DatetimeGregorian(4500, 12, 31)]
888+
time_2 = [cftime.DatetimeGregorian(4600, 12, 31)]
889+
time_3 = [cftime.DatetimeGregorian(5100, 12, 31)]
890+
891+
da_1 = DataArray([0], dims=["time"], coords=[time_1], name="a").to_dataset()
892+
da_2 = DataArray([1], dims=["time"], coords=[time_2], name="a").to_dataset()
893+
da_3 = DataArray([2], dims=["time"], coords=[time_3], name="a").to_dataset()
894+
895+
result = combine_by_coords([da_1, da_2, da_3])
896+
897+
expected_time = np.concatenate([time_1, time_2, time_3])
898+
expected = DataArray(
899+
[0, 1, 2], dims=["time"], coords=[expected_time], name="a"
900+
).to_dataset()
901+
assert_identical(result, expected)

0 commit comments

Comments
 (0)