我正在尝试使用 Great Expectations v3 api 创建一个非常简单的期望:expect_column_values_to_be_positive。我正在使用 PandasExecutionEngine,我的数据资产是 pandas 数据框。
my_custom_expectation.py 位于 plugins/ 文件夹中。
这是我在 my_custom_expectation.py 中的代码:
from great_expectations.execution_engine import (
PandasExecutionEngine,
)
from great_expectations.expectations.metrics import (
ColumnMapMetricProvider,
column_condition_partial
)
from great_expectations.expectations.expectation import (
ColumnMapExpectation,
)
def check_positive(value):
if value:
return True if value > 0 else False
return True
class ColumnValueIsPositive(ColumnMapMetricProvider):
condition_metric_name = "column_values.to_be_positive"
@column_condition_partial(engine=PandasExecutionEngine)
def _pandas(cls, column, **kwargs):
return column.apply(lambda x: check_positive(x))
class ExpectColumnValuesToBePositive(ColumnMapExpectation):
map_metric = "column_values.to_be_positive"
然后在我的 jupyter notebook 中,我尝试创建我的期望:
from my_custom_expectation import ExpectColumnValuesToBePositive
validator.expect_column_values_to_be_positive(column="duration")
但是,我收到以下错误:
TypeError Traceback (most recent call last)
/tmp/ipykernel_5957/859745029.py in <module>
----> 1 validator.expect_column_values_to_be_positive(column="duration")
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/validator/validator.py in inst_expectation(*args, **kwargs)
285
286 else:
--> 287 raise err
288 return validation_result
289
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/validator/validator.py in inst_expectation(*args, **kwargs)
240 )
241 else:
--> 242 validation_result = expectation.validate(
243 validator=self,
244 evaluation_parameters=self._expectation_suite.evaluation_parameters,
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/expectations/expectation.py in validate(self, validator, configuration, evaluation_parameters, interactive_evaluation, data_context, runtime_configuration)
631 evaluation_parameters, interactive_evaluation, data_context
632 )
--> 633 evr = validator.graph_validate(
634 configurations=[configuration],
635 runtime_configuration=runtime_configuration,
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/validator/validator.py in graph_validate(self, configurations, metrics, runtime_configuration)
499 return evrs
500 else:
--> 501 raise err
502
503 for configuration in processed_configurations:
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/validator/validator.py in graph_validate(self, configurations, metrics, runtime_configuration)
477 # an exception occurring as part of resolving the combined validation graph impacts all expectations in suite.
478 try:
--> 479 self.resolve_validation_graph(
480 graph=graph,
481 metrics=metrics,
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/validator/validator.py in resolve_validation_graph(self, graph, metrics, runtime_configuration)
555
556 metrics.update(
--> 557 self._resolve_metrics(
558 execution_engine=self._execution_engine,
559 metrics_to_resolve=ready_metrics,
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/validator/validator.py in _resolve_metrics(execution_engine, metrics_to_resolve, metrics, runtime_configuration)
603 """A means of accessing the Execution Engine's resolve_metrics method, where missing metric configurations are
604 resolved"""
--> 605 return execution_engine.resolve_metrics(
606 metrics_to_resolve=metrics_to_resolve,
607 metrics=metrics,
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/execution_engine/execution_engine.py in resolve_metrics(self, metrics_to_resolve, metrics, runtime_configuration)
283 # than data to optimize compute in the future
284 try:
--> 285 resolved_metrics[metric_to_resolve.id] = metric_fn(
286 **metric_provider_kwargs
287 )
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/expectations/metrics/metric_provider.py in inner_func(*args, **kwargs)
53 @wraps(metric_fn)
54 def inner_func(*args, **kwargs):
---> 55 return metric_fn(*args, **kwargs)
56
57 inner_func.metric_engine = engine
~/.local/share/virtualenvs/ge-YbASoQtb/lib/python3.8/site-packages/great_expectations/expectations/metrics/map_metric_provider.py in inner_func(cls, execution_engine, metric_domain_kwargs, metric_value_kwargs, metrics, runtime_configuration)
326 df = df[df[column_name].notnull()]
327
--> 328 meets_expectation_series = metric_fn(
329 cls,
330 df[column_name],
TypeError: _pandas() got an unexpected keyword argument '_metrics'
我错过了什么吗?我按照这个例子来写我的期望/指标。