Search code examples
pythonpandasinterpolationlinear-interpolation

How to add interpolated values to pandas DataFrame?


I have the following pandas DataFrame called df:

timestamp   param_1     param_2
0.000       -0.027655   0.0
0.25        -0.034012   0.0
0.50        -0.040369   0.0
0.75        -0.046725   0.0
1.00        -0.050023   0.0
1.25        -0.011015   0.0
1.50        -0.041366   0.0
1.75        -0.056723   0.0
2.00        -0.013081   0.0

Now I need to add two new columns created from the following lists:

timestamp_new = [0.5, 1.0, 1.5, 2.0]
param_3 = [10.0, 25.0, 15.0, 22.0]

The problem is that timestamp_new has a different granularity. Thus, I need to interpolate (linearly) both timestamp_new and param_3 in order to fit the granularity of timestamp in df.

Expected result (please notice that I interpolated param_3 values randomly just to show the format of an expected result):

timestamp   param_1     param_2   param_3
0.000       -0.027655   0.0       8.0
0.25        -0.034012   0.0       9.0
0.50        -0.040369   0.0       10.0
0.75        -0.046725   0.0       20.0
1.00        -0.050023   0.0       25.0
1.25        -0.011015   0.0       18.0
1.50        -0.041366   0.0       15.0
1.75        -0.056723   0.0       17.0
2.00        -0.013081   0.0       22.0

Is there any way to do it?


Solution

  • Let's try reindex().interpolate:

    ref_df = pd.Series(param_3, index=timestamp_new)
    new_vals = (ref_df.reindex(df['timestamp'])
                      .interpolate('index')
                      .bfill()                            # fill the first few nans
                      .ffill()                            # fill the last few nans
               )
    
    df['param_3'] = df['timestamp'].map(new_vals)
    

    Output:

       timestamp   param_1  param_2  param_3
    0       0.00 -0.027655      0.0     10.0
    1       0.25 -0.034012      0.0     10.0
    2       0.50 -0.040369      0.0     10.0
    3       0.75 -0.046725      0.0     17.5
    4       1.00 -0.050023      0.0     25.0
    5       1.25 -0.011015      0.0     20.0
    6       1.50 -0.041366      0.0     15.0
    7       1.75 -0.056723      0.0     18.5
    8       2.00 -0.013081      0.0     22.0