Skip to content
Open
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
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ Bug Fixes
By `Emmanuel Ferdman <https://github.com/emmanuel-ferdman>`_.
- :func:`combine_by_coords` no longer returns an empty dataset when a generator is passed as ``data_objects`` (:issue:`10114`, :pull:`11265`).
By `Amartya Anand <https://github.com/SurfyPenguin>`_.
- Warn when tuple-style ``DataArray`` coordinates are renamed by explicitly
provided dimension names (:issue:`11234`).
By `Asish Kumar <https://github.com/officialasishkumar>`_.
- Fix h5netcdf backend module detection and ros3 tests (:issue:`11243`, :pull:`11274`).
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.

Expand Down
15 changes: 15 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ def _infer_coords_and_dims(
if not hashable(d):
raise TypeError(f"Dimension {d} is not hashable")

if coords is not None and not utils.is_dict_like(coords):
if any(
isinstance(coord, tuple)
Comment thread
headtr1ck marked this conversation as resolved.
and len(coord) >= 2
and hashable(coord[0])
and coord[0] != dim
for dim, coord in zip(dims_tuple, coords, strict=True)
):
utils.emit_user_level_warning(
"Coordinate names in tuple-style coords are ignored when `dims` "
"are provided. Use a mapping for `coords` if you need named "
"coordinates.",
UserWarning,
)

new_coords: Mapping[Hashable, Any]

if isinstance(coords, Coordinates):
Expand Down
32 changes: 32 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,38 @@ def test_constructor(self) -> None:
expected = DataArray([1, 2, 3], coords=[("x", [0, 1, 2])])
assert_identical(expected, actual)

def test_constructor_tuple_coords_warn_when_dims_override_names(self) -> None:
data = np.random.random((2, 3))
coords = [("a", [0, 1]), ("b", [-1, -2, -3])]

with pytest.warns(
UserWarning,
match="Coordinate names in tuple-style coords are ignored",
):
actual = DataArray(data, coords=coords, dims=["x", "y"])

expected = Dataset(
{None: (["x", "y"], data)},
coords={"x": [0, 1], "y": [-1, -2, -3]},
)[None]
assert_identical(expected, actual)

def test_constructor_tuple_coords_no_warning_when_names_match_dims(self) -> None:
data = np.random.random((2, 3))

with assert_no_warnings():
actual = DataArray(
data,
coords=[("x", [0, 1]), ("y", [-1, -2, -3])],
dims=["x", "y"],
)

expected = Dataset(
{None: (["x", "y"], data)},
coords={"x": [0, 1], "y": [-1, -2, -3]},
)[None]
assert_identical(expected, actual)

def test_constructor_invalid(self) -> None:
data = np.random.randn(3, 2)

Expand Down
Loading