code: def _apply_categorical_encoding_to_feature_array(
      feature_array: pa.Array,
      categorical_encoding: Dict[Any, int],
      encoding_length: int,
  ) -> List[Any]:
      """Applies the provided encoding to the feature array.
  
      For each example, the frequency of each category is computed. Using the
      categorical_encoding dict, an encoding is created for the example by storing
      these counts in the appropriate index of the encoding.
  
      Args:
        feature_array: Arrow Array.
        categorical_encoding: A dict where the key is the category and the value is
          the index in the encoding to which the category corresponds to.
        encoding_length: The length of the list containing the encoded feature
          values.
  
      Returns:
        A list containing the encoded feature values for each example.
      """
      if pa.types.is_null(feature_array.type):
          return []
      result = [None for _ in range(len(feature_array))]
      flattened, non_missing_parent_indices = array_util.flatten_nested(
          feature_array, True
      )
      non_missing_values = flattened.to_pylist()
      non_missing_parent_indices = list(non_missing_parent_indices)
      for value, index in zip(
          non_missing_values, non_missing_parent_indices
      ):
          if result[index] is None:
              result[index] = []
          result[index].append(value)
      for i in range(len(result)):
          if result[i] is None:
              result[i] = [None] * encoding_length
          else:
              category_frequencies = collections.Counter(result[i])
              encoded_values = [0] * encoding_length
              for category in category_frequencies:
                  if category in categorical_encoding:
                      encoded_values[
                          categorical_encoding[category]
                      ] = category_frequencies[category]
                  elif not pd.isnull(category):
                      encoded_values[-1] += category_frequencies[
                          category
                      ]
              result[i] = encoded_values
      return result
  
imports: import collections
  from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
  from absl import logging
  import apache_beam
  import numpy
  import pandas
  import pyarrow
  from tensorflow_data_validation import types
  from tensorflow_data_validation.statistics.generators import partitioned_stats_generator
  from tensorflow_data_validation.utils import feature_partition_util
  from tensorflow_data_validation.utils import mutual_information_util
  from tensorflow_data_validation.utils import schema_util
  from tensorflow_data_validation.utils import stats_util
  from tfx_bsl.arrow import array_util
  from tensorflow_metadata.proto.v0 import schema_pb2
  from tensorflow_metadata.proto.v0 import statistics_pb2
label_desc: Use image node_id 3 for calling a global function with example usage: _apply_categorical_encoding_to_feature_array(feature_array, categorical_encoding, encoding_length) and returns: result, list