@@ -2904,9 +2904,8 @@ def sel(
2904
2904
for k , v in query_results .variables .items ():
2905
2905
if v .dims :
2906
2906
no_scalar_variables [k ] = v
2907
- else :
2908
- if k in self ._coord_names :
2909
- query_results .drop_coords .append (k )
2907
+ elif k in self ._coord_names :
2908
+ query_results .drop_coords .append (k )
2910
2909
query_results .variables = no_scalar_variables
2911
2910
2912
2911
result = self .isel (indexers = query_results .dim_indexers , drop = drop )
@@ -4552,26 +4551,25 @@ def expand_dims(
4552
4551
for d , c in zip_axis_dim :
4553
4552
all_dims .insert (d , c )
4554
4553
variables [k ] = v .set_dims (dict (all_dims ))
4555
- else :
4556
- if k not in variables :
4557
- if k in coord_names and create_index_for_new_dim :
4558
- # If dims includes a label of a non-dimension coordinate,
4559
- # it will be promoted to a 1D coordinate with a single value.
4560
- index , index_vars = create_default_index_implicit (v .set_dims (k ))
4561
- indexes [k ] = index
4562
- variables .update (index_vars )
4563
- else :
4564
- if create_index_for_new_dim :
4565
- warnings .warn (
4566
- f"No index created for dimension { k } because variable { k } is not a coordinate. "
4567
- f"To create an index for { k } , please first call `.set_coords('{ k } ')` on this object." ,
4568
- UserWarning ,
4569
- stacklevel = 2 ,
4570
- )
4554
+ elif k not in variables :
4555
+ if k in coord_names and create_index_for_new_dim :
4556
+ # If dims includes a label of a non-dimension coordinate,
4557
+ # it will be promoted to a 1D coordinate with a single value.
4558
+ index , index_vars = create_default_index_implicit (v .set_dims (k ))
4559
+ indexes [k ] = index
4560
+ variables .update (index_vars )
4561
+ else :
4562
+ if create_index_for_new_dim :
4563
+ warnings .warn (
4564
+ f"No index created for dimension { k } because variable { k } is not a coordinate. "
4565
+ f"To create an index for { k } , please first call `.set_coords('{ k } ')` on this object." ,
4566
+ UserWarning ,
4567
+ stacklevel = 2 ,
4568
+ )
4571
4569
4572
- # create 1D variable without creating a new index
4573
- new_1d_var = v .set_dims (k )
4574
- variables .update ({k : new_1d_var })
4570
+ # create 1D variable without creating a new index
4571
+ new_1d_var = v .set_dims (k )
4572
+ variables .update ({k : new_1d_var })
4575
4573
4576
4574
return self ._replace_with_new_dims (
4577
4575
variables , coord_names = coord_names , indexes = indexes
@@ -4890,9 +4888,8 @@ def set_xindex(
4890
4888
index_cls = PandasIndex
4891
4889
else :
4892
4890
index_cls = PandasMultiIndex
4893
- else :
4894
- if not issubclass (index_cls , Index ):
4895
- raise TypeError (f"{ index_cls } is not a subclass of xarray.Index" )
4891
+ elif not issubclass (index_cls , Index ):
4892
+ raise TypeError (f"{ index_cls } is not a subclass of xarray.Index" )
4896
4893
4897
4894
invalid_coords = set (coord_names ) - self ._coord_names
4898
4895
@@ -6744,34 +6741,33 @@ def reduce(
6744
6741
if name in self .coords :
6745
6742
if not reduce_dims :
6746
6743
variables [name ] = var
6747
- else :
6748
- if (
6749
- # Some reduction functions (e.g. std, var) need to run on variables
6750
- # that don't have the reduce dims: PR5393
6751
- not is_extension_array_dtype (var .dtype )
6752
- and (
6753
- not reduce_dims
6754
- or not numeric_only
6755
- or np .issubdtype (var .dtype , np .number )
6756
- or (var .dtype == np .bool_ )
6757
- )
6758
- ):
6759
- # prefer to aggregate over axis=None rather than
6760
- # axis=(0, 1) if they will be equivalent, because
6761
- # the former is often more efficient
6762
- # keep single-element dims as list, to support Hashables
6763
- reduce_maybe_single = (
6764
- None
6765
- if len (reduce_dims ) == var .ndim and var .ndim != 1
6766
- else reduce_dims
6767
- )
6768
- variables [name ] = var .reduce (
6769
- func ,
6770
- dim = reduce_maybe_single ,
6771
- keep_attrs = keep_attrs ,
6772
- keepdims = keepdims ,
6773
- ** kwargs ,
6774
- )
6744
+ elif (
6745
+ # Some reduction functions (e.g. std, var) need to run on variables
6746
+ # that don't have the reduce dims: PR5393
6747
+ not is_extension_array_dtype (var .dtype )
6748
+ and (
6749
+ not reduce_dims
6750
+ or not numeric_only
6751
+ or np .issubdtype (var .dtype , np .number )
6752
+ or (var .dtype == np .bool_ )
6753
+ )
6754
+ ):
6755
+ # prefer to aggregate over axis=None rather than
6756
+ # axis=(0, 1) if they will be equivalent, because
6757
+ # the former is often more efficient
6758
+ # keep single-element dims as list, to support Hashables
6759
+ reduce_maybe_single = (
6760
+ None
6761
+ if len (reduce_dims ) == var .ndim and var .ndim != 1
6762
+ else reduce_dims
6763
+ )
6764
+ variables [name ] = var .reduce (
6765
+ func ,
6766
+ dim = reduce_maybe_single ,
6767
+ keep_attrs = keep_attrs ,
6768
+ keepdims = keepdims ,
6769
+ ** kwargs ,
6770
+ )
6775
6771
6776
6772
coord_names = {k for k in self .coords if k in variables }
6777
6773
indexes = {k : v for k , v in self ._indexes .items () if k in variables }
@@ -7970,8 +7966,6 @@ def sortby(
7970
7966
variables = variables (self )
7971
7967
if not isinstance (variables , list ):
7972
7968
variables = [variables ]
7973
- else :
7974
- variables = variables
7975
7969
arrays = [v if isinstance (v , DataArray ) else self [v ] for v in variables ]
7976
7970
aligned_vars = align (self , * arrays , join = "left" )
7977
7971
aligned_self = cast ("Self" , aligned_vars [0 ])
@@ -8395,25 +8389,24 @@ def _integrate_one(self, coord, datetime_unit=None, cumulative=False):
8395
8389
if dim not in v .dims or cumulative :
8396
8390
variables [k ] = v
8397
8391
coord_names .add (k )
8398
- else :
8399
- if k in self .data_vars and dim in v .dims :
8400
- coord_data = to_like_array (coord_var .data , like = v .data )
8401
- if _contains_datetime_like_objects (v ):
8402
- v = datetime_to_numeric (v , datetime_unit = datetime_unit )
8403
- if cumulative :
8404
- integ = duck_array_ops .cumulative_trapezoid (
8405
- v .data , coord_data , axis = v .get_axis_num (dim )
8406
- )
8407
- v_dims = v .dims
8408
- else :
8409
- integ = duck_array_ops .trapz (
8410
- v .data , coord_data , axis = v .get_axis_num (dim )
8411
- )
8412
- v_dims = list (v .dims )
8413
- v_dims .remove (dim )
8414
- variables [k ] = Variable (v_dims , integ )
8392
+ elif k in self .data_vars and dim in v .dims :
8393
+ coord_data = to_like_array (coord_var .data , like = v .data )
8394
+ if _contains_datetime_like_objects (v ):
8395
+ v = datetime_to_numeric (v , datetime_unit = datetime_unit )
8396
+ if cumulative :
8397
+ integ = duck_array_ops .cumulative_trapezoid (
8398
+ v .data , coord_data , axis = v .get_axis_num (dim )
8399
+ )
8400
+ v_dims = v .dims
8415
8401
else :
8416
- variables [k ] = v
8402
+ integ = duck_array_ops .trapz (
8403
+ v .data , coord_data , axis = v .get_axis_num (dim )
8404
+ )
8405
+ v_dims = list (v .dims )
8406
+ v_dims .remove (dim )
8407
+ variables [k ] = Variable (v_dims , integ )
8408
+ else :
8409
+ variables [k ] = v
8417
8410
indexes = {k : v for k , v in self ._indexes .items () if k in variables }
8418
8411
return self ._replace_with_new_dims (
8419
8412
variables , coord_names = coord_names , indexes = indexes
0 commit comments