The objective is to use apply to slice a range of array and apply NumPy argmax.
However, the compiler returns TypeError
TypeError: slice indices must be integers or None or have an __index__ method
For the following code
import pandas as pd import numpy as np
arr=np.array([10,2,5,3,6,8,3,3,2,5,6,8,11,14,11,100,1,3,20,21])
arr=arr.reshape((1,-1))
df=pd.DataFrame(zip([4,7],[15,18],[25,40]),columns=['lb','rb','mv'])
df['ss'] = df.apply(lambda x: np.argmax(arr[0][x['lb']:x['rb']] >= 0.3 * x['mv'] ), axis=1)
df['cc']=0.3 *df['mv']
df['es'] = df.apply(lambda x: np.argmax(arr[0][x['ss']:-1] < x['cc'] ), axis=1)
However, if I modify the two lines
df['cc']=0.3 *df['mv']
df['es'] = df.apply(lambda x: np.argmax(arr[0][x['ss']:-1] < x['cc'] ), axis=1)
into
df['es'] = df.apply(lambda x: np.argmax(arr[0][x['ss']:-1] < 0.3 *x['mv'] ), axis=1)
The program works like a charm.
I am curious why this issue arises.