Merge on Nan - 这个错误是我想要的行为。我应该担心未来的修正吗?

Merge on Nan - The bug is the behavior I want. Should I worry about future correction?

Pandaspd.Nan != pd.Nan中,但是现在,合并到dataframe,Nan会合并在一起。

如问题 , the normal behavior should be to not merge on that. The question is discussed on the Pandas issue tracker 中所述。

来自 It_is_chris:

# merge example
df = pd.DataFrame({'col1':[np.nan, 'match'], 'col2':[1,2]})
df2 = pd.DataFrame({'col1':[np.nan, 'no match'], 'col3':[3,4]})
pd.merge(df,df2, on='col1')

    col1    col2    col3
0   NaN      1       3

现在我们知道了,在我的代码中,我也需要对 Nan 进行合并。我可以使用 Pandas 中的故障,但在未来,行为会改变然后破坏我的代码吗?

防止这种情况的最佳选择是什么?

谢谢

正如您正确指出的那样,将来有可能无法加入 NaN。根据编程语言的不同,此行为会发生变化。

最简单的 future-proof 解决方案是将 NaN 替换为“NA”或类似的字符串。如果需要,您可以将其替换回 NaN post 合并。

df = pd.DataFrame({'col1':[np.nan, 'match'], 'col2':[1,2]}).fillna("NA")
df2 = pd.DataFrame({'col1':[np.nan, 'no match'], 'col3':[3,4]}).fillna("NA")
pd.merge(df,df2, on='col1')