title: pandas interpolation : use np.interp with changing values
body: <p>I have a 3 million rows dataframe that contains the different values :</p>
  
  <pre><code>d    a0    a1    a2
  0.5    10.0    5.0    1.0
  0.8    10.0    2.0    0.0
  </code></pre>
  
  <p>I want to fill a fourth column with a linear interpolation of (a0,a1,a2) that takes the value in the "d" case,</p>
  
  <pre><code>d    a0    a1    a2    newcol
  1.5    10.0    5.0    1.0    3.0
  0.8    10.0    2.0    0.0    3.6
  </code></pre>
  
  <p>newcol is the weighted average between a[int(d)] and a[int(d+1)], e.g. when d = 0.8,  newcol = 0.2 * a0 + 0.8 * a1    because 0.8 is 80% of the way between 0 and 1</p>
  
  <p>I found that np.interp can be used, but there is no way for me to put the three column names in variable) : </p>
  
  <pre><code>df["newcol"]=np.interp(df["d"],[0,1,2], [100,200,300])
  </code></pre>
  
  <p>will indeed give me </p>
  
  <pre><code>d    a0    a1    a2    newcol
  1.5    10.0    5.0    1.0    250.0
  0.8    10.0    2.0    0.0    180.0
  </code></pre>
  
  <p>BUT I have no way to specify that the values vector changes :</p>
  
  <pre><code>df["newcol"]=np.interp(df["d"],[0,1,2], df[["a0","a1","a2"]])
  </code></pre>
  
  <p>gives me the following traceback :</p>
  
  <blockquote>
    <p>File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 1271, in interp
    return compiled_interp(x, xp, fp, left, right)
    ValueError: object too deep for desired array</p>
  </blockquote>
  
  <p>Is there any way to use a different vector for values at each line?  Could you think of any workaround ? </p>
  
  <p>Basically, I could find no way to create this new column based on the definition :</p>
  
  <blockquote>
    <p>What is the value in x = column "d" of the function that is piecewise linear 
    between given points and whose values at these points are described in the columns "ai"</p>
  </blockquote>
  
  <p>Edit:  Before, I used scipy.interp1d, which is not memory efficient, the comment helped me to solve partially my problem</p>
  
  <p>Edit2 : </p>
  
  <p>I tried the approach from ev-br that stated that I had to try to code the loop myself.</p>
  
  <pre><code>for i in range(len(tps)):
      columns=["a1","a2","a3"]
      length=len(columns)
      x=np.maximum(0,np.minimum(df.ix[i,"d"],len-2))
      xint = np.int(x)
      xfrac = x-xint
      name1=columns[xint]
      name2=columns[xint+1]
      tps.ix[i,"Multiplier"]=df.ix[i,name1]+xfrac*(df.ix[i,name2]-tps.ix[i,name1])  
  </code></pre>
  
  <p>The above loop loops around 50 times a second, so I guess I have a major optimisation issue. What part of working on a DataFrame do I do wrong?  </p>