find_or_check_categorical_variables#

feature_engine.variable_handling.find_or_check_categorical_variables(X, variables=None)[source]#

Returns the names of all the variables of type object or categorical in a dataframe. Alternatively, it checks that the variables entered by the user are of type object or categorical.

Note that when variables is None, the transformer will not select variables of type object that can be parsed as datetime. But if the user passes a list with datetime variables cast as object to the variables parameter, they will be allowed.

More details in the User Guide.

Parameters
Xpandas dataframe of shape = [n_samples, n_features]

The dataset

variableslist, default=None

If None, the function returns the names of all object or categorical variables in X. Alternatively, it checks that the variables in the list are of type object or categorical.

Returns
variables: List

The names of the categorical variables.

Examples

>>> import pandas as pd
>>> from feature_engine.variable_handling import find_or_check_categorical_variables
>>> X = pd.DataFrame({
>>>     "var_num": [1, 2, 3],
>>>     "var_cat": ["A", "B", "C"],
>>>     "var_date": pd.date_range("2020-02-24", periods=3, freq="T")
>>> })
>>> var_cat = find_or_check_categorical_variables(X)
>>> var_cat
['var_cat']
Return type

List[Union[str, int]]