python - 测试无类型的熊猫数据框
问题描述
我pytest
用来测试我的代码。
我想执行的一个特定测试是关于在 pandas 中的列上运行的函数df
。
一个函数foo_fun
应用于pandas df,它会在对原始数据帧执行一些操作后添加一个新列。如果输入是这个函数None
应该返回None
。
例子:
def foo_fun(data):
# create a new column based on INFO column
txt=list(data.INFO)
data['NEW_COLUMN'] = ['value' if s is not None else None for s in txt]
return data
def foo_test():
fake_data = pd.DataFrame({'ENTITYKEY':['1','2','3','4'],
'INFO':['That\'s what she said',
'Questo è un testo in italiano',
'Eso es lo que dice el',
None]})
fake_data = foo_fun(fake_data)
assert(all(fake_data['NEW_COLUMN']==['value','value','value',None]))
我该如何测试呢?测试失败,问题是最后一个值,None
.
解决方案
使用 iloc 获取最后一列,然后检查。
if fake_data.iloc[:,-1] != None:
print("Last Column is not None")
推荐阅读
- mysql - laravel SQLSTATE [42S02]:找不到基表或视图:1146 表'name.system_functions'不存在
- php - 如何使用 php 获取联系表单?
- text - 在消息框中显示错误的位置
- c# - Unity 将文件移动到桌面
- sql - 旋转一列并保留 SUM、AVG、COUNT 列
- android - 我在 AdMob 控制台中的应用不再与 Google Play 关联?
- bash - 当它在环境变量中时如何使失败的命令替换退出shell
- django - Django:加入两个模型
- swift - 如何在组合中将错误类型从从不更改为错误?
- arrays - 从具有第二个数组元素的数组中获取数据