Search code examples
pandasseriescalculation

Optimize calculations involving a Pandas series


I'm trying to do some calculations involving a pandas series as shown below. Basically first I extracted t from a DataFrame column and then used a for loop with "if...else..." to do further calculation, because I found out that when I used max(f_min, nan), f_min was always returned. The code below worked, but it looks rather cumbersome. Is there a better way to do what I wanted to do here? Thank you so much for your help!

f_min = 0.1
t_min=0.  #degree C    
t_max=35.
t_opt=21.

t=pd.Series([nan, nan, nan, 37., 31., 23.], 
            index=['08/22/2011 07','08/22/2011 08','08/22/2011 09', 
                  '08/22/2011 10','08/22/2011 11','08/22/2011 12'],  
             name='T')    
#    t=df.T
a = (t - t_min)/(t_opt - t_min)
bt = (t_max - t_opt)/(t_opt - t_min)
b = ((t_max - t)/(t_max - t_opt))**bt
d = a * b

i= 0
for x in d:
    if (pd.isna(x)):
        d.iloc[i] = np.nan
    else:
        f_temp = max (f_min, x)
        d.iloc[i] = f_temp
    i = i+1

Solution

  • Let's use either:

    d.clip(f_min,) 
    

    or

    d.loc[d<f_min] = f_min