|
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + text_representation: |
| 4 | + format_name: myst |
| 5 | +kernelspec: |
| 6 | + display_name: Python 3 |
| 7 | + name: python |
| 8 | +--- |
| 9 | + |
| 10 | +# The default `PandasIndex` |
| 11 | + |
| 12 | +````{grid} |
| 13 | +```{grid-item} |
| 14 | +:columns: 3 |
| 15 | +```{image} https://pandas.pydata.org/docs/_static/pandas.svg |
| 16 | +--- |
| 17 | +alt: Pandas logo |
| 18 | +width: 200px |
| 19 | +align: center |
| 20 | +--- |
| 21 | +``` |
| 22 | +```` |
| 23 | + |
| 24 | +## Highlights |
| 25 | + |
| 26 | +1. {py:class}`xarray.indexes.PandasIndex` can wrap _one dimensional_ {py:class}`pandas.Index` objects to allow indexing along 1D coordinate variables. These indexes can apply to both {term}`"dimension" coordinates <xarray:Dimension coordinate>` and {term}`"non-dimension" coordinates <xarray:Non-dimension coordinate>`. |
| 27 | +1. When opening or constructing a new Dataset or DataArray, Xarray creates by default a {py:class}`xarray.indexes.PandasIndex` for each {term}`"dimension" coordinate <xarray:Dimension coordinate>`. |
| 28 | +1. It is possible to either drop those default indexes or skip their creation. |
| 29 | + |
| 30 | +## Example |
| 31 | + |
| 32 | +Let's open a tutorial dataset. |
| 33 | + |
| 34 | +```{code-cell} python |
| 35 | +import xarray as xr |
| 36 | +``` |
| 37 | + |
| 38 | +```{code-cell} python |
| 39 | +--- |
| 40 | +tags: [remove-cell] |
| 41 | +--- |
| 42 | +%xmode minimal |
| 43 | +
|
| 44 | +xr.set_options( |
| 45 | + display_expand_indexes=True, |
| 46 | + display_expand_attrs=False, |
| 47 | +); |
| 48 | +``` |
| 49 | + |
| 50 | +```{code-cell} python |
| 51 | +ds_air = xr.tutorial.open_dataset("air_temperature") |
| 52 | +ds_air |
| 53 | +``` |
| 54 | + |
| 55 | +It has created by default a {py:class}`~xarray.indexes.PandasIndex` for each of |
| 56 | +the "lat", "lon" and "time" dimension coordinates, as we can also see below via |
| 57 | +the {py:attr}`xarray.Dataset.xindexes` property. |
| 58 | + |
| 59 | +```{code-cell} python |
| 60 | +ds_air.xindexes |
| 61 | +``` |
| 62 | + |
| 63 | +Those indexes are used under the hood for, e.g., label-based selection. |
| 64 | + |
| 65 | +```{code-cell} python |
| 66 | +ds_air.sel(time="2013") |
| 67 | +``` |
| 68 | + |
| 69 | +### Set indexes for non-dimension coordinates |
| 70 | + |
| 71 | +Xarray does not automatically create an index for non-dimension coordinates like |
| 72 | +the "season (time)" coordinate added below. |
| 73 | + |
| 74 | +```{code-cell} python |
| 75 | +ds_air.coords["season"] = ds_air.time.dt.season |
| 76 | +ds_air |
| 77 | +``` |
| 78 | + |
| 79 | +Without an index, it is not possible select data based on the "season" |
| 80 | +coordinate. |
| 81 | + |
| 82 | +```{code-cell} python |
| 83 | +--- |
| 84 | +tags: [raises-exception] |
| 85 | +--- |
| 86 | +ds_air.sel(season="DJF") |
| 87 | +``` |
| 88 | + |
| 89 | +However, it is possible to manually set a `PandasIndex` for that 1-dimensional |
| 90 | +coordinate. |
| 91 | + |
| 92 | +```{code-cell} python |
| 93 | +ds_extra = ds_air.set_xindex("season", xr.indexes.PandasIndex) |
| 94 | +ds_extra |
| 95 | +``` |
| 96 | + |
| 97 | +Which now enables label-based selection. |
| 98 | + |
| 99 | +```{code-cell} python |
| 100 | +ds_extra.sel(season="DJF") |
| 101 | +``` |
| 102 | + |
| 103 | +It is not yet supported to provide labels to {py:meth}`xarray.Dataset.sel` for |
| 104 | +multiple index coordinates sharing common dimensions (unless those coordinates |
| 105 | +also share the same index object, e.g., like shown in the {doc}`PandasMultiIndex example <pdmultiindex>`). |
| 106 | + |
| 107 | +```{code-cell} python |
| 108 | +--- |
| 109 | +tags: [raises-exception] |
| 110 | +--- |
| 111 | +ds_extra.sel(season="DJF", time="2013") |
| 112 | +``` |
| 113 | + |
| 114 | +### Drop indexes |
| 115 | + |
| 116 | +Indexes are not always necessary and (re-)computing them may introduce some |
| 117 | +unwanted overhead. |
| 118 | + |
| 119 | +The code line below drops the default indexes that have been created when |
| 120 | +opening the example dataset. |
| 121 | + |
| 122 | +```{code-cell} python |
| 123 | +ds_air.drop_indexes(["time", "lat", "lon"]) |
| 124 | +``` |
| 125 | + |
| 126 | +### Skip the creation of default indexes |
| 127 | + |
| 128 | +Let's re-open the example dataset above, this time with no index. |
| 129 | + |
| 130 | +```{code-cell} python |
| 131 | +ds_air_no_index = xr.tutorial.open_dataset( |
| 132 | + "air_temperature", create_default_indexes=False |
| 133 | +) |
| 134 | +
|
| 135 | +ds_air_no_index |
| 136 | +``` |
| 137 | + |
| 138 | +Like {py:func}`xarray.open_dataset`, indexes are created by default for |
| 139 | +dimension coordinates when constructing a new Dataset. |
| 140 | + |
| 141 | +```{code-cell} python |
| 142 | +ds = xr.Dataset(coords={"x": [1, 2], "y": [3, 4, 5]}) |
| 143 | +
|
| 144 | +ds |
| 145 | +``` |
| 146 | + |
| 147 | +Also when assigning new coordinates. |
| 148 | + |
| 149 | +```{code-cell} python |
| 150 | +ds.assign_coords(u=[10, 20]) |
| 151 | +``` |
| 152 | + |
| 153 | +To skip the creation of those default indexes, we need to explicitly create a |
| 154 | +new {py:class}`xarray.Coordinates` object and pass `indexes={}` (empty |
| 155 | +dictionary). |
| 156 | + |
| 157 | +```{code-cell} python |
| 158 | +coords = xr.Coordinates({"u": [10, 20]}, indexes={}) |
| 159 | +
|
| 160 | +ds.assign_coords(coords) |
| 161 | +``` |
0 commit comments