From 2b932737b76c06232014138e6412aaf7b02ffe3a Mon Sep 17 00:00:00 2001 From: Anderson Banihirwe Date: Fri, 27 Oct 2023 16:01:10 -0700 Subject: [PATCH] revert to .set_dims on Variable --- xarray/core/variable.py | 20 ++++++++++++++++++++ xarray/namedarray/core.py | 3 +-- xarray/namedarray/utils.py | 22 ++++++++++------------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/xarray/core/variable.py b/xarray/core/variable.py index a675f3e2f0f..897e3f2dcf1 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -45,6 +45,7 @@ is_duck_array, maybe_coerce_to_str, ) +from xarray.namedarray._typing import _DimsLike, _ShapeLike from xarray.namedarray.core import NamedArray NON_NUMPY_SUPPORTED_ARRAY_TYPES = ( @@ -1381,6 +1382,25 @@ def roll(self, shifts=None, **shifts_kwargs): result = result._roll_one_dim(dim, count) return result + def set_dims(self, dims: _DimsLike, shape: _ShapeLike | None = None) -> Self: + """ + Return a new variable with given set of dimensions. + This method might be used to attach new dimension(s) to variable. + When possible, this operation does not copy this variable's data. + + Parameters + ---------- + dims : str or sequence of str or dict + Dimensions to include on the new object (must be a superset of the existing dimensions). + If a dict, values are used to provide the sizes of new dimensions; otherwise, new dimensions are inserted with length 1. + + shape : sequence of int, optional + Shape to broadcast the data to. Must be specified in the same order as `dims`. + If not provided, new dimensions are inserted with length 1. + """ + + return self.expand_dims(dims, shape) + def _stack_once(self, dims: list[Hashable], new_dim: Hashable): if not set(dims) <= set(self.dims): raise ValueError(f"invalid existing dimensions: {dims}") diff --git a/xarray/namedarray/core.py b/xarray/namedarray/core.py index c2fe807c1d3..e90858e4a9c 100644 --- a/xarray/namedarray/core.py +++ b/xarray/namedarray/core.py @@ -913,8 +913,7 @@ def T(self) -> Self: def broadcast_to(self, shape: _ShapeLike) -> NamedArray[Any, _DType_co]: from xarray.core import duck_array_ops # TODO: remove this import - data = duck_array_ops.broadcast_to(self.data, shape) - return self._replace(data=data) + return duck_array_ops.broadcast_to(self.data, shape) def _create_expanded_obj( self, expanded_data: duckarray[Any, Any], expanded_dims: _DimsLike diff --git a/xarray/namedarray/utils.py b/xarray/namedarray/utils.py index ca8d7f62176..b5119374fa0 100644 --- a/xarray/namedarray/utils.py +++ b/xarray/namedarray/utils.py @@ -2,13 +2,13 @@ import sys import warnings -from collections.abc import Collection, Hashable, Iterable, Iterator, Mapping +from collections.abc import Hashable, Iterator, Mapping from enum import Enum from typing import TYPE_CHECKING, Any, Final import numpy as np -from xarray.namedarray._typing import ErrorOptionsWithWarn +from xarray.namedarray._typing import ErrorOptionsWithWarn, _DimsLike if TYPE_CHECKING: if sys.version_info >= (3, 10): @@ -82,10 +82,10 @@ def is_dict_like(value: Any) -> TypeGuard[Mapping[Any, Any]]: def drop_missing_dims( - supplied_dims: Iterable[Hashable], - dims: Iterable[Hashable], + supplied_dims: _DimsLike, + dims: _DimsLike, missing_dims: ErrorOptionsWithWarn, -) -> Iterable[Hashable]: +) -> _DimsLike: """Depending on the setting of missing_dims, drop any dimensions from supplied_dims that are not present in dims. @@ -98,8 +98,7 @@ def drop_missing_dims( if missing_dims == "raise": supplied_dims_set = {val for val in supplied_dims if val is not ...} - invalid = supplied_dims_set - set(dims) - if invalid: + if invalid := supplied_dims_set - set(dims): raise ValueError( f"Dimensions {invalid} do not exist. Expected one or more of {dims}" ) @@ -107,8 +106,7 @@ def drop_missing_dims( return supplied_dims elif missing_dims == "warn": - invalid = set(supplied_dims) - set(dims) - if invalid: + if invalid := set(supplied_dims) - set(dims): warnings.warn( f"Dimensions {invalid} do not exist. Expected one or more of {dims}" ) @@ -125,10 +123,10 @@ def drop_missing_dims( def infix_dims( - dims_supplied: Collection[Any], - dims_all: Collection[Any], + dims_supplied: _DimsLike, + dims_all: _DimsLike, missing_dims: ErrorOptionsWithWarn = "raise", -) -> Iterator[Any]: +) -> Iterator[_DimsLike]: """ Resolves a supplied list containing an ellipsis representing other items, to a generator with the 'realized' list of all items