Skip to content

value_mapping

value_mapping ¤

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",
    time_column: 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.

Initializes ValueMapper and the base DataFrame from the Base class.

Parameters:

Name Type Description Default
dataframe DataFrame

The DataFrame to be processed and mapped.

required
mapping_file str

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

required
map_column str

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

required
mapping_key_column str

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

required
mapping_value_column str

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

required
file_type str

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

'csv'
sep str

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

','
encoding str

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

'utf-8'
time_column str

Timestamp column the base class sorts by (formerly column_name). Defaults to 'systime'.

'systime'

map_values ¤

map_values() -> pd.DataFrame

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

Keys are compared as text, so a mapping file with string keys maps an integer code column (1, 1.0 and "1" all match). Codes the table does not contain keep their original value, with a :class:~ts_shape.errors.DataQualityWarning counting them. The other columns of the DataFrame are unchanged, whatever the mapping table's column names are.

Returns:

Type Description
DataFrame

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

Raises:

Type Description
ColumnNotFoundError

When map_column or a mapping-table column is missing.

ValueError

When the mapping table has duplicate keys, or the key and value columns of the mapping table are the same column.