Merge_asof行为

huangapple go评论56阅读模式
英文:

Merge_asof behavior

问题

I am trying to use merge_asof() but it has some issues with sorting. For example:

trades = trades.sort_values(['user_id', 'create_at'], ascending=True)
deposits = deposits.sort_values(['user_id', 'create_at'], ascending=True)
data = pd.merge_asof(trades, deposits, on='create_at', by='user_id', direction='backward')

The above will throw the error:

ValueError: left keys must be sorted

To solve it, I had to explicitly re-sort inside the merge_asof, i.e.

data = pd.merge_asof(trades.sort_values('create_at', ascending=True),
deposits.sort_values('create_at', ascending=True),
on='create_at',
by='user_id',
direction='backward')

Any idea as to why this happens?

英文:

I am trying to use merge_asof() but it has some issues with sorting. For example:

trades = trades.sort_values(['user_id', 'create_at'], ascending=True)
deposits = deposits.sort_values(['user_id', 'create_at'], ascending=True)
data = pd.merge_asof(trades, deposits, on='create_at', by='user_id', direction='backward')

The above will throw the error:
> ValueError: left keys must be sorted

To solve it, I had to explicitly re-sort inside the merge_asof, i.e.

data = pd.merge_asof(trades.sort_values('create_at', ascending=True), 
                     deposits.sort_values('create_at', ascending=True), 
                     on='create_at', 
                     by='user_id', 
                     direction='backward')

Any idea as to why this happens?

答案1

得分: 1

ValueError是合理的,因为在第一种方法中,你将两个DataFrame按两个键进行排序(即,你按by=['user_id', 'create_at']进行排序),然后尝试在单个键上合并(即,在on='create_at'上合并)。

pandas.merge_asof(*left,right,on = None,left_on = None,right_on = None,left_index = False,right_index = False,by = None,left_by = None,right_by = None,suffixes =(' _x ',' _y '),tolerance = None,allow_exact_matches = True,direction = 'backward' *)

按键距离执行合并。这类似于左连接,但我们匹配的是最近的键,而不是相等的键。两个DataFrame必须按键排序

来源:[文档]

英文:

The ValueError is legit because in the first approach, you're sorting your two DataFrames with two keys (i.e, you're sorting by=['user_id', 'create_at']), then trying to merge on a single one (i.e on='create_at').

> pandas.merge_asof(left, right, on=None,
> left_on=None, right_on=None, left_index=False, right_index=False,
> by=None, left_by=None, right_by=None, suffixes=('_x', '_y'),
> tolerance=None, allow_exact_matches=True,
> direction='backward'
)
>
> Perform a merge by key distance.
> This is similar to a left-join except that we match on nearest key rather than equal keys. Both DataFrames must be sorted by the key.
>
> Source : [docs]

huangapple
  • 本文由 发表于 2023年6月12日 18:28:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定