Is there anyway to create a numpy array that returns np.nan when indexed out of bounds? Eg
x = np.array([1,2,3])
x[1] # 2
x[-2] # np.nan
x[5] # np.nan
The closest thing I found was np.pad.
I know I could write a wrapper class, but I was wondering if there's any efficient numpy way to do it.
x[-2]is valid right? Numpy supports negative indices. Would you like to override this feature?try except__getitem__you can then also check if the index is out of bounds manually or with a try/except. I don't think you can monkeypatch anp.ndarray.