Skip to content

Commit fe7962a

Browse files
Transpose coords by default (#3824)
* transpose coords by default * whatsnew * Update doc/whats-new.rst Co-authored-by: crusaderky <crusaderky@gmail.com> * Update whats-new.rst Co-authored-by: crusaderky <crusaderky@gmail.com>
1 parent 9ec3f7b commit fe7962a

File tree

4 files changed

+10
-35
lines changed

4 files changed

+10
-35
lines changed

doc/whats-new.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ v0.16.0 (unreleased)
2121

2222
Breaking changes
2323
~~~~~~~~~~~~~~~~
24+
25+
- ``groupby`` operations will restore coord dimension order. Pass ``restore_coord_dims=False``
26+
to revert to previous behavior.
27+
- :meth:`DataArray.transpose` will now transpose coordinates by default.
28+
Pass ``transpose_coords=False`` to revert to previous behaviour.
29+
By `Maximilian Roos <https://github.com/max-sixty>`_
2430
- Alternate draw styles for :py:meth:`plot.step` must be passed using the
2531
``drawstyle`` (or ``ds``) keyword argument, instead of the ``linestyle`` (or
2632
``ls``) keyword argument, in line with the `upstream change in Matplotlib

xarray/core/dataarray.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import datetime
22
import functools
3-
import warnings
43
from numbers import Number
54
from typing import (
65
TYPE_CHECKING,
@@ -1915,15 +1914,15 @@ def to_unstacked_dataset(self, dim, level=0):
19151914
# unstacked dataset
19161915
return Dataset(data_dict)
19171916

1918-
def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArray":
1917+
def transpose(self, *dims: Hashable, transpose_coords: bool = True) -> "DataArray":
19191918
"""Return a new DataArray object with transposed dimensions.
19201919
19211920
Parameters
19221921
----------
19231922
*dims : hashable, optional
19241923
By default, reverse the dimensions. Otherwise, reorder the
19251924
dimensions to this order.
1926-
transpose_coords : boolean, optional
1925+
transpose_coords : boolean, default True
19271926
If True, also transpose the coordinates of this DataArray.
19281927
19291928
Returns
@@ -1952,15 +1951,6 @@ def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArra
19521951
coords[name] = coord.variable.transpose(*coord_dims)
19531952
return self._replace(variable, coords)
19541953
else:
1955-
if transpose_coords is None and any(self[c].ndim > 1 for c in self.coords):
1956-
warnings.warn(
1957-
"This DataArray contains multi-dimensional "
1958-
"coordinates. In the future, these coordinates "
1959-
"will be transposed as well unless you specify "
1960-
"transpose_coords=False.",
1961-
FutureWarning,
1962-
stacklevel=2,
1963-
)
19641954
return self._replace(variable)
19651955

19661956
@property

xarray/core/groupby.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def __init__(
272272
squeeze=False,
273273
grouper=None,
274274
bins=None,
275-
restore_coord_dims=None,
275+
restore_coord_dims=True,
276276
cut_kwargs=None,
277277
):
278278
"""Create a GroupBy object
@@ -292,7 +292,7 @@ def __init__(
292292
bins : array-like, optional
293293
If `bins` is specified, the groups will be discretized into the
294294
specified bins by `pandas.cut`.
295-
restore_coord_dims : bool, optional
295+
restore_coord_dims : bool, default True
296296
If True, also restore the dimension order of multi-dimensional
297297
coordinates.
298298
cut_kwargs : dict, optional
@@ -389,21 +389,6 @@ def __init__(
389389
"Failed to group data. Are you grouping by a variable that is all NaN?"
390390
)
391391

392-
if (
393-
isinstance(obj, DataArray)
394-
and restore_coord_dims is None
395-
and any(obj[c].ndim > 1 for c in obj.coords)
396-
):
397-
warnings.warn(
398-
"This DataArray contains multi-dimensional "
399-
"coordinates. In the future, the dimension order "
400-
"of these coordinates will be restored as well "
401-
"unless you specify restore_coord_dims=False.",
402-
FutureWarning,
403-
stacklevel=2,
404-
)
405-
restore_coord_dims = False
406-
407392
# specification for the groupby operation
408393
self._obj = obj
409394
self._group = group

xarray/tests/test_dataarray.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,9 +2161,6 @@ def test_transpose(self):
21612161
with pytest.raises(ValueError):
21622162
da.transpose("x", "y")
21632163

2164-
with pytest.warns(FutureWarning):
2165-
da.transpose()
2166-
21672164
def test_squeeze(self):
21682165
assert_equal(self.dv.variable.squeeze(), self.dv.squeeze().variable)
21692166

@@ -2753,9 +2750,6 @@ def test_groupby_restore_coord_dims(self):
27532750
)["c"]
27542751
assert result.dims == expected_dims
27552752

2756-
with pytest.warns(FutureWarning):
2757-
array.groupby("x").map(lambda x: x.squeeze())
2758-
27592753
def test_groupby_first_and_last(self):
27602754
array = DataArray([1, 2, 3, 4, 5], dims="x")
27612755
by = DataArray(["a"] * 2 + ["b"] * 3, dims="x", name="ab")

0 commit comments

Comments
 (0)