Skip to content

Omit coordinates using False instead of None #8254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,12 @@ def _encode_coordinates(variables, attributes, non_dim_coord_names):
f"'coordinates' found in both attrs and encoding for variable {name!r}."
)

# if coordinates set to None, don't write coordinates attribute
# if coordinates set to False, don't write coordinates attribute
if (
"coordinates" in attrs
and attrs.get("coordinates") is None
and attrs.get("coordinates") is False
or "coordinates" in encoding
and encoding.get("coordinates") is None
and encoding.get("coordinates") is False
):
# make sure "coordinates" is removed from attrs/encoding
attrs.pop("coordinates", None)
Expand Down
34 changes: 26 additions & 8 deletions xarray/tests/test_conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,38 +204,56 @@ def test_deterministic_coords_encoding(self) -> None:
assert attrs["coordinates"] == "bar baz"

@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
def test_emit_coordinates_attribute_in_attrs(self) -> None:
def test_omit_coordinates_attribute_in_attrs(self) -> None:
orig = Dataset(
{"a": 1, "b": 1},
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
)

orig["a"].attrs["coordinates"] = None
orig["a"].attrs["coordinates"] = False
enc, _ = conventions.encode_dataset_coordinates(orig)

# check coordinate attribute emitted for 'a'
# check coordinate attribute omitted for 'a'
assert "coordinates" not in enc["a"].attrs
assert "coordinates" not in enc["a"].encoding

# check coordinate attribute not emitted for 'b'
# check coordinate attribute not omitted for 'b'
assert enc["b"].attrs.get("coordinates") == "t"
assert "coordinates" not in enc["b"].encoding

@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
def test_emit_coordinates_attribute_in_encoding(self) -> None:
def test_omit_coordinates_attribute_in_assign_attrs(self) -> None:
orig = Dataset(
{"a": 1, "b": 1},
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
)

orig["a"].encoding["coordinates"] = None
orig["a"] = orig["a"].assign_attrs({"coordinates": False})
enc, _ = conventions.encode_dataset_coordinates(orig)

# check coordinate attribute emitted for 'a'
# check coordinate attribute omitted for 'a'
assert "coordinates" not in enc["a"].attrs
assert "coordinates" not in enc["a"].encoding

# check coordinate attribute not emitted for 'b'
# check coordinate attribute not omitted for 'b'
assert enc["b"].attrs.get("coordinates") == "t"
assert "coordinates" not in enc["b"].encoding

@pytest.mark.filterwarnings("ignore:Converting non-nanosecond")
def test_omit_coordinates_attribute_in_encoding(self) -> None:
orig = Dataset(
{"a": 1, "b": 1},
coords={"t": np.array("2004-11-01T00:00:00", dtype=np.datetime64)},
)

orig["a"].encoding["coordinates"] = False
enc, _ = conventions.encode_dataset_coordinates(orig)

# check coordinate attribute omitted for 'a'
assert "coordinates" not in enc["a"].attrs
assert "coordinates" not in enc["a"].encoding

# check coordinate attribute not omitted for 'b'
assert enc["b"].attrs.get("coordinates") == "t"
assert "coordinates" not in enc["b"].encoding

Expand Down