lundi 25 février 2019

if statement: return NaN if number of data points is less than x else calculate annualized return for dataframe

I have a dataframe that contains monthly log returns of different funds. Dataframe consists of more than 100 columns and 500 rows. For dates for which return is not available np.nan value is reported. I am trying to calculate annualized return for the last 3 years for each fund and I am using the below function:

def calculate_3Y_return(data):

 data_cumsum = data.iloc[-36:].cumsum()
 return_3Y = data_cumsum.iloc[-1]*(1/3)

 return_3Y

For funds that do not have enough data points (their track record is shorter than 36 months) I would like my function to return NaN.

Can you help me with adding if statement to my function that will calculate 3 year annualized return for funds that have 36 data points available and return NaN for funds that have less than 36 data points (these funds were launched recently and therefore number of available data points is less than 36).

I already tried to used dataframe.count() but it didn’t help

Many thanks

Aucun commentaire:

Enregistrer un commentaire