Rohit
Rohit

Reputation: 6020

Interpolate one time series onto custom time series

Goal: Interpolate one time series onto another custom time series.

I checked stack overflow and found the following solution. However, I get the following error:

 ValueError: cannot reindex from a duplicate axis

Question: Do I need to use datetimeindex or the Unix timestamp will also work? Because the former will require me to convert to datetimeindex, then interpolate, and convert back to unixtimestamp. These are multiple steps, I would like to avoid.

Here's what I have:

The time series I would like to interpolate:

In [111]: p.head()
Out[111]:
       Timestamp  Pressure  Quality
0  1477294046400    101155        3
1  1477294046901    101152        3
2  1477294047421    101150        3
3  1477294047922    101151        3
4  1477294048425    101151        3

And the custom time series:

In [112]: a.head()
Out[112]:
            Time
0  1477294032458
1  1477294032463
2  1477294032468
3  1477294032473
4  1477294032478

Following the solution in the link above, I did the following:

pressure = pd.concat([p, a]).sort_index().interpolate().reindex(a.index)

but I get an error as shown above.

Upvotes: 2

Views: 811

Answers (1)

piRSquared
piRSquared

Reputation: 294258

You didn't provide enough information so I created my own. You will have to pay attention and adjust this to suit your needs.

This answer was given for this question.

setup

p = pd.DataFrame(
    dict(
        Pressure=[101155, 101152, 101150, 101151, 101151],
        Quality=[3, 3, 3, 3, 3]
    ),
    pd.Index([0, 10, 20, 30, 40], name='Timestamp')
)

a = [5, 12, 18, 24, 33, 35, 37]

general strategy

  • make sure timestamp is in index of p
  • take a union of p.index (your timestamp) and the new time list a
  • reindex with the union. NaN's will show up for 'new' index values.
  • when you interpolate, use method='index' DOCUMENTATION

code

idx = p.index.union(a)
p.reindex(idx).interpolate('index')

p

enter image description here

idx = p.index.union(a)
p.reindex(idx).interpolate('index')

enter image description here

Upvotes: 2

Related Questions