0

I have 2 timeseries dataframes where xp are say the x coordinates of the data fp. I want to interpolate the values from xp/fp combinations per date for a fixed set of x values. So resulting output being a timeseries dataframe with same datetime index as the xp and fp and no of columns = no of elements in x

I have tried to use numpy.interp() but end up with ValueError: object too deep for desired array

import pandas as pd
import numpy as np

fp = pd.DataFrame(
    data=np.random.randint(0,100,size=(10, 4)),
    index=pd.date_range("20180101", periods=10),
    columns=list('ABCD'),
)

xp = pd.DataFrame(
    data=np.column_stack([
        list(range(1,11)),
        list(range(70,80)),
        list(range(150,160)),
        list(range(220,230))
    ]),
    index=pd.date_range("20180101", periods=10),
    columns=list('ABCD'),
)

x = [60, 120, 180]
x_interp = np.interp(x,xp,fp)

It seems like np.interp cant take dataframes as input? but it sounds like this is the fastest way for me to do it for a large dataset (of >3000 xp and fp rows)

Would appreciate any pointers.

UPDATE found a way of doing what I wanted as below

   x_interp = pd.DataFrame.from_records(fp.index.to_series().apply(lambda z: np.interp(x, xp.loc[(z)], fp.loc[(z)])).values, index = fp.index)
  • 2
    Whats your expected output ? – rafaelc Sep 04 '18 at 19:28
  • Perhaps just run a simple loop? `for col in fp.columns: np.interp(x, xp[col], fp[col]))` (Assuming the indices and column names are the same in both) – ALollz Sep 04 '18 at 19:29
  • @RafaelC expected output is a dataframe with 3 columns each column corresponding to interpolated fp value for x values (60, 120, 180) per date. So a time series as output with same datetimeindex – StackOverDose Sep 04 '18 at 20:00
  • I can't follow your question well enough to answer but maybe [this](https://stackoverflow.com/questions/43772218/fastest-way-to-use-numpy-interp-on-a-2-d-array/43775224#43775224) will help – Daniel F Sep 05 '18 at 06:41
  • Thanks for your help Daniel, added the solution basically what I was looking for. – StackOverDose Sep 05 '18 at 09:32

0 Answers0