用于验证pandas数据帧架构的包

opulent-pandas的Python项目详细描述


Build StatusPyPI version

奢华的熊猫

pounlent pandas是一个模式验证包,专门用于验证pandas数据帧的模式。 它从voluptuous中获得了巨大的灵感,并试图尽可能接近这个包中定义的api。奢华的熊猫 与volupturic不同的是,它在很大程度上依赖Pandas来执行验证。这使得富裕的熊猫更快 比在更大的数据集上狂妄自大。但是,这确实意味着输入格式也是pandas数据帧,而不是dict(voluptuous就是这样) 本自述将很快添加一个性感和奢华熊猫的性能比较!

示例

在奢华的熊猫身上定义一个模式和你在纵欲中的定义非常相似。为了使相同点和不同点变得清晰,让我们浏览一下与在《华丽自述》中所做的相同的示例。

twitter的user search API接受 查询URL,例如:

$ curl 'https://api.twitter.com/1.1/users/search.json?q=python&per_page=20&page=1'

为了验证这一点,我们可以使用如下模式:

>>> fromopulent_pandasimportSchema,TypeValidator,Required>>> schema=Schema({... Required('q'):[TypeValidator(str)],... Required('per_page'):[TypeValidator(int)],... Required('page'):[TypeValidator(int)],... })

与voluptouch相比,您会注意到每个字段的验证器总是被指定为一个列表。除此之外, 这与使用volupturous定义模式非常相似

如果我们看看voluptuous自述中定义的更复杂的模式,我们会看到非常相似的模式:

>>> fromopulent_pandas.validatorimportRequired,RangeValidator,TypeValidator,ValueLengthValidator>>> schema=Schema({... Required('q'):[TypeValidator(str),ValueLengthValidator(min_length=1)],... Required('per_page'):[TypeValidator(int),RangeValidator(min=1,max=20)],... Required('page'):[TypeValidator(int),RangeValidator(min=0)],... })

丰盛熊猫和性感熊猫的一个区别是,丰盛熊猫有一个可以使用的validate函数。 为了验证给定的数据结构,而不是将数据直接作为参数传递到模式的“令人兴奋的”方法。

如果传入的数据不满足pounlent pandas模式中指定的要求,则会收到相应的错误消息。行走 通过自述文件中提供的示例:

有3个必需字段: TOdo:这个示例还应该告诉您缺少哪些列。好像是个虫子。

>>> fromopulent_pandasimportMissingColumnError>>> try:... schema.validate({})... raiseAssertionError('MissingColumnError not raised')... exceptMissingColumnErrorase:... exc=e>>> str(exc)=="Columns missing"True

q必须是字符串:

>>> fromopulent_pandasimportInvalidTypeError>>> try:... schema.validate(pd.DataFrame({'q':[123],'per_page':[10],'page':[1]})... raiseAssertionError('InvalidTypeError not raised')... exceptInvalidTypeErrorase:... exc=e>>> str(exc)=="Invalid data type found for column: q. Required: <class 'str'>"True

…并且长度必须至少为一个字符:

>>> fromopulent_pandasimportValueLengthError>>> try:... schema.validate(pd.DataFrame({'q':[''],'per_page':5,'page':12}))... raiseAssertionError('ValueLengthError not raised')... exceptValueLengthErrorase:... exc=e>>> str(exc)=="Value found with length smaller than enforced minimum length for column: q. Minimum Length: 1"True

“每页”是不大于20的正整数:

>>> fromopulent_pandasimportRangeError>>> try:... schema.validate(pd.DataFrame({'q':['#topic'],'per_page':[900],'page':[12]}))... raiseAssertionError('RangeError not raised')... exceptRangeErrorase:... exc=e>>> str(exc)=="Value found larger than enforced maximum for column: per_page. Required maximum: 20"True>>> try:... schema.validate(pd.DataFrame({'q':['#topic'],'per_page':[-10],'page':[12]}))... raiseAssertionError('RangeError not raised')... exceptRangeErrorase:... exc=e>>> str(exc)=="Value found larger than enforced minimum for column: per_page. Required minimum: 1"True

“page”是一个整数>;=0:

>>> try:... schema.validate(pd.DataFrame({'q':['#topic'],'per_page':['one']})... raiseAssertionError('InvalidTypeError not raised')... exceptInvalidTypeErrorase:... exc=e>>> str(exc)=="Invalid data type found for column: page. Required type: <class 'int'>"True

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
virtualbox无法从java移动共享文件夹中的文件   java如何连接Android 4.3.5(GA)的apache HttpClient库?   片段中的java Recyclerview未立即显示警报对话框结果   javac(n,r)计算器程序不工作   java使用BooleanQuery还是编写更多索引?   如何在java中设置y/n循环?   java不兼容的通用通配符捕获   java如何在安卓xml中编写数据绑定时的三元操作条件   java如何使用FileDialog?   java如何创建单元测试来检测是否有人使用错误的编码编辑了文件?   java如何从唯一的字符串生成唯一的int?   java gradletomcatplugin:log4j:WARN找不到记录器的附加程序   java我的动态编程解决方案(Kefa和第一步)在codeforces中有什么问题?   java每天更新两个数据库,使它们都包含相同的有效数据集   java如何检查给定的时间是否在时间限制之间   java在单个json POST上保存父级和子级   java如何获取Solr字段类型