@@ -4479,6 +4479,7 @@ def expand_dims(
4479
4479
self ,
4480
4480
dim : None | Hashable | Sequence [Hashable ] | Mapping [Any , Any ] = None ,
4481
4481
axis : None | int | Sequence [int ] = None ,
4482
+ create_1d_index : bool = True ,
4482
4483
** dim_kwargs : Any ,
4483
4484
) -> Self :
4484
4485
"""Return a new object with an additional axis (or axes) inserted at
@@ -4488,6 +4489,8 @@ def expand_dims(
4488
4489
If dim is already a scalar coordinate, it will be promoted to a 1D
4489
4490
coordinate consisting of a single value.
4490
4491
4492
+ The
4493
+
4491
4494
Parameters
4492
4495
----------
4493
4496
dim : hashable, sequence of hashable, mapping, or None
@@ -4503,6 +4506,8 @@ def expand_dims(
4503
4506
multiple axes are inserted. In this case, dim arguments should be
4504
4507
same length list. If axis=None is passed, all the axes will be
4505
4508
inserted to the start of the result array.
4509
+ create_1d_index : bool, default is True
4510
+ Whether to create new PandasIndex objects for new 1D coordinate variables.
4506
4511
**dim_kwargs : int or sequence or ndarray
4507
4512
The keywords are arbitrary dimensions being inserted and the values
4508
4513
are either the lengths of the new dims (if int is given), or their
@@ -4622,6 +4627,8 @@ def expand_dims(
4622
4627
# save the coordinates to the variables dict, and set the
4623
4628
# value within the dim dict to the length of the iterable
4624
4629
# for later use.
4630
+
4631
+ # TODO should we have an option to not create a variable here?
4625
4632
index = PandasIndex (v , k )
4626
4633
indexes [k ] = index
4627
4634
variables .update (index .create_variables ())
@@ -4660,11 +4667,16 @@ def expand_dims(
4660
4667
variables [k ] = v .set_dims (dict (all_dims ))
4661
4668
else :
4662
4669
if k not in variables :
4663
- # If dims includes a label of a non-dimension coordinate,
4664
- # it will be promoted to a 1D coordinate with a single value.
4665
- index , index_vars = create_default_index_implicit (v .set_dims (k ))
4666
- indexes [k ] = index
4667
- variables .update (index_vars )
4670
+ if create_1d_index :
4671
+ # If dims includes a label of a non-dimension coordinate,
4672
+ # it will be promoted to a 1D coordinate with a single value.
4673
+ index , index_vars = create_default_index_implicit (v .set_dims (k ))
4674
+ indexes [k ] = index
4675
+ variables .update (index_vars )
4676
+ else :
4677
+ # create 1D variable without creating a new index
4678
+ new_1d_var = v .set_dims (k )
4679
+ variables .update ({k : new_1d_var })
4668
4680
4669
4681
return self ._replace_with_new_dims (
4670
4682
variables , coord_names = coord_names , indexes = indexes
0 commit comments