Sasiwut Chaiyadecha
Sasiwut Chaiyadecha

Reputation: 193

How to use np.Vectorize() with Pandas function?

I have the function that operates in Pandas DataFrame format. It works with pandas.apply() but it does not work with np.Vectorize(). Find the function below:

def AMTTL(inputData, amortization = []):
    rate = inputData['EIR']
    payment = inputData['INSTALMENT']
    amount = inputData['OUTSTANDING']
    amortization = [amount]
    if amount - payment <= 0:
        return amortization
    else:
        while amount > 0:
            amount = BALTL(rate, payment, amount)
            if amount <= 0:
                continue
            amortization.append(amount)
    return amortization

The function receives inputData as Pandas DataFrame format. The EIR, INSTALMENT and OUTSTANDING are the columns name. This function works well with pandas.apply()

data.apply(AMTTL, axis = 1)

However, I have tried to use np.Vectorize(). it does not work with the code below:

vfunc = np.vectorize(AMTTL)
vfunc(data)

It got error like 'Timestamp' object is not subscriptable. So, I tried to drop other columns that not used but it still got the another error like invalid index to scalar variable.

I am not sure how to adjust pandas.apply() to np.Vectorize(). Any suggestion? Thank you in advance.

Upvotes: 0

Views: 1090

Answers (1)

s510
s510

Reputation: 2822

np.vectorize is nothing more than a map function that is applied to all the elements of the array - meaning you cannot differentiate between the columns with in the function. It has no idea of the column names like EIR or INSTALMENT. Therefore your current implementation for numpy will not work.

From the docs:

The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy.

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.

Based on your problem, you should try np.apply_along_axis instead, where you can refer different columns with their indexes.

Upvotes: 1

Related Questions