code: def test_sanitize_dataframe():
      # create a dataframe with various types
      df = pd.DataFrame(
          {
              "s": list("abcde"),
              "f": np.arange(5, dtype=float),
              "i": np.arange(5, dtype=int),
              "b": np.array([True, False, True, True, False]),
              "d": pd.date_range("2012-01-01", periods=5, freq="H"),
              "c": pd.Series(list("ababc"), dtype="category"),
              "c2": pd.Series(
                  [1, "A", 2.5, "B", None], dtype="category"
              ),
              "o": pd.Series([np.array(i) for i in range(5)]),
              "p": pd.date_range(
                  "2012-01-01", periods=5, freq="H"
              ).tz_localize("UTC"),
          }
      )
  
      # add some nulls
      df.iloc[0, df.columns.get_loc("s")] = None
      df.iloc[0, df.columns.get_loc("f")] = np.nan
      df.iloc[0, df.columns.get_loc("d")] = pd.NaT
      df.iloc[0, df.columns.get_loc("o")] = np.array(np.nan)
  
      # JSON serialize. This will fail on non-sanitized dataframes
      print(df[["s", "c2"]])
      df_clean = sanitize_dataframe(df)
      print(df_clean[["s", "c2"]])
      print(df_clean[["s", "c2"]].to_dict())
      s = json.dumps(df_clean.to_dict(orient="records"))
      print(s)
  
      # Re-construct pandas dataframe
      df2 = pd.read_json(io.StringIO(s))
  
      # Re-order the columns to match df
      df2 = df2[df.columns]
  
      # Re-apply original types
      for col in df:
          if str(df[col].dtype).startswith("datetime"):
              # astype(datetime) introduces time-zone issues:
              # to_datetime() does not.
              utc = isinstance(
                  df[col].dtype, pd.core.dtypes.dtypes.DatetimeTZDtype
              )
              df2[col] = pd.to_datetime(df2[col], utc=utc)
          else:
              df2[col] = df2[col].astype(df[col].dtype)
  
      # pandas doesn't properly recognize np.array(np.nan), so change it here
      df.iloc[0, df.columns.get_loc("o")] = np.nan
      assert df.equals(df2)
  
file_path: altair/tests/utils/test_utils.py
imports: import io
  import json
  import warnings
  import numpy
  import pandas
  import pytest
  from altair.utils import infer_vegalite_type, sanitize_dataframe
label_desc: Use image node_id 2 for calling a global function with example usage: test_sanitize_dataframe() without return types