Skip to content

Commit e569058

Browse files
alexamicikeewis
andauthored
Do not write an empty "coordinates" attribute to a netCDF file (#5121)
Co-authored-by: keewis <[email protected]>
1 parent f7252f4 commit e569058

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

xarray/conventions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,13 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names):
756756
# we get support for attrs["coordinates"] for free.
757757
coords_str = pop_to(encoding, attrs, "coordinates")
758758
if not coords_str and variable_coordinates[name]:
759-
attrs["coordinates"] = " ".join(
759+
coordinates_text = " ".join(
760760
str(coord_name)
761761
for coord_name in variable_coordinates[name]
762762
if coord_name not in not_technically_coordinates
763763
)
764+
if coordinates_text:
765+
attrs["coordinates"] = coordinates_text
764766
if "coordinates" in attrs:
765767
written_coords.update(attrs["coordinates"].split())
766768

xarray/tests/test_backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def test_grid_mapping_and_bounds_are_not_coordinates_in_file(self):
920920
with open_dataset(tmp_file, decode_coords=False) as ds:
921921
assert ds.coords["latitude"].attrs["bounds"] == "latitude_bnds"
922922
assert ds.coords["longitude"].attrs["bounds"] == "longitude_bnds"
923-
assert "latlon" not in ds["variable"].attrs["coordinates"]
923+
assert "coordinates" not in ds["variable"].attrs
924924
assert "coordinates" not in ds.attrs
925925

926926
def test_coordinate_variables_after_dataset_roundtrip(self):

0 commit comments

Comments
 (0)