Returns a new object with swapped dimensions. Parameters ---------- dims_dict : dict-like Dictionary whose keys are current dimension names and whose values are new names. **dims_kwargs : {existing_dim: new_dim, ...}, optional The
(
self, dims_dict: Mapping[Any, Hashable] | None = None, **dims_kwargs
)
| 4373 | return self._replace(variables, coord_names, dims=dims, indexes=indexes) |
| 4374 | |
| 4375 | def swap_dims( |
| 4376 | self, dims_dict: Mapping[Any, Hashable] | None = None, **dims_kwargs |
| 4377 | ) -> Self: |
| 4378 | """Returns a new object with swapped dimensions. |
| 4379 | |
| 4380 | Parameters |
| 4381 | ---------- |
| 4382 | dims_dict : dict-like |
| 4383 | Dictionary whose keys are current dimension names and whose values |
| 4384 | are new names. |
| 4385 | **dims_kwargs : {existing_dim: new_dim, ...}, optional |
| 4386 | The keyword arguments form of ``dims_dict``. |
| 4387 | One of dims_dict or dims_kwargs must be provided. |
| 4388 | |
| 4389 | Returns |
| 4390 | ------- |
| 4391 | swapped : Dataset |
| 4392 | Dataset with swapped dimensions. |
| 4393 | |
| 4394 | Examples |
| 4395 | -------- |
| 4396 | >>> ds = xr.Dataset( |
| 4397 | ... data_vars={"a": ("x", [5, 7]), "b": ("x", [0.1, 2.4])}, |
| 4398 | ... coords={"x": ["a", "b"], "y": ("x", [0, 1])}, |
| 4399 | ... ) |
| 4400 | >>> ds |
| 4401 | <xarray.Dataset> Size: 56B |
| 4402 | Dimensions: (x: 2) |
| 4403 | Coordinates: |
| 4404 | * x (x) <U1 8B 'a' 'b' |
| 4405 | y (x) int64 16B 0 1 |
| 4406 | Data variables: |
| 4407 | a (x) int64 16B 5 7 |
| 4408 | b (x) float64 16B 0.1 2.4 |
| 4409 | |
| 4410 | >>> ds.swap_dims({"x": "y"}) |
| 4411 | <xarray.Dataset> Size: 56B |
| 4412 | Dimensions: (y: 2) |
| 4413 | Coordinates: |
| 4414 | * y (y) int64 16B 0 1 |
| 4415 | x (y) <U1 8B 'a' 'b' |
| 4416 | Data variables: |
| 4417 | a (y) int64 16B 5 7 |
| 4418 | b (y) float64 16B 0.1 2.4 |
| 4419 | |
| 4420 | >>> ds.swap_dims({"x": "z"}) |
| 4421 | <xarray.Dataset> Size: 56B |
| 4422 | Dimensions: (z: 2) |
| 4423 | Coordinates: |
| 4424 | x (z) <U1 8B 'a' 'b' |
| 4425 | y (z) int64 16B 0 1 |
| 4426 | Dimensions without coordinates: z |
| 4427 | Data variables: |
| 4428 | a (z) int64 16B 5 7 |
| 4429 | b (z) float64 16B 0.1 2.4 |
| 4430 | |
| 4431 | See Also |
| 4432 | -------- |