将一个时间序列插入到自定义时间序列上 [英] Interpolate one time series onto custom time series

查看:48
本文介绍了将一个时间序列插入到自定义时间序列上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:将一个时间序列插入到另一个自定义时间序列上.

我检查了堆栈溢出并找到了以下

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

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.

解决方案

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

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

这篇关于将一个时间序列插入到自定义时间序列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆