gpt4 book ai didi

numpy - 同步两个数组的向量化方法

转载 作者:行者123 更新时间:2023-12-02 00:13:08 29 4
gpt4 key购买 nike

我有两个 Pandas TimeSeries:xy,我想将其“截至”同步。我想为 x 中的每个元素找到 y 中位于它之前(按索引值)的最新(按索引)元素。例如,我想计算这个 new_x:

x       new_x
---- -----
13:01 13:00
14:02 14:00

y
----
13:00
13:01
13:30
14:00

我正在寻找矢量化解决方案,而不是 Python 循环。时间值基于 Numpy datetime64y 数组的长度约为数百万,因此 O(n^2) 解可能不切实际。

最佳答案

在某些圈子中,此操作称为“asof”连接。 Here is an implementation :

def diffCols(df1, df2):
""" Find columns in df1 not present in df2
Return df1.columns - df2.columns maintaining the order which the resulting
columns appears in df1.

Parameters:
----------
df1 : pandas dataframe object
df2 : pandas dataframe objct
Pandas already offers df1.columns - df2.columns, but unfortunately
the original order of the resulting columns is not maintained.
"""
return [i for i in df1.columns if i not in df2.columns]


def aj(df1, df2, overwriteColumns=True, inplace=False):
""" KDB+ like asof join.
Finds prevailing values of df2 asof df1's index. The resulting dataframe
will have same number of rows as df1.

Parameters
----------
df1 : Pandas dataframe
df2 : Pandas dataframe
overwriteColumns : boolean, default True
The columns of df2 will overwrite the columns of df1 if they have the same
name unless overwriteColumns is set to False. In that case, this function
will only join columns of df2 which are not present in df1.
inplace : boolean, default False.
If True, adds columns of df2 to df1. Otherwise, create a new dataframe with
columns of both df1 and df2.

*Assumes both df1 and df2 have datetime64 index. """
joiner = lambda x : x.asof(df1.index)
if not overwriteColumns:
# Get columns of df2 not present in df1
cols = diffCols(df2, df1)
if len(cols) > 0:
df2 = df2.ix[:,cols]
result = df2.apply(joiner)
if inplace:
for i in result.columns:
df1[i] = result[i]
return df1
else:
return result

在内部,这使用 pandas.Series.asof() .

关于numpy - 同步两个数组的向量化方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497777/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com