Home > Blockchain >  Find the value of a float number in an array based on the float number of another array - Python
Find the value of a float number in an array based on the float number of another array - Python

Time:02-28

I have the following arrays, Time and Flux:

Time = np.array(lc2.time.value[transit_mask]) 
print('Time =',Time) 
Flux = np.array(lc2.sap_flux.value[transit_mask]) 
print('Flux =', Flux) 

and they give me the following arrays:

Time = [2420.69047176 2420.69186069 2420.69324961 2420.69463854 2420.69602747
 2420.69741639 2420.69880532 2420.70019425 2420.70158318 2420.7029721
 2420.70436103 2420.70574996 2420.70713888 2420.70852781 2420.70991674]
Flux = [3655.0168 3648.3088 3660.2622 3651.3274 3657.7986 3664.8052 3666.8796
 3665.003  3660.034  3654.9521 3651.9277 3652.042  3668.4712 3659.576
 3657.6863]

If I have the value 2420.70019425 from the array Time, how can I find the corresponding value in the array Flux ?

(I have tried many possible solutions but I also get the following index error message: IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices)

Thank you!

CodePudding user response:

idx = np.argwhere(Time == 2420.70019425)
print(Flux[idx[0]])

HOWEVER, if you do this, make sure that you really do have an element from Time, and not a literal value. You cannot reliably compare floating point values for equality. They're all just approximations.

Runnable example:

import numpy as np
Time = np.array([2420.69047176, 2420.69186069, 2420.69324961, 2420.69463854, 2420.69602747,
 2420.69741639, 2420.69880532, 2420.70019425, 2420.70158318, 2420.7029721,
 2420.70436103, 2420.70574996, 2420.70713888, 2420.70852781, 2420.70991674])
Flux = np.array([3655.0168, 3648.3088, 3660.2622, 3651.3274, 3657.7986, 3664.8052, 3666.8796,
 3665.003, 3660.034, 3654.9521, 3651.9277, 3652.042, 3668.4712, 3659.576,
 3657.6863])

val = Time[12]
idx = np.argwhere(Time==val)
print(idx)
print(Flux[idx[0]])
  • Related