谢谢大家的贡献。在查看了答案并进行了更多思考之后,我发现了一个更通用的解决方案,可以处理所有可能的情况。在这种情况下,我想将 dummy_arrayA 上采样到与 dummy_arrayB 相同的索引。我所做的是创建一个同时具有 A 和 B 的新索引。然后我使用 reindex 和 interpolate 函数来计算新值,最后我删除旧索引,以便获得相同的数组大小作为 dummy_array-B。
import pandas as pd
import numpy as np
# Create Dummy arrays
dummy_arrayA = pd.Series(np.array(range(0, 4)), index=[0,0.5,1.0,1.5])
dummy_arrayB = pd.Series(np.array(range(0, 5)), index=[0,0.4,0.8,1.2,1.6])
# Create new index based on array A
new_ind = pd.Index(dummy_arrayA.index)
# merge index A and B
new_ind=new_ind.union(dummy_arrayB.index)
# Use the reindex function. This will copy all the values and add the missing ones with nan. Then we call the interpolate function with the index method. So that it's interpolates based on the time.
df2 = dummy_arrayA.reindex(index=new_ind).interpolate(method="index")
# Delete the points.
New_ind_inter = dummy_arrayA.index.intersection(new_ind)
# We need to prevent that common point are also deleted.
new_ind = new_ind.difference(New_ind_inter)
# Delete the old points. So that the final array matchs dummy_arrayB
df2 = df2.drop(new_ind)
print(df2)