nodes: 0, module; 1, function_definition; 2, function_name:factorize; 3, parameters; 4, block; 5, identifier:train; 6, identifier:test; 7, identifier:features; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, expression_statement; 12, for_statement; 13, return_statement; 14, identifier:na_value; 15, unary_operator; 16, identifier:full; 17, False; 18, identifier:sort; 19, True; 20, comment:"""Factorize categorical features.
  
      Parameters
      ----------
      train : pd.DataFrame
      test : pd.DataFrame
      features : list
             Column names in the DataFrame to be encoded.
      na_value : int, default -9999
      full : bool, default False
          Whether use all columns from train/test or only from train.
      sort : bool, default True
          Sort by values.
  
      Returns
      -------
      train : pd.DataFrame
      test : pd.DataFrame
      """; 21, identifier:column; 22, identifier:features; 23, block; 24, expression_list; 25, integer:9999; 26, if_statement; 27, expression_statement; 28, expression_statement; 29, if_statement; 30, identifier:train; 31, identifier:test; 32, identifier:full; 33, block; 34, else_clause; 35, assignment; 36, assignment; 37, comparison_operator:na_value != -1; 38, block; 39, expression_statement; 40, expression_statement; 41, block; 42, subscript; 43, call; 44, subscript; 45, call; 46, identifier:na_value; 47, unary_operator; 48, expression_statement; 49, expression_statement; 50, assignment; 51, assignment; 52, expression_statement; 53, identifier:train; 54, identifier:column; 55, attribute; 56, argument_list; 57, identifier:test; 58, identifier:column; 59, attribute; 60, argument_list; 61, integer:1; 62, assignment; 63, assignment; 64, identifier:vs; 65, call; 66, pattern_list; 67, call; 68, assignment; 69, identifier:indexer; 70, identifier:get_indexer; 71, subscript; 72, identifier:indexer; 73, identifier:get_indexer; 74, subscript; 75, subscript; 76, call; 77, subscript; 78, call; 79, attribute; 80, argument_list; 81, identifier:labels; 82, identifier:indexer; 83, attribute; 84, argument_list; 85, pattern_list; 86, call; 87, identifier:train; 88, identifier:column; 89, identifier:test; 90, identifier:column; 91, identifier:train; 92, identifier:column; 93, attribute; 94, argument_list; 95, identifier:test; 96, identifier:column; 97, attribute; 98, argument_list; 99, identifier:pd; 100, identifier:concat; 101, list; 102, identifier:pd; 103, identifier:factorize; 104, identifier:vs; 105, keyword_argument; 106, identifier:labels; 107, identifier:indexer; 108, attribute; 109, argument_list; 110, subscript; 111, identifier:replace; 112, unary_operator; 113, identifier:na_value; 114, subscript; 115, identifier:replace; 116, unary_operator; 117, identifier:na_value; 118, subscript; 119, subscript; 120, identifier:sort; 121, identifier:sort; 122, identifier:pd; 123, identifier:factorize; 124, subscript; 125, keyword_argument; 126, identifier:train; 127, identifier:column; 128, integer:1; 129, identifier:test; 130, identifier:column; 131, integer:1; 132, identifier:train; 133, identifier:column; 134, identifier:test; 135, identifier:column; 136, identifier:train; 137, identifier:column; 138, identifier:sort; 139, identifier:sort
code: def factorize(train, test, features, na_value=-9999, full=False, sort=True):
      """Factorize categorical features.
  
      Parameters
      ----------
      train : pd.DataFrame
      test : pd.DataFrame
      features : list
             Column names in the DataFrame to be encoded.
      na_value : int, default -9999
      full : bool, default False
          Whether use all columns from train/test or only from train.
      sort : bool, default True
          Sort by values.
  
      Returns
      -------
      train : pd.DataFrame
      test : pd.DataFrame
      """
  
      for column in features:
          if full:
              vs = pd.concat([train[column], test[column]])
              labels, indexer = pd.factorize(vs, sort=sort)
          else:
              labels, indexer = pd.factorize(train[column], sort=sort)
  
          train[column] = indexer.get_indexer(train[column])
          test[column] = indexer.get_indexer(test[column])
  
          if na_value != -1:
              train[column] = train[column].replace(-1, na_value)
              test[column] = test[column].replace(-1, na_value)
  
      return train, test