MCPcopy
hub / github.com/pydata/xarray / test_encode_time_bounds

Function test_encode_time_bounds

xarray/tests/test_coding_times.py:766–800  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

764
765@requires_cftime
766def test_encode_time_bounds() -> None:
767 time = pd.date_range("2000-01-16", periods=1)
768 time_bounds = pd.date_range("2000-01-01", periods=2, freq="MS")
769 ds = Dataset(dict(time=time, time_bounds=time_bounds))
770 ds.time.attrs = {"bounds": "time_bounds"}
771 ds.time.encoding = {"calendar": "noleap", "units": "days since 2000-01-01"}
772
773 expected = {}
774 # expected['time'] = Variable(data=np.array([15]), dims=['time'])
775 expected["time_bounds"] = Variable(data=np.array([0, 31]), dims=["time_bounds"])
776
777 encoded, _ = cf_encoder(ds.variables, ds.attrs)
778 assert_equal(encoded["time_bounds"], expected["time_bounds"])
779 assert "calendar" not in encoded["time_bounds"].attrs
780 assert "units" not in encoded["time_bounds"].attrs
781
782 # if time_bounds attrs are same as time attrs, it doesn't matter
783 ds.time_bounds.encoding = {"calendar": "noleap", "units": "days since 2000-01-01"}
784 encoded, _ = cf_encoder(dict(ds.variables.items()), ds.attrs)
785 assert_equal(encoded["time_bounds"], expected["time_bounds"])
786 assert "calendar" not in encoded["time_bounds"].attrs
787 assert "units" not in encoded["time_bounds"].attrs
788
789 # for CF-noncompliant case of time_bounds attrs being different from
790 # time attrs; preserve them for faithful roundtrip
791 ds.time_bounds.encoding = {"calendar": "noleap", "units": "days since 1849-01-01"}
792 encoded, _ = cf_encoder(dict(ds.variables.items()), ds.attrs)
793 with pytest.raises(AssertionError):
794 assert_equal(encoded["time_bounds"], expected["time_bounds"])
795 assert "calendar" not in encoded["time_bounds"].attrs
796 assert encoded["time_bounds"].attrs["units"] == ds.time_bounds.encoding["units"]
797
798 ds.time.encoding = {}
799 with pytest.warns(UserWarning):
800 cf_encoder(ds.variables, ds.attrs)
801
802
803@pytest.fixture(params=_ALL_CALENDARS)

Callers

nothing calls this directly

Calls 5

DatasetClass · 0.90
VariableClass · 0.90
cf_encoderFunction · 0.90
assert_equalFunction · 0.90
itemsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…