用于验证pandas数据帧架构的包
opulent-pandas的Python项目详细描述
奢华的熊猫
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