Skip to content

Commit 92e49f9

Browse files
authored
combine_by_coords: error on differing calendars (#4503)
* mend * add test and doc * remove newline
1 parent 98e9692 commit 92e49f9

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Bug fixes
4545
is now ignored when not applicable, i.e. when ``skipna=False`` or when ``skipna=None``
4646
and the dtype does not have a missing value (:issue:`4352`).
4747
By `Mathias Hauser <https://github.com/mathause>`_.
48+
- :py:func:`combine_by_coords` now raises an informative error when passing coordinates
49+
with differing calendars (:issue:`4495`). By `Mathias Hauser <https://github.com/mathause>`_.
4850

4951
Documentation
5052
~~~~~~~~~~~~~

xarray/core/combine.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def _infer_concat_order_from_coords(datasets):
9393
# position indices - they should be concatenated along another
9494
# dimension, not along this one
9595
series = first_items.to_series()
96-
rank = series.rank(method="dense", ascending=ascending)
96+
rank = series.rank(
97+
method="dense", ascending=ascending, numeric_only=False
98+
)
9799
order = rank.astype(int).values - 1
98100

99101
# Append positions along extra dimension to structure which

xarray/tests/test_combine.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,3 +839,20 @@ def test_combine_by_coords_distant_cftime_dates():
839839
[0, 1, 2], dims=["time"], coords=[expected_time], name="a"
840840
).to_dataset()
841841
assert_identical(result, expected)
842+
843+
844+
@requires_cftime
845+
def test_combine_by_coords_raises_for_differing_calendars():
846+
# previously failed with uninformative StopIteration instead of TypeError
847+
# https://github.com/pydata/xarray/issues/4495
848+
849+
import cftime
850+
851+
time_1 = [cftime.DatetimeGregorian(2000, 1, 1)]
852+
time_2 = [cftime.DatetimeProlepticGregorian(2001, 1, 1)]
853+
854+
da_1 = DataArray([0], dims=["time"], coords=[time_1], name="a").to_dataset()
855+
da_2 = DataArray([1], dims=["time"], coords=[time_2], name="a").to_dataset()
856+
857+
with raises_regex(TypeError, r"cannot compare .* \(different calendars\)"):
858+
combine_by_coords([da_1, da_2])

0 commit comments

Comments
 (0)