tubular.mapping.MappingTransformer

class tubular.mapping.MappingTransformer(mappings, **kwargs)[source]

Bases: tubular.mapping.BaseMappingTransformer, tubular.mapping.BaseMappingTransformMixin

Transformer to map values in columns to other values e.g. to merge two levels into one.

Note, the MappingTransformer does not require ‘self-mappings’ to be defined i.e. if you want to map a value to itself, you can omit this value from the mappings rather than having to map it to itself. This is because it uses the ReturnKeyDict type to store the mappings for each columns, this dict will return the key i.e. the original value in that row if it is not available in the mapping dict.

This transformer inherits from BaseMappingTransformMixin as well as the BaseMappingTransformer in order to access the standard pd.Series.map transform function.

Parameters
  • mappings (dict) – Dictionary containing column mappings. Each value in mappings should be a dictionary of key (column to apply mapping to) value (mapping dict for given columns) pairs. For example the following dict {‘a’: {1: 2, 3: 4}, ‘b’: {‘a’: 1, ‘b’: 2}} would specify a mapping for column a of 1->2, 3->4 and a mapping for column b of ‘a’->1, b->2.

  • **kwargs – Arbitrary keyword arguments passed onto BaseMappingTransformer.init method.

mappings

Dictionary of mappings for each column individually. The dict passed to mappings in init is set to the mappings attribute.

Type

dict

__init__(mappings, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(mappings, **kwargs)

Initialize self.

check_is_fitted(attribute)

Check if particular attributes are on the object.

check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

columns_set_or_check(X)

Function to check or set columns attribute.

fit(X[, y])

Base transformer fit method, checks X and y types.

fit_transform(X[, y])

Fit to data, then transform it.

get_params([deep])

Get parameters for this estimator.

set_params(**params)

Set the parameters of this estimator.

transform(X[, suppress_dtype_warning])

Transform the input data X according to the mappings in the mappings attribute dict.

check_is_fitted(attribute)

Check if particular attributes are on the object. This is useful to do before running transform to avoid trying to transform data without first running the fit method.

Wrapper for utils.validation.check_is_fitted function.

Parameters

attributes (List) – List of str values giving names of attribute to check exist on self.

static check_weights_column(X, weights_column)

Helper method for validating weights column in dataframe.

X (pd.DataFrame): df containing weight column weights_column (str): name of weight column

classname()

Method that returns the name of the current class when called.

columns_check(X)

Method to check that the columns attribute is set and all values are present in X.

Parameters

X (pd.DataFrame) – Data to check columns are in.

columns_set_or_check(X)

Function to check or set columns attribute.

If the columns attribute is None then set it to all columns in X. Otherwise run the columns_check method.

Parameters

X (pd.DataFrame) – Data to check columns are in.

fit(X, y=None)

Base transformer fit method, checks X and y types. Currently only pandas DataFrames are allowed for X and DataFrames or Series for y.

Fit calls the columns_set_or_check method which will set the columns attribute to all columns in X, if it is None.

Parameters
  • X (pd.DataFrame) – Data to fit the transformer on.

  • y (None or pd.DataFrame or pd.Series, default = None) – Optional argument only required for the transformer to work with sklearn pipelines.

fit_transform(X, y=None, **fit_params)

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters
  • X (array-like of shape (n_samples, n_features)) – Input samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).

  • **fit_params (dict) – Additional fit parameters.

Returns

X_new – Transformed array.

Return type

ndarray array of shape (n_samples, n_features_new)

get_params(deep=True)

Get parameters for this estimator.

Parameters

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns

params – Parameter names mapped to their values.

Return type

dict

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters

**params (dict) – Estimator parameters.

Returns

self – Estimator instance.

Return type

estimator instance

transform(X, suppress_dtype_warning=False)[source]

Transform the input data X according to the mappings in the mappings attribute dict.

This method calls the BaseMappingTransformMixin.transform. Note, this transform method is different to some of the transform methods in the nominal module, even though they also use the BaseMappingTransformMixin.transform method. Here, if a value does not exist in the mapping it is unchanged.

Due to the way pd.Series.map works, mappings can result in column dtypes changing, sometimes unexpectedly. If the result of the mappings is a dtype that doesn’t match the original dtype, or the dtype of the values provided in the mapping a warning will be raised. This normally results from an incomplete mapping being provided, or a mix of dtypes causing pandas to default to the object dtype.

For columns with a ‘category’ dtype the warning will not be raised.

Parameters
  • X (pd.DataFrame) – Data with nominal columns to transform.

  • suppress_dtype_warning (Bool, default = False) – Whether to suppress warnings about dtype changes

Returns

X – Transformed input X with levels mapped accoriding to mappings dict.

Return type

pd.DataFrame