我将一个feather导入到pandas dataframe中,然后使用np.where()比较两个字符串列。然而,我得到了以下错误:TypeError: boolean value of NA is ambiguous
。MRE如下:
import pandas as pd
import numpy as np
d = {"col1": [np.NaN, "x"], "col2": [np.NaN, "x"]}
df = pd.DataFrame(data=d)
df.to_feather("test.feather")
df_f = pd.read_feather("test.feather")
df_f["col1"] = df_f["col1"].astype("string")
df_f["col2"] = df_f["col2"].astype("string")
df_f["is_equal"] = np.where(df_f["col1"] == df["col2"], 1, 0)
我必须手动将两列格式化为字符串,以便在导入时复制实际 Dataframe 的格式。
我已经阅读了这个错误,它与将列转换为字符串时创建的pd.NA
值有关。
我尝试将这些值转换为np.NaN
,如here:
df_f["col1"].replace({pd.NA: np.NaN}, inplace=True)
df_f["col2"].replace({pd.NA: np.NaN}, inplace=True)
但我得到了同样的错误。
我试着按照here将列转换为浮点数:
df_f["col1"] = df_f["col1"].astype("float")
df_f["col2"] = df_f["col2"].astype("float")
但我得到了ValueError: could not convert string to float
。
有没有人有任何建议,我可以如何解决这个问题?
2条答案
按热度按时间hmae6n7t1#
对于原始的
object
dtype,与NA
或None
值进行比较将强制结果为False。当您将列转换为
string
dtype时,与NA
或None
值进行比较不会将结果强制为False。对于
np.where
,它接受一个布尔值数组,因此它不理解NA
是什么,并抛出TypeError。对于字符串列,
object
dtype是标准类型,您不需要将其转换为字符串dtype。flvtvl502#
如果我只是创建一个普通的df(不使用feather),用
pd.NA
代替原始df中的np.NaN
值,np.where
可以正常工作。但是,如果我首先使用
astype("string")
更新每个列,将列的dtypes从object
更改为string
,则会出现错误。因此,我认为
astype("string")
是罪魁祸首,我将尝试消除调用此方法的赋值。