Python 在未编制索引的坐标上编制索引,且尺寸与编制索引的坐标相对应?

Python 在未编制索引的坐标上编制索引,且尺寸与编制索引的坐标相对应?,python,python-xarray,Python,Python Xarray,假设我有几个尺寸相同的坐标,如下面的示例: In [46]: ds = xarray.Dataset({"x": (("a", "b"), arange(25).reshape(5,5)+100), "y": ("b", arange(5)-100)}, {"a": arange(5), "b": arange(5)*2, "c": (("a",), list("ABCDE"))}) In [47]: print(ds) <xarray.Dataset> Dimensions:

假设我有几个尺寸相同的坐标,如下面的示例:

In [46]: ds = xarray.Dataset({"x": (("a", "b"), arange(25).reshape(5,5)+100), "y": ("b", arange(5)-100)}, {"a": arange(5), "b": arange(5)*2, "c": (("a",), list("ABCDE"))})

In [47]: print(ds)
<xarray.Dataset>
Dimensions:  (a: 5, b: 5)
Coordinates:
  * b        (b) int64 0 2 4 6 8
    c        (a) <U1 'A' 'B' 'C' 'D' 'E'
  * a        (a) int64 0 1 2 3 4
Data variables:
    x        (a, b) int64 100 101 102 103 104 105 106 107 108 109 110 111 ...
    y        (b) int64 -100 -99 -98 -97 -96
但是我不能使用坐标
c

In [49]: print(ds.loc[dict(c=slice("A", "C"))])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-49-33483295fec4> in <module>()
----> 1 print(ds.loc[dict(c=slice("A", "C"))])

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/dataset.py in __getitem__(self, key)
    292         if not utils.is_dict_like(key):
    293             raise TypeError('can only lookup dictionaries from Dataset.loc')
--> 294         return self.dataset.sel(**key)
    295 
    296 

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/dataset.py in sel(self, method, tolerance, drop, **indexers)
   1180         """
   1181         pos_indexers, new_indexes = indexing.remap_label_indexers(
-> 1182             self, indexers, method=method, tolerance=tolerance
   1183         )
   1184         result = self.isel(drop=drop, **pos_indexers)

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    273     new_indexes = {}
    274 
--> 275     dim_indexers = get_dim_indexers(data_obj, indexers)
    276     for dim, label in iteritems(dim_indexers):
    277         try:

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/indexing.py in get_dim_indexers(data_obj, indexers)
    241     if invalid:
    242         raise ValueError("dimensions or multi-index levels %r do not exist"
--> 243                          % invalid)
    244 
    245     level_indexers = defaultdict(dict)

ValueError: dimensions or multi-index levels ['c'] do not exist
或者使用
where
方法(尽管这对将
ds['y']
变大有副作用(?)

[57]中的
:ds.where((ds.c>='A')&(ds.c也可以:

[67]中的
:打印(ds.set_索引(a='c').loc[dict(a=slice('a','c')))
尺寸:(a:3,b:5)
协调:
*b(b)int64 0 2 4 6 8
*a(a)对象“a”“B”“C”
数据变量:
x(a,b)int64 100 101 102 103 104 105 106 107 108 109 110 111。。。
y(b)int64-100-99-98-97-96
这可能比问题中的两个备选方案略为简洁

In [49]: print(ds.loc[dict(c=slice("A", "C"))])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-49-33483295fec4> in <module>()
----> 1 print(ds.loc[dict(c=slice("A", "C"))])

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/dataset.py in __getitem__(self, key)
    292         if not utils.is_dict_like(key):
    293             raise TypeError('can only lookup dictionaries from Dataset.loc')
--> 294         return self.dataset.sel(**key)
    295 
    296 

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/dataset.py in sel(self, method, tolerance, drop, **indexers)
   1180         """
   1181         pos_indexers, new_indexes = indexing.remap_label_indexers(
-> 1182             self, indexers, method=method, tolerance=tolerance
   1183         )
   1184         result = self.isel(drop=drop, **pos_indexers)

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    273     new_indexes = {}
    274 
--> 275     dim_indexers = get_dim_indexers(data_obj, indexers)
    276     for dim, label in iteritems(dim_indexers):
    277         try:

/dev/shm/gerrit/venv/stable-3.5/lib/python3.5/site-packages/xarray/core/indexing.py in get_dim_indexers(data_obj, indexers)
    241     if invalid:
    242         raise ValueError("dimensions or multi-index levels %r do not exist"
--> 243                          % invalid)
    244 
    245     level_indexers = defaultdict(dict)

ValueError: dimensions or multi-index levels ['c'] do not exist
In [61]: ds.loc[dict(a=((ds.c>='A') & (ds.c<='C')))]
Out[61]: 
<xarray.Dataset>
Dimensions:  (a: 3, b: 5)
Coordinates:
  * b        (b) int64 0 2 4 6 8
    c        (a) <U1 'A' 'B' 'C'
  * a        (a) int64 0 1 2
Data variables:
    x        (a, b) int64 100 101 102 103 104 105 106 107 108 109 110 111 ...
    y        (b) int64 -100 -99 -98 -97 -96
In [57]: ds.where((ds.c>='A') & (ds.c<='C'), drop=True)
Out[57]: 
<xarray.Dataset>
Dimensions:  (a: 3, b: 5)
Coordinates:
  * b        (b) int64 0 2 4 6 8
    c        (a) <U1 'A' 'B' 'C'
  * a        (a) int64 0 1 2
Data variables:
    x        (a, b) float64 100.0 101.0 102.0 103.0 104.0 105.0 106.0 107.0 ...
    y        (b, a) float64 -100.0 -100.0 -100.0 -99.0 -99.0 -99.0 -98.0 ...
In [67]: print(ds.set_index(a='c').loc[dict(a=slice('A', 'C'))])
<xarray.Dataset>
Dimensions:  (a: 3, b: 5)
Coordinates:
  * b        (b) int64 0 2 4 6 8
  * a        (a) object 'A' 'B' 'C'
Data variables:
    x        (a, b) int64 100 101 102 103 104 105 106 107 108 109 110 111 ...
    y        (b) int64 -100 -99 -98 -97 -96