{ "cells": [ { "cell_type": "markdown", "id": "6fad80b2-cb76-49a4-abbd-7ee148d8416d", "metadata": {}, "source": [ "# Xarray RangeIndex prototype\n", "\n", "Author: Benoît Bovy ([benbovy](https://github.com/benbovy)) - December 2023" ] }, { "cell_type": "code", "execution_count": 141, "id": "78210461-b73b-4499-8b5e-89e9c257f4ff", "metadata": {}, "outputs": [], "source": [ "from typing import Any, Hashable, Mapping, Self\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import xarray as xr\n", "from xarray.indexes import PandasIndex" ] }, { "cell_type": "code", "execution_count": 164, "id": "79f51030-1b4d-4e7d-922c-31ffc869cbc7", "metadata": {}, "outputs": [], "source": [ "class RangeIndex(PandasIndex):\n", " \"\"\"A range index.\n", "\n", " This Xarray index uses a `pandas.RangeIndex` internally.\n", " It can be set from either:\n", "\n", " - a coordinate encapsulating a `pandas.RangeIndex` instance\n", " - a scalar coordinate (with \"start\", \"stop\" and \"step\" attributes)\n", " - any arbitrary coordinate (validation is performed by default)\n", " \n", " \"\"\"\n", "\n", " def __init__(\n", " self,\n", " array: pd.RangeIndex,\n", " dim: Hashable,\n", " coord_dtype: Any = None,\n", " ):\n", " assert isinstance(array, pd.RangeIndex)\n", " super().__init__(array, dim, coord_dtype=coord_dtype)\n", "\n", " @classmethod\n", " def from_variables(\n", " cls,\n", " variables: Mapping[Any, xr.Variable],\n", " *,\n", " options: Mapping[str, Any],\n", " ) -> Self:\n", "\n", " if len(variables) != 1:\n", " raise ValueError(\n", " f\"RangeIndex only accepts one variable, found {len(variables)} variables\"\n", " )\n", "\n", " name, var = next(iter(variables.items()))\n", "\n", " # case of a scalar coordinate\n", " if var.ndim == 0:\n", " idx = pd.RangeIndex(\n", " start=var.attrs.get(\"start\"),\n", " stop=var.attrs.get(\"stop\"),\n", " step=var.attrs.get(\"step\"),\n", " )\n", " dim = options.get(\"dim\", name)\n", " return cls(idx, dim)\n", "\n", " if var.ndim != 1:\n", " raise ValueError(\n", " \"RangeIndex only accepts a 1-dimensional variable, \"\n", " f\"variable {name!r} has {var.ndim} dimensions\"\n", " )\n", " \n", " # fastpath (variable encapsulates a pd.RangeIndex)\n", " # TODO: calling var.to_index() may be expensive?\n", " if isinstance(var.to_index(), pd.RangeIndex):\n", " return super().from_variables(variables, options=options)\n", " \n", " # case of a 1-d arbitrary coordinate\n", " var_data = var.data\n", " start = var_data[0]\n", " step = var_data[1] - start\n", " stop = var_data[-1] + step\n", " if options.get(\"validate\", True):\n", " np.testing.assert_array_equal(var.data, np.arange(start, stop, step))\n", " idx = pd.RangeIndex(start=start, stop=stop, step=step)\n", " return cls(idx, var.dims[0])\n", "\n", " @property\n", " def start(self):\n", " return self.index.start\n", "\n", " @property\n", " def stop(self):\n", " return self.index.stop\n", "\n", " @property\n", " def step(self):\n", " return self.index.step\n", " \n", " def _repr_inline_(self, max_width=0):\n", " return f\"RangeIndex(start={self.start}, stop={self.stop}, step={self.step})\"\n" ] }, { "cell_type": "markdown", "id": "3332b4a6-dbfc-4380-abe9-66655c3a8593", "metadata": {}, "source": [ "## Case 1: set RangeIndex from a coordinate encapsulating a `pd.RangeIndex`" ] }, { "cell_type": "code", "execution_count": 165, "id": "93bba47e-79ea-43c9-8363-152763c89760", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 10000)\n",
       "Coordinates:\n",
       "    x        (x) int64 0 1 2 3 4 5 6 7 ... 9993 9994 9995 9996 9997 9998 9999\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 10000)\n", "Coordinates:\n", " x (x) int64 0 1 2 3 4 5 6 7 ... 9993 9994 9995 9996 9997 9998 9999\n", "Data variables:\n", " *empty*" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = xr.Dataset(coords={\"x\": pd.RangeIndex(0, 10_000, 1)}).drop_indexes(\"x\")\n", "ds" ] }, { "cell_type": "code", "execution_count": 166, "id": "4cdaf26d-010e-420f-a2c3-e8c2ff22e9c8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 10000)\n",
       "Coordinates:\n",
       "  * x        (x) int64 0 1 2 3 4 5 6 7 ... 9993 9994 9995 9996 9997 9998 9999\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 10000)\n", "Coordinates:\n", " * x (x) int64 0 1 2 3 4 5 6 7 ... 9993 9994 9995 9996 9997 9998 9999\n", "Data variables:\n", " *empty*" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = ds.set_xindex(\"x\", RangeIndex)\n", "ds" ] }, { "cell_type": "code", "execution_count": 167, "id": "d1d66b4b-47a7-4e6d-b2f6-4ba9492a8555", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Indexes:\n", " x RangeIndex(start=0, stop=10000, step=1)" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.xindexes" ] }, { "cell_type": "markdown", "id": "85acc1e6-1261-43ad-863a-78a043376520", "metadata": {}, "source": [ "Selection works just like for any `pandas.Index`. In the results the \"x\" coordinate also has a `RangeIndex`." ] }, { "cell_type": "code", "execution_count": 175, "id": "22a6c419-ce99-4ec5-ae6f-f09f551526b7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 9)\n",
       "Coordinates:\n",
       "  * x        (x) int64 2 3 4 5 6 7 8 9 10\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 9)\n", "Coordinates:\n", " * x (x) int64 2 3 4 5 6 7 8 9 10\n", "Data variables:\n", " *empty*" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds.sel(x=slice(2, 10))" ] }, { "cell_type": "markdown", "id": "b30be658-b920-436e-92b4-3a390fb60683", "metadata": {}, "source": [ "Alignment works too" ] }, { "cell_type": "code", "execution_count": 179, "id": "b5d45a19-dfe9-471b-921c-8f87a76cd548", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 3)\n",
       "Coordinates:\n",
       "  * x        (x) int64 4 5 6\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 3)\n", "Coordinates:\n", " * x (x) int64 4 5 6\n", "Data variables:\n", " *empty*" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1, a2 = xr.align(ds.sel(x=slice(0, 6)), ds.sel(x=slice(4, 12)))\n", "\n", "a1" ] }, { "cell_type": "code", "execution_count": 180, "id": "bc849521-4333-4a2e-b971-d4215f0f0e3d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 3)\n",
       "Coordinates:\n",
       "  * x        (x) int64 4 5 6\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 3)\n", "Coordinates:\n", " * x (x) int64 4 5 6\n", "Data variables:\n", " *empty*" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2" ] }, { "cell_type": "markdown", "id": "8be802ab-fb8d-4a3e-9270-1e3b3a86db0f", "metadata": {}, "source": [ "## Case 2: set RangeIndex from a scalar coordinate" ] }, { "cell_type": "code", "execution_count": 169, "id": "0beafde9-8ecf-46a9-9879-8b1bf0ed7cd7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  ()\n",
       "Coordinates:\n",
       "    x        int64 1\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: ()\n", "Coordinates:\n", " x int64 1\n", "Data variables:\n", " *empty*" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds2 = xr.Dataset(coords={\"x\": ((), 1, {\"start\": 0, \"stop\": 10_000, \"step\": 1})})\n", "ds2" ] }, { "cell_type": "code", "execution_count": 170, "id": "545c8a54-6379-4267-8029-8f6f5435ac60", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  ()\n",
       "Coordinates:\n",
       "  * x        (x) int64 0 1 2 3 4 5 6 7 ... 9993 9994 9995 9996 9997 9998 9999\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: ()\n", "Coordinates:\n", " * x (x) int64 0 1 2 3 4 5 6 7 ... 9993 9994 9995 9996 9997 9998 9999\n", "Data variables:\n", " *empty*" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds2 = ds2.set_xindex(\"x\", RangeIndex)\n", "ds2" ] }, { "cell_type": "code", "execution_count": 171, "id": "eb3c0d0c-b66f-4b2f-97ba-bf910695338b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Indexes:\n", " x RangeIndex(start=0, stop=10000, step=1)" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds2.xindexes" ] }, { "cell_type": "markdown", "id": "10ac7267-5636-4a24-9ba3-87dc3794c6ca", "metadata": {}, "source": [ "Example of converting back to a scalar coordinate for serialization:" ] }, { "cell_type": "code", "execution_count": 181, "id": "ae9d4f85-237b-4df6-87ab-03738e6b666e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  ()\n",
       "Coordinates:\n",
       "    x        int64 1\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: ()\n", "Coordinates:\n", " x int64 1\n", "Data variables:\n", " *empty*" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "idx = ds2.xindexes[\"x\"]\n", "\n", "ds2.assign_coords(x=((), 1, {\"start\": idx.start, \"stop\": idx.stop, \"step\": idx.step}))" ] }, { "cell_type": "markdown", "id": "01f56035-0c74-4101-940d-b70c468eb6e2", "metadata": {}, "source": [ "## Case 3: set RangeIndex from an arbitrary coordinate" ] }, { "cell_type": "code", "execution_count": 172, "id": "1b75aa93-a3e7-40ac-874f-29aaabe35778", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 4)\n",
       "Coordinates:\n",
       "    x        (x) int64 0 1 2 3\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 4)\n", "Coordinates:\n", " x (x) int64 0 1 2 3\n", "Data variables:\n", " *empty*" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds3 = xr.Dataset(coords={\"x\": [0, 1, 2, 3]}).drop_indexes(\"x\")\n", "ds3" ] }, { "cell_type": "code", "execution_count": 173, "id": "2d70af5b-c4ce-4c80-af16-9b5c715c15d3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (x: 4)\n",
       "Coordinates:\n",
       "  * x        (x) int64 0 1 2 3\n",
       "Data variables:\n",
       "    *empty*
" ], "text/plain": [ "\n", "Dimensions: (x: 4)\n", "Coordinates:\n", " * x (x) int64 0 1 2 3\n", "Data variables:\n", " *empty*" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds3 = ds3.set_xindex(\"x\", RangeIndex)\n", "ds3" ] }, { "cell_type": "code", "execution_count": 174, "id": "2c26ab0a-3315-4b9c-8d57-0ca4e8661dd0", "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "\nArrays are not equal\n\n(shapes (4,), (6,) mismatch)\n x: array([0, 1, 2, 5])\n y: array([0, 1, 2, 3, 4, 5])", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[174], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m ds4 \u001b[38;5;241m=\u001b[39m xr\u001b[38;5;241m.\u001b[39mDataset(coords\u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m5\u001b[39m]})\u001b[38;5;241m.\u001b[39mdrop_indexes(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mx\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[43mds4\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset_xindex\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mx\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mRangeIndex\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Git/github/benbovy/xarray/xarray/core/dataset.py:4940\u001b[0m, in \u001b[0;36mDataset.set_xindex\u001b[0;34m(self, coord_names, index_cls, **options)\u001b[0m\n\u001b[1;32m 4934\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 4935\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthose coordinates already have an index: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mindexed_coords\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 4936\u001b[0m )\n\u001b[1;32m 4938\u001b[0m coord_vars \u001b[38;5;241m=\u001b[39m {name: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_variables[name] \u001b[38;5;28;01mfor\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m coord_names}\n\u001b[0;32m-> 4940\u001b[0m index \u001b[38;5;241m=\u001b[39m \u001b[43mindex_cls\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_variables\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcoord_vars\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moptions\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4942\u001b[0m new_coord_vars \u001b[38;5;241m=\u001b[39m index\u001b[38;5;241m.\u001b[39mcreate_variables(coord_vars)\n\u001b[1;32m 4944\u001b[0m \u001b[38;5;66;03m# special case for setting a pandas multi-index from level coordinates\u001b[39;00m\n\u001b[1;32m 4945\u001b[0m \u001b[38;5;66;03m# TODO: remove it once we depreciate pandas multi-index dimension (tuple\u001b[39;00m\n\u001b[1;32m 4946\u001b[0m \u001b[38;5;66;03m# elements) coordinate\u001b[39;00m\n", "Cell \u001b[0;32mIn[164], line 64\u001b[0m, in \u001b[0;36mRangeIndex.from_variables\u001b[0;34m(cls, variables, options)\u001b[0m\n\u001b[1;32m 62\u001b[0m stop \u001b[38;5;241m=\u001b[39m var_data[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m] \u001b[38;5;241m+\u001b[39m step\n\u001b[1;32m 63\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m options\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalidate\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m---> 64\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtesting\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43massert_array_equal\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvar\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marange\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstart\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstep\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 65\u001b[0m idx \u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mRangeIndex(start\u001b[38;5;241m=\u001b[39mstart, stop\u001b[38;5;241m=\u001b[39mstop, step\u001b[38;5;241m=\u001b[39mstep)\n\u001b[1;32m 66\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mcls\u001b[39m(idx, var\u001b[38;5;241m.\u001b[39mdims[\u001b[38;5;241m0\u001b[39m])\n", " \u001b[0;31m[... skipping hidden 1 frame]\u001b[0m\n", "File \u001b[0;32m~/miniconda3/envs/xarray_dev/lib/python3.11/contextlib.py:81\u001b[0m, in \u001b[0;36mContextDecorator.__call__..inner\u001b[0;34m(*args, **kwds)\u001b[0m\n\u001b[1;32m 78\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(func)\n\u001b[1;32m 79\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21minner\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds):\n\u001b[1;32m 80\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_recreate_cm():\n\u001b[0;32m---> 81\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/miniconda3/envs/xarray_dev/lib/python3.11/site-packages/numpy/testing/_private/utils.py:778\u001b[0m, in \u001b[0;36massert_array_compare\u001b[0;34m(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf, strict)\u001b[0m\n\u001b[1;32m 772\u001b[0m reason \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m(dtypes \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mx\u001b[38;5;241m.\u001b[39mdtype\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00my\u001b[38;5;241m.\u001b[39mdtype\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m mismatch)\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 773\u001b[0m msg \u001b[38;5;241m=\u001b[39m build_err_msg([x, y],\n\u001b[1;32m 774\u001b[0m err_msg\n\u001b[1;32m 775\u001b[0m \u001b[38;5;241m+\u001b[39m reason,\n\u001b[1;32m 776\u001b[0m verbose\u001b[38;5;241m=\u001b[39mverbose, header\u001b[38;5;241m=\u001b[39mheader,\n\u001b[1;32m 777\u001b[0m names\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mx\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124my\u001b[39m\u001b[38;5;124m'\u001b[39m), precision\u001b[38;5;241m=\u001b[39mprecision)\n\u001b[0;32m--> 778\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mAssertionError\u001b[39;00m(msg)\n\u001b[1;32m 780\u001b[0m flagged \u001b[38;5;241m=\u001b[39m bool_(\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 781\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m isnumber(x) \u001b[38;5;129;01mand\u001b[39;00m isnumber(y):\n", "\u001b[0;31mAssertionError\u001b[0m: \nArrays are not equal\n\n(shapes (4,), (6,) mismatch)\n x: array([0, 1, 2, 5])\n y: array([0, 1, 2, 3, 4, 5])" ] } ], "source": [ "ds4 = xr.Dataset(coords={\"x\": [0, 1, 2, 5]}).drop_indexes(\"x\")\n", "ds4.set_xindex(\"x\", RangeIndex)" ] }, { "cell_type": "code", "execution_count": null, "id": "67f470ae-3ed3-4268-853b-c0cddeb2c819", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.0" } }, "nbformat": 4, "nbformat_minor": 5 }