Refactor data handling in various modules to ensure DataFrame consistency - Replaced direct DataFrame instantiation with `ensure_dataframe` utility in Gates, MLFlow, OPC, and ModelMetrics classes to standardize data handling. - Updated return types in several asynchronous methods to return DataFrames instead of dictionaries for improved usability. - Adjusted data export processes in workflows to convert DataFrames to dictionaries with `to_dict(orient='records')` for compatibility with downstream systems.
32 lines
825 B
Python
32 lines
825 B
Python
"""
|
|
DataFrame utility functions for handling serialized DataFrames.
|
|
|
|
This module provides helper functions to work with DataFrames that may
|
|
come from Temporal serialization (already as DataFrame) or from legacy
|
|
code (as dict).
|
|
"""
|
|
|
|
from typing import Any
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
def ensure_dataframe(data: Any) -> DataFrame:
|
|
"""
|
|
Ensure that data is a DataFrame, converting from dict if necessary.
|
|
|
|
This function handles both cases:
|
|
- Data already deserialized as DataFrame (from Temporal codec)
|
|
- Data as dict (legacy format or non-DataFrame serialization)
|
|
|
|
Args:
|
|
data: Data that should be a DataFrame (can be DataFrame or dict)
|
|
|
|
Returns:
|
|
DataFrame: The data as a pandas DataFrame
|
|
"""
|
|
if isinstance(data, DataFrame):
|
|
return data
|
|
return DataFrame(data)
|
|
|