<jupyter_start><jupyter_text>Indexing and Selecting Data Table of Contents1&nbsp;&nbsp;Indexing and Selecting Data1.1&nbsp;&nbsp;Learning Objectives1.2&nbsp;&nbsp;Label-based Indexing1.3&nbsp;&nbsp;NumPy Positional Indexing1.4&nbsp;&nbsp;Indexing with xarray1.5&nbsp;&nbsp;Vectorized Indexing1.6&nbsp;&nbsp;Going Further  Learning Objectives- Select data by position using .isel with values or slices- Select data by label using .sel with values or slices- Select timeseries data by date/time with values or slices- Use nearest-neighbor lookups with .sel  Label-based IndexingScientific data is inherently labeled. For example, time series data includes timestamps that label individual periods or points in time, spatial data has coordinates (e.g. longitude, latitude, elevation), and model or laboratory experiments are often identified by unique identifiers.<jupyter_code>import xarray as xr
ds = xr.open_dataset('../../../data/air_temperature.nc')
ds<jupyter_output><empty_output><jupyter_text>NumPy Positional IndexingWhen working with numpy, indexing is done by position (slices/ranges/scalars).<jupyter_code>t = ds['air'].data # numpy array 
t
t.shape
# extract a time-series for one spatial location
t[:, 20, 40]<jupyter_output><empty_output><jupyter_text>but wait, what labels go with 10 and 20? Was that lat/lon or lon/lat? Where are the timestamps that go along with this time-series?  Indexing with xarrayxarray offers extremely flexible indexing routines that combine the best features of NumPy and pandas for data selection.<jupyter_code>da = ds['air'] # Extract data array
da<jupyter_output><empty_output><jupyter_text>- **NumPy style indexing still works (but preserves the labels/metadata)**<jupyter_code>da[:, 20, 40]<jupyter_output><empty_output><jupyter_text>- **Positional indexing using dimension names**<jupyter_code>da.isel(lat=20, lon=40)<jupyter_output><empty_output><jupyter_text>- **Label-based indexing**<jupyter_code>da.sel(lat=50., lon=200.)<jupyter_output><empty_output><jupyter_text>- **Nearest Neighbor Lookups**<jupyter_code>da.sel(lat=52.25, lon=251.8998, method='nearest')<jupyter_output><empty_output><jupyter_text>- **All of these indexing methods work on the dataset too:**<jupyter_code>ds.sel(lat=52.25, lon=251.8998, method='nearest')<jupyter_output><empty_output><jupyter_text>Vectorized IndexingLike numpy and pandas, xarray supports indexing many array elements at once in a vectorized manner:<jupyter_code># generate a coordinates for a transect of points
lat_points = xr.DataArray([52, 52.5, 53], dims='points')
lon_points = xr.DataArray([250, 250, 250], dims='points')
lat_points
lon_points
# nearest neighbor selection along the transect
da.sel(lat=lat_points, lon=lon_points, method='nearest')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10602235845654526, 'fraction_numerical': 0.02596465921384782, 'mean_word_length': 7.33033033033033, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '451724', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 937, 'n_tokens_neox': 860, 'n_words': 256, 'path': 'notebooks/bytopic/xarray/03_indexing.ipynb', 'repo_name': 'jukent/ncar-python-tutorial', 'size': '2773'}