Need expert statistical support?

More Info

Module

asreml.define_categorical_variables

define_categorical_variables module

Members

function

define_categorical_variables

#
def define_categorical_variables( data, variables: Optional[list[Union[str, dict]]] = None, no_labels: Optional[list[str]] = None, equate_levels: Optional[list[Union[str, list]]] = None, overwrite: bool = False, text_convert: Optional[bool] = None, date_convert: Optional[bool] = True, use_data_order: bool = False) -> dict:

define categorical variables / factors, flagging them in the pandas dataframe as categorical and (if required) recoding them and returning their levels in a dictionary. The returned dictionary is suitable for passing to the data argument of asreml.asreml or the data and categories arguments of asreml.ASReml.add_data (if unpacked).

Arguments:
  • data: pandas dataframe
  • variables: list of variables that are categorical, each element of variables should either be a column name of data, or a dictionary with keys: "name" and "levels", where "name" is the column name and "levels" is a list of values defining the levels.
  • no_labels: list of variables in variables that are to be treated as numerical 1...n with all levels present i.e. they have no labels added to categories. This can be used for factors in autocorrelation functions to stop warnings.
  • equate_levels: list of variables that are to have their labels and levels combined, or a list of list of variables if there are multiple groups
  • overwrite: if True data will be overwritten on exit, otherwise a deep copy is made.
  • text_convert: if True text columns in the data frames will automatically be converted to categorical variables, if False, text columns will be left in the data frame, otherwise if not set, text columns not in variables will be dropped from the data frame.
  • date_convert: if True date columns will be converted to a float, being the number of days from the first date in the column, if False, date columns will be left in the data frame, otherwise if not set, date columns not in variables will be dropped from the data frame.
  • use_data_order: if True then the levels are defined in data order, otherwise they are sorted
Returns:

dictionary with two items holding the categorized data frame and category labels item: 'data' - pandas dataframe with specified variables recoded as categories item: 'categories' - dictionary holding the factor level/labels

Want to sharpen your statistics skills?

Browse Courses