Skip to content

Add pandas interval index #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 0 additions & 5 deletions docs/builtin/pandas.md

This file was deleted.

100 changes: 100 additions & 0 deletions docs/builtin/pdinterval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
jupytext:
text_representation:
format_name: myst
kernelspec:
display_name: Python 3
name: python
---

# pandas: IntervalIndex

````{grid}
```{grid-item}
:columns: 3
```{image} https://pandas.pydata.org/docs/_static/pandas.svg
---
alt: Alt text
width: 200px
align: center
---
```
```{grid-item}
:columns: 9
```{seealso}
Learn more at the [Pandas](https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html#intervalindex) documentation.
```
````

# Highlights

1. Xarray's built-in support for pandas Index classes extends to more sophisticated classes like {py:class}`pandas.IntervalIndex`.
1. Xarray now generates such indexes automatically when using {py:meth}`xarray.DataArray.groupby_bins` or {py:meth}`xarray.Dataset.groupby_bins`.
1. Sadly {py:class}`pandas.IntervalIndex` supports numpy datetimes but not cftime.

```{important}
A pandas IntervalIndex models intervals using a single variable. The [Climate and Forecast Conventions](https://cfconventions.org/Data/cf-conventions/cf-conventions-1.11/cf-conventions.html#cell-boundaries), by contrast, model the intervals using two arrays: the intervals ("bounds" variable) and "central values".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the implications of this are. For example, does this mean you can't directly save to a CF-compliant netCDF?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, not until pydata/xarray#10483 or pydata/xarray#10445 goes in

```

## Example

### Assigning

```{code-cell}
%xmode minimal

import pandas as pd
import xarray as xr

xr.set_options(display_expand_indexes=True, display_expand_attrs=False)
pd.set_option('display.max_seq_items', 10)

orig = xr.tutorial.open_dataset("air_temperature")
orig
```

Let's replace the `time` vector with an IntervalIndex, assuming that the data represent averages over 6 hour periods centered at 00h, 06h, 12h, 18h

```{code-cell}
left = orig.time.data - pd.Timedelta("3h")
right = orig.time.data + pd.Timedelta("3h")
time_bounds = pd.IntervalIndex.from_arrays(left, right, closed="left")
time_bounds
```

```{code-cell}
indexed = orig.copy(deep=True)
indexed["time"] = time_bounds
indexed
```

### Indexing

Let's index out a representative value for 2013-05-01 02:00.

```{code-cell}
---
tags: [raises-exception]
---
orig.sel(time="2013-05-01 02:00")
```

Indexing the original dataset required specifying `method="nearest"`

```{code-cell}
orig.sel(time="2013-05-01 02:00", method="nearest").time
```

With an IntervalIndex, however, that is unnecessary

```{code-cell}
indexed.sel(time="2013-05-01 02:00").time
```

### Binned grouping

Xarray now creates IntervalIndex by default for binned grouping operations

```{code-cell}
orig.groupby_bins("lat", bins=[25, 35, 45, 55]).mean()
```
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ caption: Built-in
hidden:
---
builtin/range
builtin/pandas
builtin/pdinterval
```

```{toctree}
Expand Down