英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论