![](/img/trans.png)
[英]python decorator on class: TypeError: super() argument 1 must be type, not function
[英]Getting "TypeError: type of out argument not recognized: <class 'str'>" when using class function with Pandera decorator
我正在尝试使用 Python 包“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)
接下来我用类创建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
最后我导入了这个类:
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)
我正进入(状态:
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'>
任何帮助将不胜感激
check_io
装饰器需要pandera.DataFrameSchema
类型的参数。 但是,它被传递给类型为 _out 的str
因为它是_out
file.read()
的输出。
Pandera 文档解释了check_io
装饰器期望的类型。
一个解决方案是将file.read()
行的输出传递给 Pandera 构造函数,可能需要进行一些转换:
out_ = yaml.safe_load(file.read())
感谢@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.