|
3 | 3 |
|
4 | 4 | https://specs.frictionlessdata.io/json-table-schema/
|
5 | 5 | """
|
| 6 | +from typing import TYPE_CHECKING, Any, Dict, Optional, cast |
6 | 7 | import warnings
|
7 | 8 |
|
8 | 9 | import pandas._libs.json as json
|
9 |
| -from pandas._typing import DtypeObj |
| 10 | +from pandas._typing import DtypeObj, FrameOrSeries, JSONSerializable |
10 | 11 |
|
11 | 12 | from pandas.core.dtypes.common import (
|
12 | 13 | is_bool_dtype,
|
|
24 | 25 | from pandas import DataFrame
|
25 | 26 | import pandas.core.common as com
|
26 | 27 |
|
| 28 | +if TYPE_CHECKING: |
| 29 | + from pandas.core.indexes.multi import MultiIndex # noqa: F401 |
| 30 | + |
27 | 31 | loads = json.loads
|
28 | 32 |
|
29 | 33 |
|
@@ -103,7 +107,10 @@ def convert_pandas_type_to_json_field(arr):
|
103 | 107 | name = "values"
|
104 | 108 | else:
|
105 | 109 | name = arr.name
|
106 |
| - field = {"name": name, "type": as_json_table_type(dtype)} |
| 110 | + field: Dict[str, JSONSerializable] = { |
| 111 | + "name": name, |
| 112 | + "type": as_json_table_type(dtype), |
| 113 | + } |
107 | 114 |
|
108 | 115 | if is_categorical_dtype(dtype):
|
109 | 116 | cats = dtype.categories
|
@@ -182,7 +189,12 @@ def convert_json_field_to_pandas_type(field):
|
182 | 189 | raise ValueError(f"Unsupported or invalid field type: {typ}")
|
183 | 190 |
|
184 | 191 |
|
185 |
| -def build_table_schema(data, index=True, primary_key=None, version=True): |
| 192 | +def build_table_schema( |
| 193 | + data: FrameOrSeries, |
| 194 | + index: bool = True, |
| 195 | + primary_key: Optional[bool] = None, |
| 196 | + version: bool = True, |
| 197 | +) -> Dict[str, JSONSerializable]: |
186 | 198 | """
|
187 | 199 | Create a Table schema from ``data``.
|
188 | 200 |
|
@@ -233,11 +245,12 @@ def build_table_schema(data, index=True, primary_key=None, version=True):
|
233 | 245 | if index is True:
|
234 | 246 | data = set_default_names(data)
|
235 | 247 |
|
236 |
| - schema = {} |
| 248 | + schema: Dict[str, Any] = {} |
237 | 249 | fields = []
|
238 | 250 |
|
239 | 251 | if index:
|
240 | 252 | if data.index.nlevels > 1:
|
| 253 | + data.index = cast("MultiIndex", data.index) |
241 | 254 | for level, name in zip(data.index.levels, data.index.names):
|
242 | 255 | new_field = convert_pandas_type_to_json_field(level)
|
243 | 256 | new_field["name"] = name
|
|
0 commit comments