find all elements > 0 in a np.array with np.where

Question:

I have a Array with Numbers ranging from (-infinite to +infinite)

Code looks like that:

delta_up = np.where(delta > 0, delta, 0)
delta_down = np.where(delta < 0, delta, 0)

Problem: I also have nan’s in the array and they need to stay as nan’s. But they are beeing converted to 0

How to solve it?

Asked By: SebastianHeeg

||

Answers:

my_array = np.array([1, 2, 3, 5, -1, -2, -3, None], dtype="float")


negative_idx = np.where(my_array<0) # np.nan values will be ignore
positive_idx = np.where(my_array>0) # np.nan values will be ignore

# getting subarray with values `array[indexes]`
negative_values = my_array[negative_idx]
positive_values = my_array[positive_idx] 
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.