Skip to content

Commit eab4fa8

Browse files
committed
Add tests for invalid units and reference date
1 parent e56ede2 commit eab4fa8

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

xarray/coding/times.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def _unpack_netcdf_time_units(units):
127127
# whitespace. It also ensures that the year is padded with zeros
128128
# so it will be correctly understood by pandas (via dateutil).
129129
matches = re.match(r"(.+) since (.+)", units)
130+
130131
if not matches:
131132
raise ValueError(f"invalid time units: {units}")
132133

xarray/tests/test_coding_times.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,12 @@ def test_encode_cf_datetime_overflow(shape):
823823
roundtrip = decode_cf_datetime(num, units, calendar)
824824
np.testing.assert_array_equal(dates, roundtrip)
825825

826+
with pytest.raises(ValueError, match="invalid time units"):
827+
encode_cf_datetime(dates, units="days after 2000-01-01")
828+
829+
with pytest.raises(ValueError, match="invalid reference date"):
830+
encode_cf_datetime(dates, units="days since NO_YEAR")
831+
826832

827833
def test_encode_cf_datetime_pandas_min():
828834
# GH 2623

0 commit comments

Comments
 (0)