gpt4 book ai didi

python - 在合并列中使用 pd.NA 合并两个数据帧会产生 'TypeError: boolean value of NA is ambiguous'

转载 作者:行者123 更新时间:2023-12-03 23:08:51 29 4
gpt4 key购买 nike

使用 Pandas 1.0.1,如果

df = df.merge(df2, on=some_column)

产量
File /home/torstein/code/fintechdb/Sheets/sheets/gild.py, line 42, in gild
df = df.merge(df2, on=some_column)
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py, line 7297, in merge
validate=validate,
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 88, in merge
return op.get_result()
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 643, in get_result
join_index, left_indexer, right_indexer = self._get_join_info()
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 862, in _get_join_info
(left_indexer, right_indexer) = self._get_join_indexers()
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 841, in _get_join_indexers
self.left_join_keys, self.right_join_keys, sort=self.sort, how=self.how
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 1311, in _get_join_indexers
zipped = zip(*mapped)
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 1309, in <genexpr>
for n in range(len(left_keys))
File /home/torstein/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py, line 1918, in _factorize_keys
rlab = rizer.factorize(rk)
File pandas/_libs/hashtable.pyx, line 77, in pandas._libs.hashtable.Factorizer.factorize
File pandas/_libs/hashtable_class_helper.pxi, line 1817, in pandas._libs.hashtable.PyObjectHashTable.get_labels
File pandas/_libs/hashtable_class_helper.pxi, line 1732, in pandas._libs.hashtable.PyObjectHashTable._unique
File pandas/_libs/missing.pyx, line 360, in pandas._libs.missing.NAType.__bool__

TypeError: boolean value of NA is ambiguous

虽然这有效:
df[some_column].fillna(np.nan, inplace=True)
df2[some_column].fillna(np.nan, inplace=True)
df = df.merge(df2, on=some_column)
# Works

如果相反,我会
df[some_column].fillna(pd.NA, inplace=True)

然后错误返回。

最佳答案

这与pd.NA有关正在实现 pandas 1.0.0以及 Pandas 团队如何决定它应该在 bool 上下文中工作。此外,您考虑到它是一个实验性功能,因此除了实验之外,它不应用于任何其他用途:

Warning Experimental: the behaviour of pd.NA can still change without warning.



在 Pandas 文档的另一个链接中,它涵盖了 working with missing values , 是我相信可以找到您正在寻找的原因和答案的地方:

NA in a boolean context: Since the actual value of an NA is unknown, it is ambiguous to convert NA to a boolean value. The following raises an error: TypeError: boolean value of NA is ambiguous



此外,它还提供了一条宝贵的建议:

“这也意味着 pd.NA 不能用于将其评估为 bool 值的上下文中,例如 if condition: ... where condition 可能是 pd.NA。在这种情况下, isna() 可用于可以避免检查 pd.NA 或条件为 pd.NA,例如通过预先填充缺失值。”

关于python - 在合并列中使用 pd.NA 合并两个数据帧会产生 'TypeError: boolean value of NA is ambiguous',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60280466/

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