{ "cells": [ { "cell_type": "raw", "id": "33ce3e3e", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Simple Indexing\n", "===============\n", "\n", ":mod:`named_arrays` generalizes the :mod:`numpy` indexing model using :class:`dict`\\ s insted of :class:`tuple`\\ s.\n", "This means that indexing the array depends on the name of the axis instead of it's position." ] }, { "cell_type": "code", "execution_count": null, "id": "initial_id", "metadata": { "ExecuteTime": { "end_time": "2025-08-23T15:28:18.682370Z", "start_time": "2025-08-23T15:28:16.737734Z" } }, "outputs": [], "source": [ "import named_arrays as na" ] }, { "cell_type": "raw", "id": "eb91d331", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Say we have a given 2D array, with axis names `row` and `col`." ] }, { "cell_type": "code", "execution_count": null, "id": "4b2439ec", "metadata": {}, "outputs": [], "source": [ "a = na.arange(1, 7, \"a\").reshape(dict(row=2, col=3))\n", "a" ] }, { "cell_type": "raw", "id": "b3f15708", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If we want to select the top-left element of `a`, we need to use a :class:`dict` with the row and column set to zero.\n", "It would be nice if we could use keyword arguments to ``__getitem__`` directly, but this is not supported by Python.\n", "See `PEP 637 `_ for more details." ] }, { "cell_type": "code", "execution_count": null, "id": "025ad3a0", "metadata": {}, "outputs": [], "source": [ "a[dict(row=0, col=0)]" ] }, { "cell_type": "raw", "id": "2b30f23b", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If we just want to select the first row, use a :class:`dict` with only the row set to zero." ] }, { "cell_type": "code", "execution_count": null, "id": "fe039ae7", "metadata": {}, "outputs": [], "source": [ "a[dict(row=0)]" ] }, { "cell_type": "raw", "id": "6e6758b7", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "To select only the first two columns, we need to use a :class:`slice` object.\n", "Slicing notation is not supported." ] }, { "cell_type": "code", "execution_count": null, "id": "98dacfc7", "metadata": {}, "outputs": [], "source": [ "a[dict(col=slice(2))]" ] }, { "cell_type": "raw", "id": "9234b801", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If the axis given by the item key does not exist, it is ignored.\n", "This allows vector and function indexing to work more intuitively and efficiently." ] }, { "cell_type": "code", "execution_count": null, "id": "ecab6dda", "metadata": {}, "outputs": [], "source": [ "a[dict(foo=1)]" ] }, { "cell_type": "raw", "id": "5ad2df67", "metadata": {}, "source": [ "This document only describes simple indexing, but advanced indexing is also fully supported and will be discussed in later tutorials." ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }