Skip to content

Commit ede38e5

Browse files
Warn when explicit dims rename tuple-style DataArray coords (#11292)
* Warn when tuple-style DataArray coords are renamed --------- Signed-off-by: Asish Kumar <[email protected]> Co-authored-by: Michael Niklas <[email protected]>
1 parent 154bdf6 commit ede38e5

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎doc/whats-new.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ Bug Fixes
438438
By `Emmanuel Ferdman <https://github.com/emmanuel-ferdman>`_.
439439
- :func:`combine_by_coords` no longer returns an empty dataset when a generator is passed as ``data_objects`` (:issue:`10114`, :pull:`11265`).
440440
By `Amartya Anand <https://github.com/SurfyPenguin>`_.
441+
- Warn when tuple-style ``DataArray`` coordinates are renamed by explicitly
442+
provided dimension names (:issue:`11234`, :pull:`11292`).
443+
By `Asish Kumar <https://github.com/officialasishkumar>`_.
441444
- Preserve non-grouped coordinates in fallback ``groupby`` reductions when grouping
442445
by a non-leading dimension reorders the underlying variable dimensions
443446
(:issue:`11188`, :pull:`11290`).

‎xarray/core/dataarray.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ def _infer_coords_and_dims(
167167
if not hashable(d):
168168
raise TypeError(f"Dimension {d} is not hashable")
169169

170+
if coords is not None and not utils.is_dict_like(coords):
171+
if any(
172+
isinstance(coord, tuple)
173+
and len(coord) >= 2
174+
and hashable(coord[0])
175+
and coord[0] != dim
176+
for dim, coord in zip(dims_tuple, coords, strict=True)
177+
):
178+
utils.emit_user_level_warning(
179+
"Coordinate names in tuple-style coords are ignored when `dims` "
180+
"are provided. Use a mapping for `coords` if you need named "
181+
"coordinates.",
182+
UserWarning,
183+
)
184+
170185
new_coords: Mapping[Hashable, Any]
171186

172187
if isinstance(coords, Coordinates):

‎xarray/tests/test_dataarray.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,38 @@ def test_constructor(self) -> None:
410410
expected = DataArray([1, 2, 3], coords=[("x", [0, 1, 2])])
411411
assert_identical(expected, actual)
412412

413+
def test_constructor_tuple_coords_warn_when_dims_override_names(self) -> None:
414+
data = np.random.random((2, 3))
415+
coords = [("a", [0, 1]), ("b", [-1, -2, -3])]
416+
417+
with pytest.warns(
418+
UserWarning,
419+
match="Coordinate names in tuple-style coords are ignored",
420+
):
421+
actual = DataArray(data, coords=coords, dims=["x", "y"])
422+
423+
expected = Dataset(
424+
{None: (["x", "y"], data)},
425+
coords={"x": [0, 1], "y": [-1, -2, -3]},
426+
)[None]
427+
assert_identical(expected, actual)
428+
429+
def test_constructor_tuple_coords_no_warning_when_names_match_dims(self) -> None:
430+
data = np.random.random((2, 3))
431+
432+
with assert_no_warnings():
433+
actual = DataArray(
434+
data,
435+
coords=[("x", [0, 1]), ("y", [-1, -2, -3])],
436+
dims=["x", "y"],
437+
)
438+
439+
expected = Dataset(
440+
{None: (["x", "y"], data)},
441+
coords={"x": [0, 1], "y": [-1, -2, -3]},
442+
)[None]
443+
assert_identical(expected, actual)
444+
413445
def test_constructor_invalid(self) -> None:
414446
data = np.random.randn(3, 2)
415447

0 commit comments

Comments
 (0)