-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: switch Dataset and DataArray to use explicit indexes (#2639)
* ENH: switch Dataset and DataArray to use explicit indexes This change switches Dataset.indexes and DataArray.indexes to be backed by explicit dictionaries of indexes, instead of being implicitly defined by the set of coordinates with names matching dimensions. There are no changes to the public interface yet: these will come later. For now, indexes are recreated from coordinates every time a new DataArray or Dataset is created. In follow-up PRs, I will refactor indexes to be propagated explicitly in xarray operations. This will facilitate future API changes, when indexes will no longer only be associated with dimensions. * Add xarray.core.indexes * Fixes per review
- Loading branch information
Showing
4 changed files
with
86 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,55 @@ | ||
from __future__ import absolute_import, division, print_function | ||
try: | ||
from collections.abc import Mapping | ||
except ImportError: | ||
from collections import Mapping | ||
from collections import OrderedDict | ||
|
||
from . import formatting | ||
|
||
|
||
class Indexes(Mapping, formatting.ReprMixin): | ||
"""Immutable proxy for Dataset or DataArrary indexes.""" | ||
def __init__(self, indexes): | ||
"""Not for public consumption. | ||
Parameters | ||
---------- | ||
indexes : Dict[Any, pandas.Index] | ||
Indexes held by this object. | ||
""" | ||
self._indexes = indexes | ||
|
||
def __iter__(self): | ||
return iter(self._indexes) | ||
|
||
def __len__(self): | ||
return len(self._indexes) | ||
|
||
def __contains__(self, key): | ||
return key in self._indexes | ||
|
||
def __getitem__(self, key): | ||
return self._indexes[key] | ||
|
||
def __unicode__(self): | ||
return formatting.indexes_repr(self) | ||
|
||
|
||
def default_indexes(coords, dims): | ||
"""Default indexes for a Dataset/DataArray. | ||
Parameters | ||
---------- | ||
coords : Mapping[Any, xarray.Variable] | ||
Coordinate variables from which to draw default indexes. | ||
dims : iterable | ||
Iterable of dimension names. | ||
Returns | ||
------- | ||
Mapping[Any, pandas.Index] mapping indexing keys (levels/dimension names) | ||
to indexes used for indexing along that dimension. | ||
""" | ||
return OrderedDict((key, coords[key].to_index()) | ||
for key in dims if key in coords) |