我正在尝试使用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'>
任何帮助都将不胜感激
2条答案
按热度按时间mw3dktmi1#
check_io
装饰器需要类型为pandera.DataFrameSchema
的参数。但是,由于它是file.read()
的输出,因此传递的是类型为str
的_out
。Pandera文档解释了
check_io
装饰器需要哪些类型。一个解决方案是将
file.read()
行的输出传递给Pandera构造函数,可能需要进行一些转换:unftdfkk2#
感谢@grbeazley,以下是完整的解决方案: