Custom Function help

I am trying to recreate this function from excel in Python. I got the code to work for a single Port ID. When i let the rest in, it calculates all the port IDs.

# Function to calculate the fvschedule from excel
def calculate_fvschedule(p_value, ACCT_RETURN):
    future_value = p_value
    for return in ACCT_RETURN:
            future_value *= (1 + rate)
    return future_value

Only four headers in my data set.
Added fake date for context:

PORT ID REPORT_DATE ACCT_RETURN BENCHMARK_RETURN
1 1/1/2021 1.23 1.23
1 1/1/2022 1.23 1.23
1 1/1/2023 1.23 1.23
2 1/1/2021 1.23 1.23
2 1/1/2022 1.23 1.23
2 1/1/2023 1.23 1.23

I want to do the same calculation for each port id. I assume I have to add an if statement to my for loop to look for the change but haven’t got it to work.

Thanks.

You’re not looking at the port ID in the code snippet. Can you share the calling code?

sure.

I added the port as an additional data frame to pass to the function but that is what I haven’t gotten to work.

df_y = 1
df_x = Ret_Aladdin_stacked_prepared_new_filtered_more_df['ACCT_RETURN']
df_p = Ret_Aladdin_stacked_prepared_new_filtered_more_df['PORT ID']

calc_df = calculate_fvschedule(df_y, df_x)

I would imagine that the problem lies in the filtering.

What I was looking for help was how to incorporate the port id part into the function.

The filtering works as expected. Hence being able to match the calculation to many when filtered to just that id after being passed to the function.

Something like…

def calculate_fvschedule(p_value, ACCT_RETURN, PORT_ID):
    future_value = p_value
    cur_port = port
    for return in ACCT_RETURN:
            if cur_port = cur_port:
                     future_value *= (1 + rate)
            else:
                     future_value = 1 --(reset calculation)
    return future_value

When i try this it says:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Can you share the whole stack trace?

Nothing exciting happening above. Just bring in package and data.


# -------------------------------------------------------------------------------- NOTEBOOK-CELL: CODE
# -*- coding: utf-8 -*-
import dataiku
import pandas as pd, numpy as np
from dataiku import pandasutils as pdu

# -------------------------------------------------------------------------------- NOTEBOOK-CELL: CODE
# Read recipe inputs
Ret_Aladdin_stacked_prepared_new_filtered_more = dataiku.Dataset("Ret_Aladdin_stacked_prepared_new_filtered_more")
Ret_Aladdin_stacked_prepared_new_filtered_more_df = Ret_Aladdin_stacked_prepared_new_filtered_more.get_dataframe()

That’s not the stack trace. The stack trace looks like:

>>> int('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'
  1. “if a = b” should produce a syntax error. What you want is “if a == b”.
  2. You are comparing cur_port to itself…why? Comparison of a variable with itself will always == True.
  3. “return” is a reserved name, that can’t be overwritten, and can’t be used as a temp variable in a for loop or anywhere as a variable, as far as I know.
  4. The ValueError is caused by a Series object being checked for boolean, which isn’t occuring in this code snippte, so can’t advise.

Even when i put == it doesnt work. I have rewritten it a few times so think that was just syntax.