如何使用 pandas 积分计算高于功率值产生的能量?

how to calculate energy produced above power value using integral with pandas?

我有什么?

我有 df pd.DataFrame'power' 列和 'timestamp' 列。 我还有一个叫'x_power'

的幂值

我想要什么?

我试图找出在 'x_power' 以上和以下产生了多少能量(例如 x_power = 900), 为此我想做一个积分:

y轴-幂

x 轴 - 时间戳

粉红色区域 - 在 x_power

以上产生的能量

绿色区域-低于x_power

产生的能量
from scipy.interpolate import InterpolatedUnivariateSpline

x = df['timestamp'].to_numpy()
y = df['power'].to_numpy()
max = np.max(x)
min = np.min(x)

f = InterpolatedUnivariateSpline(x, y, k=1)  # k=1 gives linear interpolation

f.integral(min , max)

输出是图表下方的区域。

没有多重积分,有没有简单的方法计算上下'x_power'

要整合我们地块的面积 超过 x_power 你需要 以“新 0”这样的方式“下移”您的 y 值 处于x_power级别。

那么你应该将负值裁剪为零。

但是因为整个剧情你只有选择了个点, 第一步应该是生成 interpolated 版本 您的电力生产线,例如步长为 1,然后才 执行上述 2 个步骤。

执行此操作的代码是:

intStep = 1    # Interpolation step
# Interpolated x and y
xInt = np.arange(min, max + 1, intStep)
yInt = (np.interp(xInt, x, y) - x_power).clip(min=0)

要查看此行,您可以 运行:

fig, ax = plt.subplots()
ax.grid(True)
plt.plot(xInt, yInt)
plt.show()

并要集成此功能,运行 你的代码,但在上面的源数据上:

f = InterpolatedUnivariateSpline(xInt, yInt, k=1)
result = f.integral(min, max)