Support fancy indexing
fperez opened this issue · 2 comments
fperez commented
Currently fancy indexing isn't supported at all.
In [79]: cap_ax_spec = 'capitals', ['washington', 'london', 'berlin', 'paris', 'moscow'] ...: time_ax_spec = 'time', ['0015', '0615', '1215', '1815'] ...: t = DataArray(np.arange(4*5).reshape(4,5), [time_ax_spec, cap_ax_spec]) ...: t.axis.capitals[['paris', 'london']] ...: --------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) /home/fperez/research/code/datarray/datarray/ in () 2 time_ax_spec = 'time', ['0015', '0615', '1215', '1815'] 3 t = DataArray(np.arange(4*5).reshape(4,5), [time_ax_spec, cap_ax_spec]) ----> 4 t.axis.capitals[['paris', 'london']] /home/fperez/research/code/datarray/datarray/datarray.py in __getitem__(self, key) 218 # XXX We don't handle fancy indexing at the moment 219 if isinstance(key, (np.ndarray, list)): --> 220 raise NotImplementedError('We do not handle fancy indexing yet') 221 parent_arr = self.parent_arr # local for speed 222 parent_arr_ndim = parent_arr.ndim NotImplementedError: We do not handle fancy indexing yet
fperez commented
As a preliminary plan, we can try to:
- do the equivalent of
asarray(t)[fancy_indexing]
but return a DataArray instance, just that it has all its axes unnamed and unlabeled. - once the above is working, we can try to define for which cases the semantics of what axes/labels survive the indexing operation are, and implement those.
fperez commented
An example where you manually pick out elements, for example, will necessarily drop dimensions, and something like this can't really preserve any labels/axes, since the output's geometry has nothing to do with the input:
In [110]: a = arange(12).reshape(3,4) ...: b = a[[(1,2,1),(0,1,3)]] ...: print a ...: print b [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [4 9 7]