Skip to main content
Version: Next

ts_shape.context.value_mapping

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

ValueMapper​

classsource
ValueMapper(
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",
time_column: str = "systime",
) -> None

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.

Functions
NameDescription
map_valuesMaps values in the specified DataFrame column based on the mapping table.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame to be processed and mapped.required
mapping_filestrThe file path of the mapping table (CSV or JSON).required
map_columnstrThe name of the column in the DataFrame that needs to be mapped.required
mapping_key_columnstrThe column in the mapping table to match with values from the DataFrame.required
mapping_value_columnstrThe column in the mapping table containing the values to map to.required
file_typestrThe type of the mapping file ('csv' or 'json'). Defaults to 'csv'.'csv'
sepstrThe separator for CSV files. Defaults to ','.','
encodingstrThe encoding to use for reading the file. Defaults to 'utf-8'.'utf-8'
time_columnstrTimestamp column the base class sorts by (formerly column_name). Defaults to 'systime'.'systime'

map_values​

methodsource
map_values() -> pd.DataFrame

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

Keys are compared as text. The mapping file's keys are read as text verbatim ("007" matches only "007", never 7; a key "1.0" matches only the text "1.0"). Values of map_column are turned into text, with integral floats written without the fraction, so the key "1" matches the codes 1, 1.0 and "1", and the key "7" matches 7 but not "007". 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
TypeDescription
DataFramepd.DataFrame: A new DataFrame with the mapped values.
Raises
TypeDescription
ColumnNotFoundErrorWhen map_column or a mapping-table column is missing.
ValueErrorWhen the mapping table has duplicate keys, or the key and value columns of the mapping table are the same column.