diff --git a/xarray/conventions.py b/xarray/conventions.py index 6eff45c5b2d..e6fab022aa3 100644 --- a/xarray/conventions.py +++ b/xarray/conventions.py @@ -794,24 +794,4 @@ def cf_encoder(variables: T_Variables, attributes: T_Attrs): new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()} - # Remove attrs from bounds variables (issue #2921) - for var in new_vars.values(): - bounds = var.attrs["bounds"] if "bounds" in var.attrs else None - if bounds and bounds in new_vars: - # see http://cfconventions.org/cf-conventions/cf-conventions.html#cell-boundaries - for attr in [ - "units", - "standard_name", - "axis", - "positive", - "calendar", - "long_name", - "leap_month", - "leap_year", - "month_lengths", - ]: - if attr in new_vars[bounds].attrs and attr in var.attrs: - if new_vars[bounds].attrs[attr] == var.attrs[attr]: - new_vars[bounds].attrs.pop(attr) - return new_vars, attributes diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 9a5589ff872..c168993616f 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -732,23 +732,25 @@ def test_encode_time_bounds() -> None: encoded, _ = cf_encoder(ds.variables, ds.attrs) assert_equal(encoded["time_bounds"], expected["time_bounds"]) - assert "calendar" not in encoded["time_bounds"].attrs - assert "units" not in encoded["time_bounds"].attrs # if time_bounds attrs are same as time attrs, it doesn't matter ds.time_bounds.encoding = {"calendar": "noleap", "units": "days since 2000-01-01"} encoded, _ = cf_encoder({k: v for k, v in ds.variables.items()}, ds.attrs) assert_equal(encoded["time_bounds"], expected["time_bounds"]) - assert "calendar" not in encoded["time_bounds"].attrs - assert "units" not in encoded["time_bounds"].attrs # for CF-noncompliant case of time_bounds attrs being different from # time attrs; preserve them for faithful roundtrip - ds.time_bounds.encoding = {"calendar": "noleap", "units": "days since 1849-01-01"} + ds.time_bounds.encoding = { + "calendar": "gregorian", + "units": "days since 1849-01-01", + } encoded, _ = cf_encoder({k: v for k, v in ds.variables.items()}, ds.attrs) with pytest.raises(AssertionError): assert_equal(encoded["time_bounds"], expected["time_bounds"]) - assert "calendar" not in encoded["time_bounds"].attrs + assert encoded["time"].attrs["calendar"] == ds.time.encoding["calendar"] + assert ( + encoded["time_bounds"].attrs["calendar"] == ds.time_bounds.encoding["calendar"] + ) assert encoded["time_bounds"].attrs["units"] == ds.time_bounds.encoding["units"] ds.time.encoding = {}