-
Notifications
You must be signed in to change notification settings - Fork 14
implement the PintIndex
#163
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 11 commits
b9baa9c
89c5e2a
17e9aec
30a1d80
34caf09
05aa5a6
2e0e5bd
818db6c
a049d03
0706bc6
a603860
dea881c
9427b20
a54b94a
23d1f76
f0d0890
fa9f1b3
fb01e32
9278a2d
3200bc8
281f03c
c5e9022
2c2c814
e5d8369
50a7287
2b3c5bb
57ea8e5
3eed8c9
aebaf37
58c540f
9cb7e91
9822520
55ccb00
6bd6726
c7d523b
bae3c3e
9dde67d
235ca0e
b927436
c31e6b0
415d059
1939b2d
2538104
eb2c405
0d46b66
caf4668
7303960
e88738d
0b400d0
5bd3ec7
77eef6d
c38eb5a
7277eb5
c7cf340
948d20f
8c76cbc
f9cb15c
49942bf
20dd15c
5efb318
f53539a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from xarray import Variable | ||
from xarray.core.indexes import Index, PandasIndex | ||
|
||
from . import conversion | ||
|
||
|
||
class PintMetaIndex(Index): | ||
# TODO: inherit from MetaIndex once that exists | ||
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. Hmm actually I'm not sure how a 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 agree, it doesn't really look like we actually need a base class for that, but I noticed that a few methods don't make sense for meta-indexes, |
||
def __init__(self, *, index, units): | ||
"""create a unit-aware MetaIndex | ||
|
||
Parameters | ||
---------- | ||
index : xarray.Index | ||
The wrapped index object. | ||
units : mapping of hashable to unit-like | ||
The units of the indexed coordinates | ||
""" | ||
self.index = index | ||
self.units = units | ||
|
||
def create_variables(self, variables=None): | ||
index_vars = self.index.create_variables(variables) | ||
|
||
index_vars_units = {} | ||
for name, var in index_vars.items(): | ||
data = conversion.array_attach_units(var.data, self.units[name]) | ||
var_units = Variable(var.dims, data, attrs=var.attrs, encoding=var.encoding) | ||
index_vars_units[name] = var_units | ||
|
||
return index_vars_units | ||
|
||
@classmethod | ||
def from_variables(cls, variables, options): | ||
index = PandasIndex.from_variables(variables) | ||
units_dict = {index.index.name: options.get("units")} | ||
return cls(index, units_dict) | ||
|
||
def sel(self, labels): | ||
converted_labels = conversion.convert_indexer_units(labels, self.units) | ||
stripped_labels = conversion.strip_indexer_units(converted_labels) | ||
|
||
return self.index.sel(stripped_labels) |
Uh oh!
There was an error while loading. Please reload this page.