I've a numpy array as follow:
a = np.array([[1, 2, 3, -999.],
[2, 3, 4, -999.],
[3, 4, 5, 6]])
How can I remove the value -999. while keeping the dimensions, as such:
array([[ 1., 2., 3.],
[ 2., 3., 4.],
[ 3., 4., 5., 6.]])
I tried:
np.delete(a, np.where(a == -999.))
But this result in
array([ 3., 2., 3., 4., -999., 3., 4., 5., 6.])
And I tried
a[a == -999.] = np.nan
a[~np.isnan(a)]
While it removes the nan (and so the -999), the numpy array becomes 1D:
array([1., 2., 3., 2., 3., 4., 3., 4., 5., 6.])
EDIT
I use the resulting jagged array (list of lists) for slicing another array where each slice can have a different length.
My use-case:
a = np.random.randint(1,35,size=(100000,5))
a[a == 14] = -999 # set a missing value
Option 1, select values non equal fill value
%%timeit
slices = np.array([i[i != -999] for i in a])
10 loops, best of 3: 183 ms per loop
Option 2, mask and compress
%%timeit
a_ma = np.ma.masked_equal(a, -999)
slices = np.array([i.compressed() for i in a_ma])
1 loop, best of 3: 2.99 s per loop