ts_shape.context.value_mapping
| Name | Description |
|---|---|
ValueMapper | A class to map values from specified columns of a DataFrame using a mapping table (CSV or JSON file), |
ValueMapper
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.
| Name | Description |
|---|---|
map_values | Maps values in the specified DataFrame column based on the mapping table. |
| 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. 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.
| Type | Description |
|---|---|
DataFrame | pd.DataFrame: A new DataFrame with the mapped values. |
| 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. |