-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
``` | ||
|
||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
### 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() | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ caption: Built-in | |
hidden: | ||
--- | ||
builtin/range | ||
builtin/pandas | ||
builtin/pdinterval | ||
``` | ||
|
||
```{toctree} | ||
|
Uh oh!
There was an error while loading. Please reload this page.