在 pandas 中将一个时间序列插值到另一个时间序列

编程入门 行业动态 更新时间:2024-10-28 12:22:17
本文介绍了在 pandas 中将一个时间序列插值到另一个时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一组定期测量的值.说:

I have one set of values measured at regular times. Say:

import pandas as pd import numpy as np rng = pd.date_range('2013-01-01', periods=12, freq='H') data = pd.Series(np.random.randn(len(rng)), index=rng)

例如,还有另一组更多的任意时间(实际上,这些时间不是常规序列)

And another set of more arbitrary times, for example, (in reality these times are not a regular sequence)

ts_rng = pd.date_range('2013-01-01 01:11:21', periods=7, freq='87Min') ts = pd.Series(index=ts_rng)

我想知道以ts为单位的时间插值的数据的值. 我可以在numpy中做到这一点:

I want to know the value of data interpolated at the times in ts. I can do this in numpy:

x = np.asarray(ts_rng,dtype=np.float64) xp = np.asarray(data.index,dtype=np.float64) fp = np.asarray(data) ts[:] = np.interp(x,xp,fp)

但是我感到熊猫在resample,reindex等位置中具有此功能,但是我不太了解.

But I feel pandas has this functionality somewhere in resample, reindex etc. but I can't quite get it.

推荐答案

您可以串联两个时间序列并按索引排序.由于第二个系列中的值是NaN,因此您可以interpolate并且只需选择代表第二个系列中的点的值即可:

You can concatenate the two time series and sort by index. Since the values in the second series are NaN you can interpolate and the just select out the values that represent the points from the second series:

pd.concat([data, ts]).sort_index().interpolate().reindex(ts.index)

pd.concat([data, ts]).sort_index().interpolate()[ts.index]

更多推荐

在 pandas 中将一个时间序列插值到另一个时间序列

本文发布于:2023-11-29 00:05:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   时间   中将   插值   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!