11
11
Generic ,
12
12
Literal ,
13
13
NoReturn ,
14
+ TypeVar ,
15
+ Union ,
14
16
overload ,
15
17
)
16
18
61
63
ReprObject ,
62
64
_default ,
63
65
either_dict_or_kwargs ,
66
+ hashable ,
64
67
)
65
68
from xarray .core .variable import (
66
69
IndexVariable ,
73
76
from xarray .util .deprecation_helpers import _deprecate_positional_args , deprecate_dims
74
77
75
78
if TYPE_CHECKING :
76
- from typing import TypeVar , Union
77
-
79
+ from dask .dataframe import DataFrame as DaskDataFrame
80
+ from dask .delayed import Delayed
81
+ from iris .cube import Cube as iris_Cube
78
82
from numpy .typing import ArrayLike
79
83
80
- try :
81
- from dask .dataframe import DataFrame as DaskDataFrame
82
- except ImportError :
83
- DaskDataFrame = None
84
- try :
85
- from dask .delayed import Delayed
86
- except ImportError :
87
- Delayed = None # type: ignore[misc,assignment]
88
- try :
89
- from iris .cube import Cube as iris_Cube
90
- except ImportError :
91
- iris_Cube = None
92
-
93
84
from xarray .backends import ZarrStore
94
85
from xarray .backends .api import T_NetcdfEngine , T_NetcdfTypes
95
86
from xarray .core .groupby import DataArrayGroupBy
@@ -140,7 +131,9 @@ def _check_coords_dims(shape, coords, dim):
140
131
141
132
142
133
def _infer_coords_and_dims (
143
- shape , coords , dims
134
+ shape : tuple [int , ...],
135
+ coords : Sequence [Sequence | pd .Index | DataArray ] | Mapping | None ,
136
+ dims : str | Iterable [Hashable ] | None ,
144
137
) -> tuple [Mapping [Hashable , Any ], tuple [Hashable , ...]]:
145
138
"""All the logic for creating a new DataArray"""
146
139
@@ -157,8 +150,7 @@ def _infer_coords_and_dims(
157
150
158
151
if isinstance (dims , str ):
159
152
dims = (dims ,)
160
-
161
- if dims is None :
153
+ elif dims is None :
162
154
dims = [f"dim_{ n } " for n in range (len (shape ))]
163
155
if coords is not None and len (coords ) == len (shape ):
164
156
# try to infer dimensions from coords
@@ -168,16 +160,15 @@ def _infer_coords_and_dims(
168
160
for n , (dim , coord ) in enumerate (zip (dims , coords )):
169
161
coord = as_variable (coord , name = dims [n ]).to_index_variable ()
170
162
dims [n ] = coord .name
171
- dims = tuple (dims )
172
- elif len (dims ) != len (shape ):
163
+ dims_tuple = tuple (dims )
164
+ if len (dims_tuple ) != len (shape ):
173
165
raise ValueError (
174
166
"different number of dimensions on data "
175
- f"and dims: { len (shape )} vs { len (dims )} "
167
+ f"and dims: { len (shape )} vs { len (dims_tuple )} "
176
168
)
177
- else :
178
- for d in dims :
179
- if not isinstance (d , str ):
180
- raise TypeError (f"dimension { d } is not a string" )
169
+ for d in dims_tuple :
170
+ if not hashable (d ):
171
+ raise TypeError (f"Dimension { d } is not hashable" )
181
172
182
173
new_coords : Mapping [Hashable , Any ]
183
174
@@ -189,17 +180,21 @@ def _infer_coords_and_dims(
189
180
for k , v in coords .items ():
190
181
new_coords [k ] = as_variable (v , name = k )
191
182
elif coords is not None :
192
- for dim , coord in zip (dims , coords ):
183
+ for dim , coord in zip (dims_tuple , coords ):
193
184
var = as_variable (coord , name = dim )
194
185
var .dims = (dim ,)
195
186
new_coords [dim ] = var .to_index_variable ()
196
187
197
- _check_coords_dims (shape , new_coords , dims )
188
+ _check_coords_dims (shape , new_coords , dims_tuple )
198
189
199
- return new_coords , dims
190
+ return new_coords , dims_tuple
200
191
201
192
202
- def _check_data_shape (data , coords , dims ):
193
+ def _check_data_shape (
194
+ data : Any ,
195
+ coords : Sequence [Sequence | pd .Index | DataArray ] | Mapping | None ,
196
+ dims : str | Iterable [Hashable ] | None ,
197
+ ) -> Any :
203
198
if data is dtypes .NA :
204
199
data = np .nan
205
200
if coords is not None and utils .is_scalar (data , include_0d = False ):
@@ -405,10 +400,8 @@ class DataArray(
405
400
def __init__ (
406
401
self ,
407
402
data : Any = dtypes .NA ,
408
- coords : Sequence [Sequence [Any ] | pd .Index | DataArray ]
409
- | Mapping [Any , Any ]
410
- | None = None ,
411
- dims : Hashable | Sequence [Hashable ] | None = None ,
403
+ coords : Sequence [Sequence | pd .Index | DataArray ] | Mapping | None = None ,
404
+ dims : str | Iterable [Hashable ] | None = None ,
412
405
name : Hashable | None = None ,
413
406
attrs : Mapping | None = None ,
414
407
# internal parameters
0 commit comments