code: def build_table_schema(
      data: DataFrame | Series,
      index: bool = True,
      primary_key: bool | None = None,
      version: bool = True,
  ) -> dict[str, JSONSerializable]:
      """
      Create a Table schema from ``data``.
  
      Parameters
      ----------
      data : Series, DataFrame
      index : bool, default True
          Whether to include ``data.index`` in the schema.
      primary_key : bool or None, default True
          Column names to designate as the primary key.
          The default `None` will set `'primaryKey'` to the index
          level or levels if the index is unique.
      version : bool, default True
          Whether to include a field `pandas_version` with the version
          of pandas that last revised the table schema. This version
          can be different from the installed pandas version.
  
      Returns
      -------
      dict
  
      Notes
      -----
      See `Table Schema
      <https://pandas.pydata.org/docs/user_guide/io.html#table-schema>`__ for
      conversion types.
      Timedeltas as converted to ISO8601 duration format with
      9 decimal places after the seconds field for nanosecond precision.
  
      Categoricals are converted to the `any` dtype, and use the `enum` field
      constraint to list the allowed values. The `ordered` attribute is included
      in an `ordered` field.
  
      Examples
      --------
      >>> from pandas.io.json._table_schema import build_table_schema
      >>> df = pd.DataFrame(
      ...     {'A': [1, 2, 3],
      ...      'B': ['a', 'b', 'c'],
      ...      'C': pd.date_range('2016-01-01', freq='d', periods=3),
      ...      }, index=pd.Index(range(3), name='idx'))
      >>> build_table_schema(df)
      {'fields': \
  [{'name': 'idx', 'type': 'integer'}, \
  {'name': 'A', 'type': 'integer'}, \
  {'name': 'B', 'type': 'string'}, \
  {'name': 'C', 'type': 'datetime'}], \
  'primaryKey': ['idx'], \
  'pandas_version': '1.4.0'}
      """
      if index is True:
          data = set_default_names(data)
  
      schema: dict[str, Any] = {}
      fields = []
  
      if index:
          if data.index.nlevels > 1:
              data.index = cast("MultiIndex", data.index)
              for level, name in zip(
                  data.index.levels, data.index.names
              ):
                  new_field = convert_pandas_type_to_json_field(level)
                  new_field["name"] = name
                  fields.append(new_field)
          else:
              fields.append(
                  convert_pandas_type_to_json_field(data.index)
              )
  
      if data.ndim > 1:
          for column, s in data.items():
              fields.append(convert_pandas_type_to_json_field(s))
      else:
          fields.append(convert_pandas_type_to_json_field(data))
  
      schema["fields"] = fields
      if index and data.index.is_unique and primary_key is None:
          if data.index.nlevels == 1:
              schema["primaryKey"] = [data.index.name]
          else:
              schema["primaryKey"] = data.index.names
      elif primary_key is not None:
          schema["primaryKey"] = primary_key
  
      if version:
          schema["pandas_version"] = TABLE_SCHEMA_VERSION
      return schema
  
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, Any, cast
  import warnings
  from pandas._libs import lib
  from pandas._libs.json import ujson_loads
  from pandas._libs.tslibs import timezones
  from pandas._libs.tslibs.dtypes import freq_to_period_freqstr
  from pandas.util._exceptions import find_stack_level
  from pandas.core.dtypes.base import _registry
  from pandas.core.dtypes.common import is_bool_dtype, is_integer_dtype, is_numeric_dtype, is_string_dtype
  from pandas.core.dtypes.dtypes import CategoricalDtype, DatetimeTZDtype, ExtensionDtype, PeriodDtype
  from pandas import DataFrame
  import pandas.core.common
  from pandas.tseries.frequencies import to_offset
label_desc: Use image node_id 5 for calling a global function with example usage: build_table_schema(data, index, primary_key, version) and returns: schema