code: def encode_categorical(
      df: pd.DataFrame,
      column_names: Union[str, Iterable[str], Hashable] = None,
      **kwargs: Any,
  ) -> pd.DataFrame:
      """Encode the specified columns with Pandas' [category dtype][cat].
  
      [cat]: http://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html
  
      It is syntactic sugar around `pd.Categorical`.
  
      This method does not mutate the original DataFrame.
  
      Simply pass a string, or a sequence of column names to `column_names`;
      alternatively, you can pass kwargs, where the keys are the column names
      and the values can either be None, `sort`, `appearance`
      or a 1-D array-like object.
  
      - None: column is cast to an unordered categorical.
      - `sort`: column is cast to an ordered categorical,
                with the order defined by the sort-order of the categories.
      - `appearance`: column is cast to an ordered categorical,
                      with the order defined by the order of appearance
                      in the original column.
      - 1d-array-like object: column is cast to an ordered categorical,
                              with the categories and order as specified
                              in the input array.
  
      `column_names` and `kwargs` parameters cannot be used at the same time.
  
      Examples:
          Using `column_names`
  
          >>> import pandas as pd
          >>> import janitor
          >>> df = pd.DataFrame({
          ...     "foo": ["b", "b", "a", "c", "b"],
          ...     "bar": range(4, 9),
          ... })
          >>> df
            foo  bar
          0   b    4
          1   b    5
          2   a    6
          3   c    7
          4   b    8
          >>> df.dtypes
          foo    object
          bar     int64
          dtype: object
          >>> enc_df = df.encode_categorical(column_names="foo")
          >>> enc_df.dtypes
          foo    category
          bar       int64
          dtype: object
          >>> enc_df["foo"].cat.categories
          Index(['a', 'b', 'c'], dtype='object')
          >>> enc_df["foo"].cat.ordered
          False
  
          Using `kwargs` to specify an ordered categorical.
  
          >>> import pandas as pd
          >>> import janitor
          >>> df = pd.DataFrame({
          ...     "foo": ["b", "b", "a", "c", "b"],
          ...     "bar": range(4, 9),
          ... })
          >>> df.dtypes
          foo    object
          bar     int64
          dtype: object
          >>> enc_df = df.encode_categorical(foo="appearance")
          >>> enc_df.dtypes
          foo    category
          bar       int64
          dtype: object
          >>> enc_df["foo"].cat.categories
          Index(['b', 'a', 'c'], dtype='object')
          >>> enc_df["foo"].cat.ordered
          True
  
      Args:
          df: A pandas DataFrame object.
          column_names: A column name or an iterable (list or tuple)
              of column names.
          **kwargs: A mapping from column name to either `None`,
              `'sort'` or `'appearance'`, or a 1-D array. This is useful
              in creating categorical columns that are ordered, or
              if the user needs to explicitly specify the categories.
  
      Raises:
          ValueError: If both `column_names` and `kwargs` are provided.
  
      Returns:
          A pandas DataFrame.
      """  # noqa: E501
  
      if all((column_names, kwargs)):
          raise ValueError(
              "Only one of `column_names` or `kwargs` can be provided."
          )
      # column_names deal with only category dtype (unordered)
      # kwargs takes care of scenarios where user wants an ordered category
      # or user supplies specific categories to create the categorical
      if column_names is not None:
          column_names = get_index_labels(
              [column_names], df, axis="columns"
          )
          dtypes = {col: "category" for col in column_names}
          return df.astype(dtypes)
  
      return _computations_as_categorical(df, **kwargs)
  
file_path: pyjanitor/janitor/functions/encode_categorical.py
imports: import warnings
  from enum import Enum
  from typing import Any, Hashable, Iterable, Union
  import numpy
  import pandas
  import pandas_flavor
  from pandas.api.types import is_list_like
  from janitor.functions.utils import get_index_labels
  from janitor.utils import check_column, deprecated_alias, find_stack_level
label_desc: Use image node_id 1 for calling a global function with example usage: encode_categorical(df, column_names) and returns: _computations_as_categorical, df