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
(
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,
)
| 4480 | return self._replace_with_new_dims(variables, coord_names, indexes=indexes) |
| 4481 | |
| 4482 | def expand_dims( |
| 4483 | self, |
| 4484 | dim: Hashable | Sequence[Hashable] | Mapping[Any, Any] | None = None, |
| 4485 | axis: int | Sequence[int] | None = None, |
| 4486 | create_index_for_new_dim: bool = True, |
| 4487 | **dim_kwargs: Any, |
| 4488 | ) -> Self: |
| 4489 | """Return a new object with an additional axis (or axes) inserted at |
| 4490 | the corresponding position in the array shape. The new object is a |
| 4491 | view into the underlying array, not a copy. |
| 4492 | |
| 4493 | If dim is already a scalar coordinate, it will be promoted to a 1D |
| 4494 | coordinate consisting of a single value. |
| 4495 | |
| 4496 | The automatic creation of indexes to back new 1D coordinate variables |
| 4497 | controlled by the create_index_for_new_dim kwarg. |
| 4498 | |
| 4499 | Parameters |
| 4500 | ---------- |
| 4501 | dim : hashable, sequence of hashable, mapping, or None |
| 4502 | Dimensions to include on the new variable. If provided as hashable |
| 4503 | or sequence of hashable, then dimensions are inserted with length |
| 4504 | 1. If provided as a mapping, then the keys are the new dimensions |
| 4505 | and the values are either integers (giving the length of the new |
| 4506 | dimensions) or array-like (giving the coordinates of the new |
| 4507 | dimensions). |
| 4508 | axis : int, sequence of int, or None, default: None |
| 4509 | Axis position(s) where new axis is to be inserted (position(s) on |
| 4510 | the result array). If a sequence of integers is passed, |
| 4511 | multiple axes are inserted. In this case, dim arguments should be |
| 4512 | same length list. If axis=None is passed, all the axes will be |
| 4513 | inserted to the start of the result array. |
| 4514 | create_index_for_new_dim : bool, default: True |
| 4515 | Whether to create new ``PandasIndex`` objects when the object being expanded contains scalar variables with names in ``dim``. |
| 4516 | **dim_kwargs : int or sequence or ndarray |
| 4517 | The keywords are arbitrary dimensions being inserted and the values |
| 4518 | are either the lengths of the new dims (if int is given), or their |
| 4519 | coordinates. Note, this is an alternative to passing a dict to the |
| 4520 | dim kwarg and will only be used if dim is None. |
| 4521 | |
| 4522 | Returns |
| 4523 | ------- |
| 4524 | expanded : Dataset |
| 4525 | This object, but with additional dimension(s). |
| 4526 | |
| 4527 | Examples |
| 4528 | -------- |
| 4529 | >>> dataset = xr.Dataset({"temperature": ([], 25.0)}) |
| 4530 | >>> dataset |
| 4531 | <xarray.Dataset> Size: 8B |
| 4532 | Dimensions: () |
| 4533 | Data variables: |
| 4534 | temperature float64 8B 25.0 |
| 4535 | |
| 4536 | # Expand the dataset with a new dimension called "time" |
| 4537 | |
| 4538 | >>> dataset.expand_dims(dim="time") |
| 4539 | <xarray.Dataset> Size: 8B |