code: def union_categoricals(to_union, sort_categories=False, ignore_order=False):
      from pandas import Index, Categorical, CategoricalIndex, Series
      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, (CategoricalIndex, Series)):
              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 all(is_dtype_equal(other.categories.dtype, first.categories.dtype)
                 for other in to_union[1:]):
          raise TypeError("dtype of categories must be the same")
      ordered = False
      if all(first.is_dtype_equal(other) for other in to_union[1:]):
          categories = first.categories
          ordered = first.ordered
          if all(first.categories.equals(other.categories)
                 for other in to_union[1:]):
              new_codes = np.concatenate([c.codes for c in to_union])
          else:
              codes = [first.codes] + [_recode_for_categories(other.codes,
                                                              other.categories,
                                                              first.categories)
                                       for other in to_union[1:]]
              new_codes = np.concatenate(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_1d
              new_codes = take_1d(indexer, new_codes, fill_value=-1)
      elif ignore_order or all(not c.ordered for c in to_union):
          cats = first.categories.append([c.categories for c in to_union[1:]])
          categories = Index(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:
          if all(c.ordered for c in to_union):
              msg = ("to union ordered Categoricals, "
                     "all categories must be the same")
              raise TypeError(msg)
          else:
              raise TypeError('Categorical.ordered must be the same')
      if ignore_order:
          ordered = False
      return Categorical(new_codes, categories=categories, ordered=ordered,
                         fastpath=True)