code: def sanitize_dataframe(df):
      """Sanitize a DataFrame to prepare it for serialization.
  
      * Make a copy
      * Raise ValueError if it has a hierarchical index.
      * Convert categoricals to strings.
      * Convert np.bool_ dtypes to Python bool objects
      * Convert np.int dtypes to Python int objects
      * Convert floats to objects and replace NaNs/infs with None.
      * Convert DateTime dtypes into appropriate string representations
      """
      import numpy as np
      import pandas as pd
  
      df = df.copy()  # type: pd.DataFrame
  
      if isinstance(df.index, pd.core.indexes.multi.MultiIndex):
          raise ValueError("Hierarchical indices not supported")
      if isinstance(df.columns, pd.core.indexes.multi.MultiIndex):
          raise ValueError("Hierarchical indices not supported")
  
      def to_list_if_array(val):
          if isinstance(val, np.ndarray):
              return val.tolist()
          else:
              return val
  
      for col_name, dtype in df.dtypes.items():
          if str(dtype) == "category":
              # XXXX: work around bug in to_json for categorical types
              # https://github.com/pydata/pandas/issues/10778
              df[col_name] = df[col_name].astype(str)
          elif str(dtype) == "bool":
              # convert numpy bools to objects; np.bool is not JSON serializable
              df[col_name] = df[col_name].astype(object)
          elif np.issubdtype(dtype, np.integer):
              # convert integers to objects; np.int is not JSON serializable
              df[col_name] = df[col_name].astype(object)
          elif np.issubdtype(dtype, np.floating):
              # For floats, convert to Python float: np.float is not JSON serializable
              # Also convert NaN/inf values to null, as they are not JSON serializable
              col = df[col_name]
              bad_values = col.isnull() | np.isinf(col)
              df[col_name] = col.astype(object).where(~bad_values, None)
          elif str(dtype).startswith("datetime"):
              # Convert datetimes to strings
              # astype(str) will choose the appropriate resolution
              df[col_name] = df[col_name].astype(str).replace("NaT", "")
          elif dtype == object:
              # Convert numpy arrays saved as objects to lists
              # Arrays are not JSON serializable
              col = df[col_name].apply(
                  to_list_if_array, convert_dtype=False
              )
              df[col_name] = col.where(col.notnull(), None)
      return df
  
file_path: ipyvega/vega/utils.py
imports: import html
  import codecs
  import os.path
label_desc: Use image node_id 4 for calling a global function with example usage: sanitize_dataframe(df) and returns: df, val, val