简体   繁体   English

获取“TypeError:无法识别输出参数的类型:<class 'str'> " 在 Pandera 装饰器中使用类函数时</class>

[英]Getting "TypeError: type of out argument not recognized: <class 'str'>" when using class function with Pandera decorator

I am trying to get to use decorators from Python package "Pandera" and I am having trouble to get them work with classes.我正在尝试使用 Python 包“Pandera”中的装饰器,但我很难让它们使用类。

First I create schemas for Pandera:首先,我为 Pandera 创建模式:

from pandera import Column, Check
import yaml
in_ = pa.DataFrameSchema(
    {
        "Name": Column(object, nullable=True),
        "Height": Column(object, nullable=True),
    })

with open("./in_.yml", "w") as file:
    yaml.dump(in_, file)

out_ = pa.DataFrameSchema(
    {
        "Name": Column(object, nullable=True),
        "Height": Column(object, nullable=True),
    })
with open("./out_.yml", "w") as file:
    yaml.dump(out_, file)

Next I create test.py file with class:接下来我用类创建test.py文件:

from pandera import check_io
import pandas as pd

class TransformClass():

    with open("./in_.yml", "r") as file:
        in_ = file.read()
    with open("./out_.yml", "r") as file:
        out_ = file.read()

    @staticmethod
    @check_io(df=in_, out=out_)
    def func(df: pd.DataFrame) -> pd.DataFrame:
        return df

Finally I importing this class:最后我导入了这个类:

from test import TransformClass
data = {'Name': [np.nan, 'Princi', 'Gaurav', 'Anuj'],
        'Height': [5.1, 6.2, 5.1, 5.2],
        'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
df = pd.DataFrame(data)
TransformClass.func(df)

I am getting:我正进入(状态:

File C:\Anaconda3\envs\py310\lib\site-packages\pandera\decorators.py:464, in check_io.<locals>._wrapper(fn, instance, args, kwargs)
    462     out_schemas = []
    463 else:
--> 464     raise TypeError(
    465         f"type of out argument not recognized: {type(out)}"
    466     )
    468 wrapped_fn = fn
    469 for input_getter, input_schema in inputs.items():
    470     # pylint: disable=no-value-for-parameter

TypeError: type of out argument not recognized: <class 'str'>

Any help would much appreciated任何帮助将不胜感激

The check_io decorator expects arguments of type pandera.DataFrameSchema . check_io装饰器需要pandera.DataFrameSchema类型的参数。 However, it is being passed _out which is type str since it is the output of file.read() .但是,它被传递给类型为 _out 的str因为它是_out file.read()的输出。

The Pandera docs explain which types the check_io decorator is expecting. Pandera 文档解释了check_io装饰器期望的类型。

A solution would be to pass the output of the file.read() line to the Pandera constructor, possibly with some transformation:一个解决方案是将file.read()行的输出传递给 Pandera 构造函数,可能需要进行一些转换:

out_ = yaml.safe_load(file.read())

Thanks to @grbeazley here is the full solution:感谢@grbeazley,这里是完整的解决方案:

from pandera import Column, Check
import yaml

in_ = pa.DataFrameSchema(
    {
        "Name": Column(object, nullable=True),
        "Height": Column(object, nullable=True),
    })
with open("in_.yml", "w") as file:
    yaml.dump(in_.to_yaml(), file)

with open("./in_.yml", "r") as file:
    in_ = yaml.safe_load(file.read())

_ = pa.DataFrameSchema.from_yaml(in_)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 class 上的 python 装饰器:TypeError:super() 参数 1 必须是类型,而不是 function - python decorator on class: TypeError: super() argument 1 must be type, not function 将派生类的超级方法与类装饰器一起使用时发生TypeError - TypeError when using super method with class decorator for a derived class Class 在装饰器 arguments 中无法识别 - Class not recognized in decorator arguments `TypeError:strptime()参数0必须是str,而不是 <class 'bytes'> ` - `TypeError: strptime() argument 0 must be str, not <class 'bytes'>` `TypeError: 'str' object is not callable` 当装饰器 function 被调用时 - `TypeError: 'str' object is not callable` when a decorator function is called 类型错误:无法识别的值类型:<class 'str'> - TypeError: Unrecognized value type: <class 'str'> 使用 Pydantic 和 Pandera 初始化 Class 属性 - Initializing Class Attributes With Pydantic and Pandera 将函数传递到类中并将其用作类方法的装饰器 - passing function into a class and using it as a decorator of class methods 在使用类型创建类时如何定义__str__函数? - How to define the __str__ function while using type to create a class? Python - 对类使用 lambda 函数的装饰器函数 - Python - decorator function using lambda function for a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM