Skip to content

ts_shape.transform.calculator.numeric_calc ¤

Classes:

  • IntegerCalc

    Provides class methods for performing calculations on integer columns in a pandas DataFrame.

IntegerCalc ¤

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

Bases: Base

Provides class methods for performing calculations on integer columns in a pandas DataFrame.

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:

  • calculate_with_fixed_factors

    Performs a calculation by multiplying with a factor and then adding an additional factor.

  • divide_column

    Divides each value in the integer column by the given divisor.

  • get_dataframe

    Returns the processed DataFrame.

  • mod_column

    Performs a modulus operation on the integer column with a specified value.

  • offset_column

    Offsets the integer column by the given value.

  • power_column

    Raises each value in the integer column to the power of a specified value.

  • scale_column

    Scales the integer column by the given factor.

  • subtract_column

    Subtracts a given value from each element in the integer column.

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)

calculate_with_fixed_factors classmethod ¤

calculate_with_fixed_factors(dataframe: DataFrame, column_name: str = 'value_integer', multiply_factor: float = 1, add_factor: float = 0) -> DataFrame

Performs a calculation by multiplying with a factor and then adding an additional factor.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the calculations to.

  • multiply_factor ¤

    (float, default: 1 ) –

    The factor to multiply each value by. Defaults to 1 (no scaling).

  • add_factor ¤

    (float, default: 0 ) –

    The value to add after multiplication. Defaults to 0 (no offset).

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame after applying the calculations.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@classmethod
def calculate_with_fixed_factors(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', multiply_factor: float = 1, add_factor: float = 0) -> pd.DataFrame:
    """
    Performs a calculation by multiplying with a factor and then adding an additional factor.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the calculations to.
        multiply_factor (float): The factor to multiply each value by. Defaults to 1 (no scaling).
        add_factor (float): The value to add after multiplication. Defaults to 0 (no offset).

    Returns:
        pd.DataFrame: The DataFrame after applying the calculations.
    """
    dataframe[column_name] = (dataframe[column_name] * multiply_factor) + add_factor
    return dataframe

divide_column classmethod ¤

divide_column(dataframe: DataFrame, column_name: str = 'value_integer', divisor: float = 1) -> DataFrame

Divides each value in the integer column by the given divisor.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the division to.

  • divisor ¤

    (float, default: 1 ) –

    The value by which to divide each element.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the divided column.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@classmethod
def divide_column(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', divisor: float = 1) -> pd.DataFrame:
    """
    Divides each value in the integer column by the given divisor.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the division to.
        divisor (float): The value by which to divide each element.

    Returns:
        pd.DataFrame: The DataFrame with the divided column.
    """
    dataframe[column_name] = dataframe[column_name] / divisor
    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

mod_column classmethod ¤

mod_column(dataframe: DataFrame, column_name: str = 'value_integer', mod_value: int = 1) -> DataFrame

Performs a modulus operation on the integer column with a specified value.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the modulus operation to.

  • mod_value ¤

    (int, default: 1 ) –

    The value to perform the modulus operation with.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the modulus operation applied.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@classmethod
def mod_column(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', mod_value: int = 1) -> pd.DataFrame:
    """
    Performs a modulus operation on the integer column with a specified value.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the modulus operation to.
        mod_value (int): The value to perform the modulus operation with.

    Returns:
        pd.DataFrame: The DataFrame with the modulus operation applied.
    """
    dataframe[column_name] = dataframe[column_name] % mod_value
    return dataframe

offset_column classmethod ¤

offset_column(dataframe: DataFrame, column_name: str = 'value_integer', offset_value: float = 0) -> DataFrame

Offsets the integer column by the given value.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the offset to.

  • offset_value ¤

    (float, default: 0 ) –

    The value to add (positive) or subtract (negative) from each element in the column.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the offset column.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@classmethod
def offset_column(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', offset_value: float = 0) -> pd.DataFrame:
    """
    Offsets the integer column by the given value.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the offset to.
        offset_value (float): The value to add (positive) or subtract (negative) from each element in the column.

    Returns:
        pd.DataFrame: The DataFrame with the offset column.
    """
    dataframe[column_name] = dataframe[column_name] + offset_value
    return dataframe

power_column classmethod ¤

power_column(dataframe: DataFrame, column_name: str = 'value_integer', power_value: float = 1) -> DataFrame

Raises each value in the integer column to the power of a specified value.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the power operation to.

  • power_value ¤

    (float, default: 1 ) –

    The exponent to raise each element to.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the power operation applied.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
@classmethod
def power_column(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', power_value: float = 1) -> pd.DataFrame:
    """
    Raises each value in the integer column to the power of a specified value.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the power operation to.
        power_value (float): The exponent to raise each element to.

    Returns:
        pd.DataFrame: The DataFrame with the power operation applied.
    """
    dataframe[column_name] = dataframe[column_name] ** power_value
    return dataframe

scale_column classmethod ¤

scale_column(dataframe: DataFrame, column_name: str = 'value_integer', factor: float = 1) -> DataFrame

Scales the integer column by the given factor.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the scaling to.

  • factor ¤

    (float, default: 1 ) –

    The scaling factor.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the scaled column.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@classmethod
def scale_column(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', factor: float = 1) -> pd.DataFrame:
    """
    Scales the integer column by the given factor.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the scaling to.
        factor (float): The scaling factor.

    Returns:
        pd.DataFrame: The DataFrame with the scaled column.
    """
    dataframe[column_name] = dataframe[column_name] * factor
    return dataframe

subtract_column classmethod ¤

subtract_column(dataframe: DataFrame, column_name: str = 'value_integer', subtract_value: float = 0) -> DataFrame

Subtracts a given value from each element in the integer column.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to perform the operation on.

  • column_name ¤

    (str, default: 'value_integer' ) –

    The column to apply the subtraction to.

  • subtract_value ¤

    (float, default: 0 ) –

    The value to subtract from each element.

Returns:

  • DataFrame

    pd.DataFrame: The DataFrame with the subtracted column.

Source code in src/ts_shape/transform/calculator/numeric_calc.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@classmethod
def subtract_column(cls, dataframe: pd.DataFrame, column_name: str = 'value_integer', subtract_value: float = 0) -> pd.DataFrame:
    """
    Subtracts a given value from each element in the integer column.

    Args:
        dataframe (pd.DataFrame): The DataFrame to perform the operation on.
        column_name (str): The column to apply the subtraction to.
        subtract_value (float): The value to subtract from each element.

    Returns:
        pd.DataFrame: The DataFrame with the subtracted column.
    """
    dataframe[column_name] = dataframe[column_name] - subtract_value
    return dataframe