0

I have seen a similar question for a single query: Closest Value in Pandas Series

I need to do something similar, but for multiple queries.

I have a column of data which is like a time input, and then another column which is supposed to act like a function.

For Eg:

df = pd.concat([pd.Series(np.random.rand(51), name = 'f'), pd.Series(np.sort(np.random.choice(range(150), 51, replace=False)), name = 't')], axis = 1)

Which gives the first five rows like:

df.head()
        f       t
0   0.459344    0
1   0.675319    3
2   0.481433    8
3   0.373959    12
4   0.554812    14

Basically column f is like a function, it's value in the interval [0, 3) is 0.459344 and in the interval [3,8) it is 0.675319.

I am using a custom library for integration which will repeatedly query the function value at a given time, in order with a fixed step, say 0.05. I have to provide it with the value of the function at that time.

for eg: the first 5 queries will be 0, 0.05, 0.1, 0.15, 0.2, all of which should return 0.459344

How can i achieve this without repeated searches, in an optimal way?

EDIT: Clarification of Input and Output format.

I have to create a function that should take the time as input, and output the function value.

The input time can be a float it most definitely will be because the timesteps will be about 0.05.

For Eg:

Let us assume that i have created a function FunF, which does what i want.

It will be called by external library like this

FunF(0) - # This should return 0.459344
FunF(0.05) - # This should return 0.459344
FunF(0.1) - # This should return 0.459344
FunF(0.15) - # This should return 0.459344
 ...
 ...
 ...
FunF(2.95) - # This should return 0.459344
FunF(3) - # This should return 0.675319
FunF(3.05) - # This should return 0.675319

and so on.

I need to write the function FunF.

Vikash Balasubramanian
  • 2,921
  • 3
  • 33
  • 74

1 Answers1

1

I think you compare column with value x and get row with last True value - e.g. by iat or values:

def FunF(x):
    return df.loc[(df['t'] <= x), 'f'].iat[-1]

Alternative:

def FunF(x):
    return df.loc[df['t'] <= x, 'f'].values[-1]


print (FunF(0))
0.459344
print (FunF(0.1))
0.459344
print (FunF(2.95))
0.459344

print (FunF(3))
0.675319
print (FunF(3.1))
0.675319
print (FunF(8))
0.554812
print (FunF(9))
0.554812
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252