code: def union_categoricals(
      to_union,
      sort_categories: bool = False,
      ignore_order: bool = False,
  ) -> Categorical:
      """
      Combine list-like of Categorical-like, unioning categories.
  
      All categories must have the same dtype.
  
      Parameters
      ----------
      to_union : list-like
          Categorical, CategoricalIndex, or Series with dtype='category'.
      sort_categories : bool, default False
          If true, resulting categories will be lexsorted, otherwise
          they will be ordered as they appear in the data.
      ignore_order : bool, default False
          If true, the ordered attribute of the Categoricals will be ignored.
          Results in an unordered categorical.
  
      Returns
      -------
      Categorical
  
      Raises
      ------
      TypeError
          - all inputs do not have the same dtype
          - all inputs do not have the same ordered property
          - all inputs are ordered and their categories are not identical
          - sort_categories=True and Categoricals are ordered
      ValueError
          Empty list of categoricals passed
  
      Notes
      -----
      To learn more about categories, see `link
      <https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html#unioning>`__
  
      Examples
      --------
      If you want to combine categoricals that do not necessarily have
      the same categories, `union_categoricals` will combine a list-like
      of categoricals. The new categories will be the union of the
      categories being combined.
  
      >>> a = pd.Categorical(["b", "c"])
      >>> b = pd.Categorical(["a", "b"])
      >>> pd.api.types.union_categoricals([a, b])
      ['b', 'c', 'a', 'b']
      Categories (3, object): ['b', 'c', 'a']
  
      By default, the resulting categories will be ordered as they appear
      in the `categories` of the data. If you want the categories to be
      lexsorted, use `sort_categories=True` argument.
  
      >>> pd.api.types.union_categoricals([a, b], sort_categories=True)
      ['b', 'c', 'a', 'b']
      Categories (3, object): ['a', 'b', 'c']
  
      `union_categoricals` also works with the case of combining two
      categoricals of the same categories and order information (e.g. what
      you could also `append` for).
  
      >>> a = pd.Categorical(["a", "b"], ordered=True)
      >>> b = pd.Categorical(["a", "b", "a"], ordered=True)
      >>> pd.api.types.union_categoricals([a, b])
      ['a', 'b', 'a', 'b', 'a']
      Categories (2, object): ['a' < 'b']
  
      Raises `TypeError` because the categories are ordered and not identical.
  
      >>> a = pd.Categorical(["a", "b"], ordered=True)
      >>> b = pd.Categorical(["a", "b", "c"], ordered=True)
      >>> pd.api.types.union_categoricals([a, b])
      Traceback (most recent call last):
          ...
      TypeError: to union ordered Categoricals, all categories must be the same
  
      Ordered categoricals with different categories or orderings can be
      combined by using the `ignore_ordered=True` argument.
  
      >>> a = pd.Categorical(["a", "b", "c"], ordered=True)
      >>> b = pd.Categorical(["c", "b", "a"], ordered=True)
      >>> pd.api.types.union_categoricals([a, b], ignore_order=True)
      ['a', 'b', 'c', 'c', 'b', 'a']
      Categories (3, object): ['a', 'b', 'c']
  
      `union_categoricals` also works with a `CategoricalIndex`, or `Series`
      containing categorical data, but note that the resulting array will
      always be a plain `Categorical`
  
      >>> a = pd.Series(["b", "c"], dtype='category')
      >>> b = pd.Series(["a", "b"], dtype='category')
      >>> pd.api.types.union_categoricals([a, b])
      ['b', 'c', 'a', 'b']
      Categories (3, object): ['b', 'c', 'a']
      """
      from pandas import Categorical
      from pandas.core.arrays.categorical import recode_for_categories
  
      if len(to_union) == 0:
          raise ValueError("No Categoricals to union")
  
      def _maybe_unwrap(x):
          if isinstance(x, (ABCCategoricalIndex, ABCSeries)):
              return x._values
          elif isinstance(x, Categorical):
              return x
          else:
              raise TypeError(
                  "all components to combine must be Categorical"
              )
  
      to_union = [_maybe_unwrap(x) for x in to_union]
      first = to_union[0]
  
      if not lib.dtypes_all_equal(
          [obj.categories.dtype for obj in to_union]
      ):
          raise TypeError("dtype of categories must be the same")
  
      ordered = False
      if all(
          first._categories_match_up_to_permutation(other)
          for other in to_union[1:]
      ):
          # identical categories - fastpath
          categories = first.categories
          ordered = first.ordered
  
          all_codes = [
              first._encode_with_my_categories(x)._codes
              for x in to_union
          ]
          new_codes = np.concatenate(all_codes)
  
          if sort_categories and not ignore_order and ordered:
              raise TypeError(
                  "Cannot use sort_categories=True with ordered Categoricals"
              )
  
          if sort_categories and not categories.is_monotonic_increasing:
              categories = categories.sort_values()
              indexer = categories.get_indexer(first.categories)
  
              from pandas.core.algorithms import take_nd
  
              new_codes = take_nd(indexer, new_codes, fill_value=-1)
      elif ignore_order or all(not c.ordered for c in to_union):
          # different categories - union and recode
          cats = first.categories.append(
              [c.categories for c in to_union[1:]]
          )
          categories = cats.unique()
          if sort_categories:
              categories = categories.sort_values()
  
          new_codes = [
              recode_for_categories(c.codes, c.categories, categories)
              for c in to_union
          ]
          new_codes = np.concatenate(new_codes)
      else:
          # ordered - to show a proper error message
          if all(c.ordered for c in to_union):
              msg = "to union ordered Categoricals, all categories must be the same"
              raise TypeError(msg)
          raise TypeError("Categorical.ordered must be the same")
  
      if ignore_order:
          ordered = False
  
      dtype = CategoricalDtype(categories=categories, ordered=ordered)
      return Categorical._simple_new(new_codes, dtype=dtype)
  
file_path: pandas/pandas/core/dtypes/concat.py
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, cast
  import warnings
  import numpy
  from pandas._libs import lib
  from pandas.util._exceptions import find_stack_level
  from pandas.core.dtypes.astype import astype_array
  from pandas.core.dtypes.cast import common_dtype_categorical_compat, find_common_type, np_find_common_type
  from pandas.core.dtypes.dtypes import CategoricalDtype
  from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCSeries
label_desc: Use image node_id 4 for calling a global function with example usage: union_categoricals(to_union, sort_categories, ignore_order) and returns: Categorical, x, x