Return a new object with an additional axis (or axes) inserted at the corresponding position in the array shape. The new object is a view into the underlying array, not a copy. If dim is already a scalar coordinate, it will be promoted to a 1D coordinate consisting o
(
self,
dim: Hashable | Sequence[Hashable] | Mapping[Any, Any] | None = None,
axis: int | Sequence[int] | None = None,
create_index_for_new_dim: bool = True,
**dim_kwargs: Any,
)
| 2666 | return self._from_temp_dataset(ds) |
| 2667 | |
| 2668 | def expand_dims( |
| 2669 | self, |
| 2670 | dim: Hashable | Sequence[Hashable] | Mapping[Any, Any] | None = None, |
| 2671 | axis: int | Sequence[int] | None = None, |
| 2672 | create_index_for_new_dim: bool = True, |
| 2673 | **dim_kwargs: Any, |
| 2674 | ) -> Self: |
| 2675 | """Return a new object with an additional axis (or axes) inserted at |
| 2676 | the corresponding position in the array shape. The new object is a |
| 2677 | view into the underlying array, not a copy. |
| 2678 | |
| 2679 | If dim is already a scalar coordinate, it will be promoted to a 1D |
| 2680 | coordinate consisting of a single value. |
| 2681 | |
| 2682 | The automatic creation of indexes to back new 1D coordinate variables |
| 2683 | controlled by the create_index_for_new_dim kwarg. |
| 2684 | |
| 2685 | Parameters |
| 2686 | ---------- |
| 2687 | dim : Hashable, sequence of Hashable, dict, or None, optional |
| 2688 | Dimensions to include on the new variable. |
| 2689 | If provided as str or sequence of str, then dimensions are inserted |
| 2690 | with length 1. If provided as a dict, then the keys are the new |
| 2691 | dimensions and the values are either integers (giving the length of |
| 2692 | the new dimensions) or sequence/ndarray (giving the coordinates of |
| 2693 | the new dimensions). |
| 2694 | axis : int, sequence of int, or None, default: None |
| 2695 | Axis position(s) where new axis is to be inserted (position(s) on |
| 2696 | the result array). If a sequence of integers is passed, |
| 2697 | multiple axes are inserted. In this case, dim arguments should be |
| 2698 | same length list. If axis=None is passed, all the axes will be |
| 2699 | inserted to the start of the result array. |
| 2700 | create_index_for_new_dim : bool, default: True |
| 2701 | Whether to create new ``PandasIndex`` objects when the object being expanded contains scalar variables with names in ``dim``. |
| 2702 | **dim_kwargs : int or sequence or ndarray |
| 2703 | The keywords are arbitrary dimensions being inserted and the values |
| 2704 | are either the lengths of the new dims (if int is given), or their |
| 2705 | coordinates. Note, this is an alternative to passing a dict to the |
| 2706 | dim kwarg and will only be used if dim is None. |
| 2707 | |
| 2708 | Returns |
| 2709 | ------- |
| 2710 | expanded : DataArray |
| 2711 | This object, but with additional dimension(s). |
| 2712 | |
| 2713 | See Also |
| 2714 | -------- |
| 2715 | Dataset.expand_dims |
| 2716 | |
| 2717 | Examples |
| 2718 | -------- |
| 2719 | >>> da = xr.DataArray(np.arange(5), dims=("x")) |
| 2720 | >>> da |
| 2721 | <xarray.DataArray (x: 5)> Size: 40B |
| 2722 | array([0, 1, 2, 3, 4]) |
| 2723 | Dimensions without coordinates: x |
| 2724 | |
| 2725 | Add new dimension of length 2: |