我有个密码
def read_file_and_transform(local_file_path):
""" """
try:
data_df = pd.read_csv(local_file_path)
data_df.columns = data_df.columns.str.replace(' ', '_')
clean_df = data_df.where((pd.notnull(data_df)), None)
except Exception as e:
logger.error("Failure in read file and transform method {}".format(e))
raise e
我正在为这三行编写单元测试用例,并使用第3行来面对错误。
下面是我的测试用例:
class MockPandas:
def __init__(self):
pass
def read_csv(self, *args, **kwargs):
""" """
return pd.DataFrame([{"a b": np.nan, "b": 2.33}])
def notnull(self, *args, **kwargs):
""" """
return pd.DataFrame([{"a_b": "None", "b": 2.33}])
@patch("path", MockPandas())
def test_read_file_and_transform(self):
""" """
result = self.obj.read_file_and_transform("/file_path")
assert result == [{"a": None, "b": 2.33}]
我面临的错误是:
ValueError: Boolean array expected for the condition, not object
这里有人能帮我吗?谢谢
发布于 2022-07-19 01:33:07
pandas.notnull
返回一个与原始数据帧大小相同的新数据帧,其中每个单元格都有一个布尔值,指示各自的值是否为nan。
因此,您应该更改模拟版本的notnull
的返回值,以匹配预期的返回值。
例如,如果原始df是:
A B C D
0 Sandy NaN 20.0 14.8
1 alex olivia 20.0 3.0
2 brook terica 7.0 NaN
3 kelly dan NaN 2.3
4 NaN amanda 8.0 6.0
那么df.notnull将是:
A B C D
0 True False True True
1 True True True True
2 True True True False
3 True True False True
4 False True True True
熊猫的非空文档可以找到这里。
https://stackoverflow.com/questions/72996760
复制相似问题