Skip to content

Commit c472f8a

Browse files
authored
Allow user to explicitly disable coordinates attribute (#5514)
1 parent 0e7c92c commit c472f8a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ New Features
4848
By `Justus Magin <https://github.com/keewis>`_.
4949
- Allow plotting categorical data (:pull:`5464`).
5050
By `Jimmy Westling <https://github.com/illviljan>`_.
51+
- Allow removal of the coordinate attribute ``coordinates`` on variables by setting ``.attrs['coordinates']= None``
52+
(:issue:`5510`).
53+
By `Elle Smith <https://github.com/ellesmith88>`_.
5154

5255
Breaking changes
5356
~~~~~~~~~~~~~~~~

xarray/conventions.py

+12
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,18 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names):
751751
f"'coordinates' found in both attrs and encoding for variable {name!r}."
752752
)
753753

754+
# if coordinates set to None, don't write coordinates attribute
755+
if (
756+
"coordinates" in attrs
757+
and attrs.get("coordinates") is None
758+
or "coordinates" in encoding
759+
and encoding.get("coordinates") is None
760+
):
761+
# make sure "coordinates" is removed from attrs/encoding
762+
attrs.pop("coordinates", None)
763+
encoding.pop("coordinates", None)
764+
continue
765+
754766
# this will copy coordinates from encoding to attrs if "coordinates" in attrs
755767
# after the next line, "coordinates" is never in encoding
756768
# we get support for attrs["coordinates"] for free.

xarray/tests/test_conventions.py

+34
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,40 @@ def test_do_not_overwrite_user_coordinates(self):
142142
with pytest.raises(ValueError, match=r"'coordinates' found in both attrs"):
143143
conventions.encode_dataset_coordinates(orig)
144144

145+
def test_emit_coordinates_attribute_in_attrs(self):
146+
orig = Dataset(
147+
{"a": 1, "b": 1},
148+
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
149+
)
150+
151+
orig["a"].attrs["coordinates"] = None
152+
enc, _ = conventions.encode_dataset_coordinates(orig)
153+
154+
# check coordinate attribute emitted for 'a'
155+
assert "coordinates" not in enc["a"].attrs
156+
assert "coordinates" not in enc["a"].encoding
157+
158+
# check coordinate attribute not emitted for 'b'
159+
assert enc["b"].attrs.get("coordinates") == "t"
160+
assert "coordinates" not in enc["b"].encoding
161+
162+
def test_emit_coordinates_attribute_in_encoding(self):
163+
orig = Dataset(
164+
{"a": 1, "b": 1},
165+
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
166+
)
167+
168+
orig["a"].encoding["coordinates"] = None
169+
enc, _ = conventions.encode_dataset_coordinates(orig)
170+
171+
# check coordinate attribute emitted for 'a'
172+
assert "coordinates" not in enc["a"].attrs
173+
assert "coordinates" not in enc["a"].encoding
174+
175+
# check coordinate attribute not emitted for 'b'
176+
assert enc["b"].attrs.get("coordinates") == "t"
177+
assert "coordinates" not in enc["b"].encoding
178+
145179
@requires_dask
146180
def test_string_object_warning(self):
147181
original = Variable(("x",), np.array(["foo", "bar"], dtype=object)).chunk()

0 commit comments

Comments
 (0)