Target
I'm dealing with 2D time-series data, and never use negative indexing. So I want to subclass np.ndarray that negative and out-of-bound indexing in axis 0 will return a nan-augmented matrix with suitable shape. For example,
>>> test
>>> array([[0, 1, 2],
[3, 4, 5]])
Target:
>>> test[-1:1] # np.array([0, 1, 2])
>>> array(
[[nan, nan, nan],
[0, 1, 2]])
>>> test[1:4] # np.array([3, 4, 5])
>>> array([
[3, 4, 5],
[nan, nan, nan],
[nan, nan, nan]])
An ideal solution is to overwrite __getitem__, test whether the first slice contains negative or out-of-bound indexing, alter it, pass in super().__getitem__, and concatenate output with appropriate nan array.
Here is an example class to test what is passed in when we index a ndarray. However, the output is wierd
class NoneNeg(np.ndarray):
def __getitem__(self, index):
print(key)
return super(NoneNeg, self).__getitem__(index)
>>> test.view(NoneNeg)
>>>(-2, -3)
(-2, -2)
(-2, -1)
(-1, -3)
(-1, -2)
(-1, -1)
NoneNeg([[0, 1, 2],
[3, 4, 5]])
Question
- What will be passed in when indexing ndarray? For example, test[:, 2] and test[2]. A guess could be (slice(None), 2) and (2,).
- Why there are some negative indexing tuple output before a viewed object? It seems I can't alter negative indexing since it happens everywhere.