def build_table_schema(data, index=True, primary_key=None, version=True):
if index is True:<EOL><INDENT>data = set_default_names(data)<EOL><DEDENT>schema = {}<EOL>fields = []<EOL>if index:<EOL><INDENT>if data.index.nlevels > <NUM_LIT:1>:<EOL><INDENT>for level in data.index.levels:<EOL><INDENT>fields.append(convert_pandas_type_to_json_field(level))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fields.append(convert_pandas_type_to_json_field(data.index))<EOL><DEDENT><DEDENT>if data.ndim > <NUM_LIT:1>:<EOL><INDENT>for column, s in data.iteritems():<EOL><INDENT>fields.append(convert_pandas_type_to_json_field(s))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fields.append(convert_pandas_type_to_json_field(data))<EOL><DEDENT>schema['<STR_LIT>'] = fields<EOL>if index and data.index.is_unique and primary_key is None:<EOL><INDENT>if data.index.nlevels == <NUM_LIT:1>:<EOL><INDENT>schema['<STR_LIT>'] = [data.index.name]<EOL><DEDENT>else:<EOL><INDENT>schema['<STR_LIT>'] = data.index.names<EOL><DEDENT><DEDENT>elif primary_key is not None:<EOL><INDENT>schema['<STR_LIT>'] = primary_key<EOL><DEDENT>if version:<EOL><INDENT>schema['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>return schema<EOL>
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 generated the schema.

Returns
-------
schema : dict

Notes
-----
See `_as_json_table_type` 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
--------
>>> 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'}],
'pandas_version': '0.20.0',
'primaryKey': ['idx']}