Skip to content

Commit 030c929

Browse files
Adapt test_dataset_math_errors for coming NumPy changes (#11435)
This PR adapts `test_dataset_math_errors` for coming updates in NumPy. We can no longer rely on arithmetic between `datetime64` values and plain integers working without a warning or error. My understanding of this test is that we want adding a `datetime64` value to `"bar"` to succeed so that we can check that the failure in adding a `datetime64` value to `"foo"` prevents updating any variable in the Dataset. Therefore we explicitly cast `"bar"` to an array of `"timedelta64[ns]"` values so that this continues to be the case moving forward. Even though it is not strictly necessary, we also provide units in the `datetime64` constructor when creating `other`. Addresses one part of #11402.
1 parent b8141f6 commit 030c929

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

‎xarray/tests/test_dataset.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6753,10 +6753,11 @@ def test_dataset_math_errors(self) -> None:
67536753
ds += ds[["bar"]]
67546754

67556755
# verify we can rollback in-place operations if something goes wrong
6756-
# nb. inplace datetime64 math actually will work with an integer array
6757-
# but not floats thanks to numpy's inconsistent handling
6758-
other = DataArray(np.datetime64("2000-01-01"), coords={"c": 2})
6756+
# (datetime64 math works with timedelta64 values stored in "bar", but
6757+
# not floats stored in "foo").
6758+
other = DataArray(np.datetime64("2000-01-01", "ns"), coords={"c": 2})
67596759
actual = ds.copy(deep=True)
6760+
actual["bar"] = actual.bar.astype("timedelta64[ns]")
67606761
with pytest.raises(TypeError):
67616762
actual += other
67626763
assert_identical(actual, ds)

0 commit comments

Comments
 (0)