code: def unionize_dataframe_categories(
      *dataframes: Any,
      column_names: Optional[Iterable[pd.CategoricalDtype]] = None,
  ) -> List[pd.DataFrame]:
      """
      Given a group of dataframes which contain some categorical columns, for
      each categorical column present, find all the possible categories across
      all the dataframes which have that column.
      Update each dataframes' corresponding column with a new categorical object
      that contains the original data
      but has labels for all the possible categories from all dataframes.
      This is useful when concatenating a list of dataframes which all have the
      same categorical columns into one dataframe.
  
      If, for a given categorical column, all input dataframes do not have at
      least one instance of all the possible categories,
      Pandas will change the output dtype of that column from `category` to
      `object`, losing out on dramatic speed gains you get from the former
      format.
  
      Examples:
          Usage example for concatenation of categorical column-containing
          dataframes:
  
          Instead of:
  
          ```python
          concatenated_df = pd.concat([df1, df2, df3], ignore_index=True)
          ```
  
          which in your case has resulted in `category` -> `object` conversion,
          use:
  
          ```python
          unionized_dataframes = unionize_dataframe_categories(df1, df2, df2)
          concatenated_df = pd.concat(unionized_dataframes, ignore_index=True)
          ```
  
      Args:
          *dataframes: The dataframes you wish to unionize the categorical
              objects for.
          column_names: If supplied, only unionize this subset of columns.
  
      Raises:
          TypeError: If any of the inputs are not pandas DataFrames.
  
      Returns:
          A list of the category-unioned dataframes in the same order they
              were provided.
      """
  
      if any(not isinstance(df, pd.DataFrame) for df in dataframes):
          raise TypeError("Inputs must all be dataframes.")
  
      if column_names is None:
          # Find all columns across all dataframes that are categorical
  
          column_names = set()
  
          for dataframe in dataframes:
              column_names = column_names.union(
                  [
                      column_name
                      for column_name in dataframe.columns
                      if isinstance(
                          dataframe[column_name].dtype,
                          pd.CategoricalDtype,
                      )
                  ]
              )
  
      else:
          column_names = [column_names]
      # For each categorical column, find all possible values across the DFs
  
      category_unions = {
          column_name: union_categoricals(
              [
                  df[column_name]
                  for df in dataframes
                  if column_name in df.columns
              ]
          )
          for column_name in column_names
      }
  
      # Make a shallow copy of all DFs and modify the categorical columns
      # such that they can encode the union of all possible categories for each.
  
      refactored_dfs = []
  
      for df in dataframes:
          df = df.copy(deep=False)
  
          for column_name, categorical in category_unions.items():
              if column_name in df.columns:
                  df[column_name] = pd.Categorical(
                      df[column_name], categories=categorical.categories
                  )
  
          refactored_dfs.append(df)
  
      return refactored_dfs
  
imports: from __future__ import annotations
  import fnmatch
  import inspect
  import re
  import warnings
  from collections.abc import Callable
  from dataclasses import dataclass
  from enum import Enum
  from functools import singledispatch
  from typing import Any, Callable, Hashable, Iterable, List, Literal, Optional, Pattern, Union
  import numpy
  import pandas
  from multipledispatch import dispatch
  from pandas.api.types import is_bool_dtype, is_datetime64_dtype, is_list_like, is_scalar, is_string_dtype, union_categoricals
  from pandas.core.common import is_bool_indexer
  from pandas.core.groupby.generic import DataFrameGroupBy, SeriesGroupBy
  from janitor.utils import _expand_grid, check, check_column, find_stack_level
label_desc: Use image node_id 1 for calling a global function with example usage: unionize_dataframe_categories() and returns: refactored_dfs