Skip to content
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

Transpose coords by default #3824

Merged
merged 12 commits into from
May 6, 2020
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ v0.16.0 (unreleased)

Breaking changes
~~~~~~~~~~~~~~~~

- ``groupby`` operations will restore coord dimension order. Pass ``restore_coord_dims=False``
to revert to previous behavior.
- :meth:`DataArray.transpose` will now transpose coordinates by default.
Pass ``transpose_coords=False`` to revert to previous behaviour.
By `Maximilian Roos <https://github.com/max-sixty>`_
- Alternate draw styles for :py:meth:`plot.step` must be passed using the
``drawstyle`` (or ``ds``) keyword argument, instead of the ``linestyle`` (or
``ls``) keyword argument, in line with the `upstream change in Matplotlib
Expand Down
14 changes: 2 additions & 12 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import functools
import warnings
from numbers import Number
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -1915,15 +1914,15 @@ def to_unstacked_dataset(self, dim, level=0):
# unstacked dataset
return Dataset(data_dict)

def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArray":
def transpose(self, *dims: Hashable, transpose_coords: bool = True) -> "DataArray":
"""Return a new DataArray object with transposed dimensions.

Parameters
----------
*dims : hashable, optional
By default, reverse the dimensions. Otherwise, reorder the
dimensions to this order.
transpose_coords : boolean, optional
transpose_coords : boolean, default True
If True, also transpose the coordinates of this DataArray.

Returns
Expand Down Expand Up @@ -1952,15 +1951,6 @@ def transpose(self, *dims: Hashable, transpose_coords: bool = None) -> "DataArra
coords[name] = coord.variable.transpose(*coord_dims)
return self._replace(variable, coords)
else:
if transpose_coords is None and any(self[c].ndim > 1 for c in self.coords):
warnings.warn(
"This DataArray contains multi-dimensional "
"coordinates. In the future, these coordinates "
"will be transposed as well unless you specify "
"transpose_coords=False.",
FutureWarning,
stacklevel=2,
)
return self._replace(variable)

@property
Expand Down
19 changes: 2 additions & 17 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def __init__(
squeeze=False,
grouper=None,
bins=None,
restore_coord_dims=None,
restore_coord_dims=True,
cut_kwargs=None,
):
"""Create a GroupBy object
Expand All @@ -292,7 +292,7 @@ def __init__(
bins : array-like, optional
If `bins` is specified, the groups will be discretized into the
specified bins by `pandas.cut`.
restore_coord_dims : bool, optional
restore_coord_dims : bool, default True
If True, also restore the dimension order of multi-dimensional
coordinates.
cut_kwargs : dict, optional
Expand Down Expand Up @@ -389,21 +389,6 @@ def __init__(
"Failed to group data. Are you grouping by a variable that is all NaN?"
)

if (
isinstance(obj, DataArray)
and restore_coord_dims is None
and any(obj[c].ndim > 1 for c in obj.coords)
):
warnings.warn(
"This DataArray contains multi-dimensional "
"coordinates. In the future, the dimension order "
"of these coordinates will be restored as well "
"unless you specify restore_coord_dims=False.",
FutureWarning,
stacklevel=2,
)
restore_coord_dims = False

# specification for the groupby operation
self._obj = obj
self._group = group
Expand Down
6 changes: 0 additions & 6 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2161,9 +2161,6 @@ def test_transpose(self):
with pytest.raises(ValueError):
da.transpose("x", "y")

with pytest.warns(FutureWarning):
da.transpose()

def test_squeeze(self):
assert_equal(self.dv.variable.squeeze(), self.dv.squeeze().variable)

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

with pytest.warns(FutureWarning):
array.groupby("x").map(lambda x: x.squeeze())

def test_groupby_first_and_last(self):
array = DataArray([1, 2, 3, 4, 5], dims="x")
by = DataArray(["a"] * 2 + ["b"] * 3, dims="x", name="ab")
Expand Down