Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions fundamentals/02.3_aligning_data_objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,36 @@
"For more, see the [Xarray documentation](https://docs.xarray.dev/en/stable/user-guide/computation.html#broadcasting-by-dimension-name) and the [numpy documentation](https://numpy.org/doc/stable/user/basics.broadcasting.html) on broadcasting.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dimension order\n",
"\n",
"Dimensions are matched by name even when they appear in a different order. Transposing `arr1` changes its shape from `(3, 4)` to `(4, 3)`, but each value still has the same `space` and `time` labels. Subtracting the transposed array therefore gives zero at every location, without reshaping either input."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"transposed = arr1.transpose(\"time\", \"space\")\n",
"print(arr1.dims, arr1.shape)\n",
"print(transposed.dims, transposed.shape)\n",
"difference = arr1 - transposed\n",
"xr.testing.assert_identical(difference, xr.zeros_like(arr1))\n",
"difference"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The values align by name, but the result still has an ordered set of dimensions: dimensions appear in the order they first occur in the inputs. Here `difference.dims` is `('space', 'time')`. Use `.transpose(...)` when you need a particular axis order, especially before passing `.data` to NumPy or another positional array API."
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down