【问题标题】:How to interpolate 100 values into the every two values?如何将 100 个值插入每两个值?
【发布时间】:2019-03-11 08:20:03
【问题描述】:

我想在每两个值(0.05秒,如下面的'data.csv')中插入100个值,并且应该保持以下数据不变。

 time         data
2013-02-12T02:58:00.047803  -1286
2013-02-12T02:58:00.097803  -1271
2013-02-12T02:58:00.147803  -1297
2013-02-12T02:58:00.197803  -1290
2013-02-12T02:58:00.247803  -1314
......

有些代码是:

import pandas as pd
from scipy import interpolate
import pylab as pl
import numpy as np

df = pd.read_csv('aa.txt', delim_whitespace=True)
xnew=np.linspace(df["time"],len(df["data"]),60)
f=interpolate.interp1d(df["time"],df["data"],kind="cubic")

ynew=f(xnew)
pl.plot(xnew,ynew,label="cubic", marker='.' )
pl.show()

【问题讨论】:

  • 我不明白你的问题。为什么要“数据保持不变”?你想在剧情上看到他​​们吗?最简单的方法是完全忘记格式正确的时间值,而是使用一些整数来指代时间
  • 原始数据无法更改。如:2013-02-12T02:58:00.047803 -1286 2013-02-12T02:58:00.097803 -1271

标签: python interpolation


【解决方案1】:

您的第一个问题是您需要正确格式化时间值,因为 pandas 将它们作为字符串加载,例如您可以将它们转换为秒。

那么,您以错误的方式使用np.linspace 函数。你要计算多少分?

下面是代码示例:

import pandas as pd
from scipy import interpolate
import pylab as pl
import numpy as np
import dateutil
import datetime

time2seconds = lambda x:(dateutil.parser.parse(x)-datetime.datetime(1970,1,1)).total_seconds()
df = pd.read_csv('aa.txt', delim_whitespace=True)
df["time"] = map(time2seconds, df["time"])
xnew = np.linspace(start=min(df["time"]), stop=max(df["time"]), num=len(df["time"])*4)
f = interpolate.interp1d(df["time"], df["data"], kind="cubic")
ynew = f(xnew)
pl.plot(xnew, ynew, label="cubic", marker='.' )
pl.show()

- 编辑- 对于您可能想要使用 pd.to_datetime 的时间戳

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多