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

Time:11-22

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?

CodePudding user response:

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] 
  • Related