-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--- | ||
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](https://unidata.github.io/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". | ||
``` | ||
|
||
## 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
|
||
Note the above object still shows the `time` coordinates has associated `PandasIndex` but the values are now represented in and "IntervalArray" (as indicated by `interval[datetime64[ns], left]`) | ||
|
||
### 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() | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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