Skip to content

ts_shape.context.value_mapping ¤

Classes:

  • ValueMapper

    A class to map values from specified columns of a DataFrame using a mapping table (CSV or JSON file),

ValueMapper ¤

ValueMapper(dataframe: DataFrame, mapping_file: str, map_column: str, mapping_key_column: str, mapping_value_column: str, file_type: str = 'csv', sep: str = ',', encoding: str = 'utf-8', column_name: str = 'systime')

Bases: Base

A class to map values from specified columns of a DataFrame using a mapping table (CSV or JSON file), inheriting from the Base class.

Parameters:

  • dataframe ¤

    (DataFrame) –

    The DataFrame to be processed and mapped.

  • mapping_file ¤

    (str) –

    The file path of the mapping table (CSV or JSON).

  • map_column ¤

    (str) –

    The name of the column in the DataFrame that needs to be mapped.

  • mapping_key_column ¤

    (str) –

    The column in the mapping table to match with values from the DataFrame.

  • mapping_value_column ¤

    (str) –

    The column in the mapping table containing the values to map to.

  • file_type ¤

    (str, default: 'csv' ) –

    The type of the mapping file ('csv' or 'json'). Defaults to 'csv'.

  • sep ¤

    (str, default: ',' ) –

    The separator for CSV files. Defaults to ','.

  • encoding ¤

    (str, default: 'utf-8' ) –

    The encoding to use for reading the file. Defaults to 'utf-8'.

  • column_name ¤

    (str, default: 'systime' ) –

    The name of the column to sort the DataFrame by in the base class. Defaults to 'systime'.

Methods:

  • get_dataframe

    Returns the processed DataFrame.

  • map_values

    Maps values in the specified DataFrame column based on the mapping table.

Source code in src/ts_shape/context/value_mapping.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def __init__(
    self, 
    dataframe: pd.DataFrame, 
    mapping_file: str, 
    map_column: str, 
    mapping_key_column: str, 
    mapping_value_column: str, 
    file_type: str = 'csv', 
    sep: str = ',', 
    encoding: str = 'utf-8', 
    column_name: str = 'systime'
) -> None:
    """
    Initializes ValueMapper and the base DataFrame from the Base class.

    Args:
        dataframe (pd.DataFrame): The DataFrame to be processed and mapped.
        mapping_file (str): The file path of the mapping table (CSV or JSON).
        map_column (str): The name of the column in the DataFrame that needs to be mapped.
        mapping_key_column (str): The column in the mapping table to match with values from the DataFrame.
        mapping_value_column (str): The column in the mapping table containing the values to map to.
        file_type (str): The type of the mapping file ('csv' or 'json'). Defaults to 'csv'.
        sep (str): The separator for CSV files. Defaults to ','.
        encoding (str): The encoding to use for reading the file. Defaults to 'utf-8'.
        column_name (str): The name of the column to sort the DataFrame by in the base class. Defaults to 'systime'.
    """
    # Initialize the Base class with the sorted DataFrame
    super().__init__(dataframe, column_name)

    # Additional attributes for ValueMapper
    self.map_column: str = map_column
    self.mapping_key_column: str = mapping_key_column
    self.mapping_value_column: str = mapping_value_column
    self.sep: str = sep
    self.encoding: str = encoding

    # Load the mapping table based on file type
    self.mapping_table: pd.DataFrame = self._load_mapping_table(mapping_file, file_type)

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

map_values ¤

map_values() -> DataFrame

Maps values in the specified DataFrame column based on the mapping table.

Returns:

  • DataFrame

    pd.DataFrame: A new DataFrame with the mapped values.

Source code in src/ts_shape/context/value_mapping.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def map_values(self) -> pd.DataFrame:
    """
    Maps values in the specified DataFrame column based on the mapping table.

    Returns:
        pd.DataFrame: A new DataFrame with the mapped values.
    """
    # Merge the mapping table with the DataFrame based on the map_column and mapping_key_column
    mapped_df = self.dataframe.merge(
        self.mapping_table[[self.mapping_key_column, self.mapping_value_column]],
        left_on=self.map_column,
        right_on=self.mapping_key_column,
        how='left'
    )

    # Replace the original column with the mapped values
    mapped_df[self.map_column] = mapped_df[self.mapping_value_column]

    # Drop unnecessary columns
    mapped_df = mapped_df.drop([self.mapping_key_column, self.mapping_value_column], axis=1)

    return mapped_df