Skip to content

ts_shape.transform.functions.lambda_func ¤

Classes:

  • LambdaProcessor

    Provides class methods for applying lambda or callable functions to columns in a pandas DataFrame.

LambdaProcessor ¤

LambdaProcessor(dataframe: DataFrame, column_name: str = 'systime')

Bases: Base

Provides class methods for applying lambda or callable functions to columns in a pandas DataFrame. This class inherits from Base, ensuring consistency with other processors.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to be processed.

  • column_name ¤

    (str, default: 'systime' ) –

    The column to sort by. Default is 'systime'. If the column is not found or is not a time column, the class will attempt to detect other time columns.

Methods:

  • apply_function

    Applies a lambda or callable function to a specified column in the DataFrame.

  • get_dataframe

    Returns the processed DataFrame.

Source code in src/ts_shape/utils/base.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def __init__(self, dataframe: pd.DataFrame, column_name: str = 'systime') -> pd.DataFrame:
    """
    Initializes the Base with a DataFrame, detects time columns, converts them to datetime,
    and sorts the DataFrame by the specified column (or the detected time column if applicable).

    Args:
        dataframe (pd.DataFrame): The DataFrame to be processed.
        column_name (str): The column to sort by. Default is 'systime'. If the column is not found or is not a time column, the class will attempt to detect other time columns.
    """
    self.dataframe = dataframe.copy()

    # Attempt to convert the specified column_name to datetime if it exists
    if column_name in self.dataframe.columns:
        self.dataframe[column_name] = pd.to_datetime(self.dataframe[column_name], errors='coerce')
    else:
        # If the column_name is not in the DataFrame, fallback to automatic time detection
        time_columns = [col for col in self.dataframe.columns if 'time' in col.lower() or 'date' in col.lower()]

        # Convert all detected time columns to datetime, if any
        for col in time_columns:
            self.dataframe[col] = pd.to_datetime(self.dataframe[col], errors='coerce')

        # If any time columns are detected, sort by the first one; otherwise, do nothing
        if time_columns:
            column_name = time_columns[0]

    # Sort by the datetime column (either specified or detected)
    if column_name in self.dataframe.columns:
        self.dataframe = self.dataframe.sort_values(by=column_name)

apply_function classmethod ¤

apply_function(dataframe: DataFrame, column_name: str, func: Callable[[Any], Any]) -> DataFrame

Applies a lambda or callable function to a specified column in the DataFrame.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame containing the data.

  • column_name ¤

    (str) –

    The name of the column to apply the function to.

  • func ¤

    (Callable) –

    The lambda function or callable to apply to the column.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the transformed column.

Source code in src/ts_shape/transform/functions/lambda_func.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@classmethod
def apply_function(cls, dataframe: pd.DataFrame, column_name: str, func: Callable[[Any], Any]) -> pd.DataFrame:
    """
    Applies a lambda or callable function to a specified column in the DataFrame.

    Args:
        dataframe (pd.DataFrame): The DataFrame containing the data.
        column_name (str): The name of the column to apply the function to.
        func (Callable): The lambda function or callable to apply to the column.

    Returns:
        pd.DataFrame: The DataFrame with the transformed column.
    """
    if column_name not in dataframe.columns:
        raise ValueError(f"Column '{column_name}' not found in DataFrame.")

    dataframe[column_name] = func(dataframe[column_name].values)
    return dataframe

get_dataframe ¤

get_dataframe() -> DataFrame

Returns the processed DataFrame.

Source code in src/ts_shape/utils/base.py
34
35
36
def get_dataframe(self) -> pd.DataFrame:
    """Returns the processed DataFrame."""
    return self.dataframe