Implementation:Evidentlyai Evidently Legacy Python Engine
| Knowledge Sources | |
|---|---|
| Domains | Calculation_Engine, Pandas, Legacy |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
legacy/calculation_engine/python_engine.py provides the PythonEngine class, a pandas-based concrete implementation of the abstract Engine, and the PythonMetricImplementation base class for metric implementations that operate on pandas DataFrames.
Description
This module implements the legacy calculation engine for pandas DataFrames:
PythonEngine-- Concrete engine that specializesEngineforPythonMetricImplementation,InputData, andpd.DataFrame. Key methods:convert_input_data-- Validates that input data is apd.DataFrameand wraps it in anInputDataobject.get_data_definition-- Creates aDataDefinitionfrom current and reference DataFrames usingcreate_data_definition.calculate_additional_features-- Iterates overGeneratedFeaturesand generates renamed feature columns for both current and reference data.merge_additional_features-- Joins all generated feature DataFrames into single current and reference DataFrames usingpd.DataFrame.join.get_metric_implementation-- Extends the parent method with a fallback: if no registered implementation exists for a metric, creates a dynamic wrapper (_Wrapper) that delegates to the metric's owncalculatemethod.form_datasets-- Joins additional features into the main DataFrames and renames columns to their display names.
PythonMetricImplementation-- Generic abstract base class for metric implementations running onPythonEngine. Holds references to the engine and the metric instance. DeclaresPythonEngineas the only supported engine viasupported_engines.
Usage
This is the default engine used by the legacy Evidently pipeline when working with pandas DataFrames. Subclass PythonMetricImplementation and decorate with @metric_implementation to register custom metric calculations.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File:
src/evidently/legacy/calculation_engine/python_engine.py
Signature
class PythonEngine(Engine["PythonMetricImplementation", InputData, pd.DataFrame]):
def convert_input_data(self, data: GenericInputData[pd.DataFrame]) -> InputData: ...
def get_data_definition(self, current_data, reference_data, column_mapping, categorical_features_cardinality=None): ...
def calculate_additional_features(self, data, features, options) -> Dict[GeneratedFeatures, FeatureResult[pd.DataFrame]]: ...
def merge_additional_features(self, features) -> EngineDatasets[pd.DataFrame]: ...
def get_metric_implementation(self, metric): ...
def form_datasets(self, data, features, data_definition) -> EngineDatasets[pd.DataFrame]: ...
class PythonMetricImplementation(Generic[TMetric], MetricImplementation):
def __init__(self, engine: PythonEngine, metric: TMetric): ...
@abc.abstractmethod
def calculate(self, context, data: InputData): ...
@classmethod
def supported_engines(cls): ...
Import
from evidently.legacy.calculation_engine.python_engine import PythonEngine, PythonMetricImplementation
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | GenericInputData[pd.DataFrame] |
Yes | Input data containing current and optional reference DataFrames plus column mapping and data definition. |
| features | List[GeneratedFeatures] |
Yes | List of generated feature definitions to compute and merge into datasets. |
| options | Options |
Yes | Legacy options for feature generation. |
| context | Context |
Yes | Suite context (passed to calculate of metric implementations).
|
Outputs
| Name | Type | Description |
|---|---|---|
| convert_input_data return | InputData |
Validated and wrapped input data for the pandas engine. |
| calculate_additional_features return | Dict[GeneratedFeatures, FeatureResult[pd.DataFrame]] |
Mapping from feature definitions to their computed current/reference DataFrames. |
| merge_additional_features return | EngineDatasets[pd.DataFrame] |
Merged current and reference additional feature DataFrames. |
| form_datasets return | EngineDatasets[pd.DataFrame] |
Final datasets with additional features joined and columns renamed. |
Usage Examples
from evidently.legacy.calculation_engine.python_engine import PythonEngine, PythonMetricImplementation
from evidently.legacy.calculation_engine.engine import metric_implementation
from evidently.legacy.base_metric import InputData
@metric_implementation(MyMetric)
class MyMetricPythonImpl(PythonMetricImplementation["MyMetric"]):
def calculate(self, context, data: InputData):
current_df = data.current_data
# Perform calculations on pandas DataFrame
return MyMetricResult(value=current_df["column"].mean())