HyukjinKwon commented on a change in pull request #34176:
URL: https://github.com/apache/spark/pull/34176#discussion_r721862446
##########
File path: python/pyspark/pandas/typedef/typehints.py
##########
@@ -696,98 +702,160 @@ def create_tuple_for_frame_type(params: Any) -> object:
Typing data columns with an index:
>>> ps.DataFrame[int, [int, int]] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, int, int]
+ typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[pdf.index.dtype, pdf.dtypes] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, numpy.int64]
+ typing.Tuple[...IndexNameType, ...NameType]
>>> ps.DataFrame[("index", int), [("id", int), ("A", int)]] #
doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
... # doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType]
+
+ Typing data columns with an Multi-index:
+ >>> arrays = [[1, 1, 2], ['red', 'blue', 'red']]
+ >>> idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
+ >>> pdf = pd.DataFrame({'a': range(3)}, index=idx)
+ >>> ps.DataFrame[[int, int], [int, int]] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[pdf.index.dtypes, pdf.dtypes] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
+ >>> ps.DataFrame[[("index-1", int), ("index-2", int)], [("id", int),
("A", int)]]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[zip(pdf.index.names, pdf.index.dtypes),
zip(pdf.columns, pdf.dtypes)]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
"""
- return Tuple[extract_types(params)]
+ return Tuple[_extract_types(params)]
-# TODO(SPARK-36708): numpy.typing (numpy 1.21+) support for nested types.
-def extract_types(params: Any) -> Tuple:
+def _extract_types(params: Any) -> Tuple:
origin = params
- if isinstance(params, zip):
- # Example:
- # DataFrame[zip(pdf.columns, pdf.dtypes)]
- params = tuple(slice(name, tpe) for name, tpe in params) # type:
ignore[misc, has-type]
- if isinstance(params, Iterable):
- params = tuple(params)
- else:
- params = (params,)
+ params = _to_tuple_of_params(params)
- if all(
- isinstance(param, slice)
- and param.start is not None
- and param.step is None
- and param.stop is not None
- for param in params
- ):
+ if _is_named_params(params):
# Example:
# DataFrame["id": int, "A": int]
- new_params = []
- for param in params:
- new_param = type("NameType", (NameTypeHolder,), {}) # type:
Type[NameTypeHolder]
- new_param.name = param.start
- # When the given argument is a numpy's dtype instance.
- new_param.tpe = param.stop.type if isinstance(param.stop,
np.dtype) else param.stop
- new_params.append(new_param)
-
+ new_params = _address_named_type_hoders(params, is_index=False)
return tuple(new_params)
elif len(params) == 2 and isinstance(params[1], (zip, list, pd.Series)):
# Example:
# DataFrame[int, [int, int]]
# DataFrame[pdf.index.dtype, pdf.dtypes]
# DataFrame[("index", int), [("id", int), ("A", int)]]
# DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
+ #
+ # DataFrame[[int, int], [int, int]]
+ # DataFrame[pdf.index.dtypes, pdf.dtypes]
+ # DataFrame[[("index", int), ("index-2", int)], [("id", int), ("A",
int)]]
+ # DataFrame[zip(pdf.index.names, pdf.index.dtypes), zip(pdf.columns,
pdf.dtypes)]
- index_param = params[0]
- index_type = type(
- "IndexNameType", (IndexNameTypeHolder,), {}
- ) # type: Type[IndexNameTypeHolder]
- if isinstance(index_param, tuple):
- if len(index_param) != 2:
- raise TypeError(
- "Type hints for index should be specified as "
- "DataFrame[('name', type), ...]; however, got %s" %
index_param
- )
- name, tpe = index_param
- else:
- name, tpe = None, index_param
+ index_params = params[0]
+
+ if isinstance(index_params, tuple) and len(index_params) == 2:
+ index_params = tuple([slice(*index_params)])
+
+ index_params = _convert_tuples_to_zip(index_params)
+ index_params = _to_tuple_of_params(index_params)
- index_type.name = name
- if isinstance(tpe, ExtensionDtype):
- index_type.tpe = tpe
+ if _is_named_params(index_params):
+ # Example:
+ # DataFrame[[("id", int), ("A", int)], [int, int]]
+ new_index_params = _address_named_type_hoders(index_params,
is_index=True)
+ index_types = tuple(new_index_params)
else:
- index_type.tpe = tpe.type if isinstance(tpe, np.dtype) else tpe
+ # Exaxmples:
+ # DataFrame[[float, float], [int, int]]
+ # DataFrame[pdf.dtypes, [int, int]]
+ index_types = _address_unnamed_type_holders(index_params, origin,
is_index=True)
data_types = params[1]
- if (
- isinstance(data_types, list)
- and len(data_types) >= 1
- and isinstance(data_types[0], tuple)
- ):
- # Example:
- # DataFrame[("index", int), [("id", int), ("A", int)]]
- data_types = zip((name for name, _ in data_types), (tpe for _, tpe
in data_types))
- return (index_type,) + extract_types(data_types)
- elif all(not isinstance(param, slice) and not isinstance(param, Iterable)
for param in params):
+ data_types = _convert_tuples_to_zip(data_types)
+
+ return index_types + _extract_types(data_types)
+
+ else:
# Exaxmples:
# DataFrame[float, float]
# DataFrame[pdf.dtypes]
+ return _address_unnamed_type_holders(params, origin, is_index=False)
+
+
+def _is_named_params(params: Any) -> Any:
+ return all(
+ isinstance(param, slice) and param.step is None and param.stop is not
None
+ for param in params
+ )
+
+
+def _address_named_type_hoders(params: Any, is_index: bool) -> Any:
+ # Example:
+ # params = (slice("id", int, None), slice("A", int, None))
+ new_params = []
+ for param in params:
Review comment:
can we add an assert on the type of `param` and throw an exception like
`_address_unnamed_type_holders`?
##########
File path: python/pyspark/pandas/typedef/typehints.py
##########
@@ -696,98 +702,160 @@ def create_tuple_for_frame_type(params: Any) -> object:
Typing data columns with an index:
>>> ps.DataFrame[int, [int, int]] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, int, int]
+ typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[pdf.index.dtype, pdf.dtypes] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, numpy.int64]
+ typing.Tuple[...IndexNameType, ...NameType]
>>> ps.DataFrame[("index", int), [("id", int), ("A", int)]] #
doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
... # doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType]
+
+ Typing data columns with an Multi-index:
+ >>> arrays = [[1, 1, 2], ['red', 'blue', 'red']]
+ >>> idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
+ >>> pdf = pd.DataFrame({'a': range(3)}, index=idx)
+ >>> ps.DataFrame[[int, int], [int, int]] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[pdf.index.dtypes, pdf.dtypes] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
+ >>> ps.DataFrame[[("index-1", int), ("index-2", int)], [("id", int),
("A", int)]]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[zip(pdf.index.names, pdf.index.dtypes),
zip(pdf.columns, pdf.dtypes)]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
"""
- return Tuple[extract_types(params)]
+ return Tuple[_extract_types(params)]
-# TODO(SPARK-36708): numpy.typing (numpy 1.21+) support for nested types.
-def extract_types(params: Any) -> Tuple:
+def _extract_types(params: Any) -> Tuple:
origin = params
- if isinstance(params, zip):
- # Example:
- # DataFrame[zip(pdf.columns, pdf.dtypes)]
- params = tuple(slice(name, tpe) for name, tpe in params) # type:
ignore[misc, has-type]
- if isinstance(params, Iterable):
- params = tuple(params)
- else:
- params = (params,)
+ params = _to_tuple_of_params(params)
- if all(
- isinstance(param, slice)
- and param.start is not None
- and param.step is None
- and param.stop is not None
- for param in params
- ):
+ if _is_named_params(params):
# Example:
# DataFrame["id": int, "A": int]
- new_params = []
- for param in params:
- new_param = type("NameType", (NameTypeHolder,), {}) # type:
Type[NameTypeHolder]
- new_param.name = param.start
- # When the given argument is a numpy's dtype instance.
- new_param.tpe = param.stop.type if isinstance(param.stop,
np.dtype) else param.stop
- new_params.append(new_param)
-
+ new_params = _address_named_type_hoders(params, is_index=False)
return tuple(new_params)
elif len(params) == 2 and isinstance(params[1], (zip, list, pd.Series)):
# Example:
# DataFrame[int, [int, int]]
# DataFrame[pdf.index.dtype, pdf.dtypes]
# DataFrame[("index", int), [("id", int), ("A", int)]]
# DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
+ #
+ # DataFrame[[int, int], [int, int]]
+ # DataFrame[pdf.index.dtypes, pdf.dtypes]
+ # DataFrame[[("index", int), ("index-2", int)], [("id", int), ("A",
int)]]
+ # DataFrame[zip(pdf.index.names, pdf.index.dtypes), zip(pdf.columns,
pdf.dtypes)]
- index_param = params[0]
- index_type = type(
- "IndexNameType", (IndexNameTypeHolder,), {}
- ) # type: Type[IndexNameTypeHolder]
- if isinstance(index_param, tuple):
- if len(index_param) != 2:
- raise TypeError(
- "Type hints for index should be specified as "
- "DataFrame[('name', type), ...]; however, got %s" %
index_param
- )
- name, tpe = index_param
- else:
- name, tpe = None, index_param
+ index_params = params[0]
+
+ if isinstance(index_params, tuple) and len(index_params) == 2:
+ index_params = tuple([slice(*index_params)])
+
+ index_params = _convert_tuples_to_zip(index_params)
+ index_params = _to_tuple_of_params(index_params)
- index_type.name = name
- if isinstance(tpe, ExtensionDtype):
- index_type.tpe = tpe
+ if _is_named_params(index_params):
+ # Example:
+ # DataFrame[[("id", int), ("A", int)], [int, int]]
+ new_index_params = _address_named_type_hoders(index_params,
is_index=True)
+ index_types = tuple(new_index_params)
else:
- index_type.tpe = tpe.type if isinstance(tpe, np.dtype) else tpe
+ # Exaxmples:
+ # DataFrame[[float, float], [int, int]]
+ # DataFrame[pdf.dtypes, [int, int]]
+ index_types = _address_unnamed_type_holders(index_params, origin,
is_index=True)
data_types = params[1]
- if (
- isinstance(data_types, list)
- and len(data_types) >= 1
- and isinstance(data_types[0], tuple)
- ):
- # Example:
- # DataFrame[("index", int), [("id", int), ("A", int)]]
- data_types = zip((name for name, _ in data_types), (tpe for _, tpe
in data_types))
- return (index_type,) + extract_types(data_types)
- elif all(not isinstance(param, slice) and not isinstance(param, Iterable)
for param in params):
+ data_types = _convert_tuples_to_zip(data_types)
+
+ return index_types + _extract_types(data_types)
+
+ else:
# Exaxmples:
# DataFrame[float, float]
# DataFrame[pdf.dtypes]
+ return _address_unnamed_type_holders(params, origin, is_index=False)
+
+
+def _is_named_params(params: Any) -> Any:
+ return all(
+ isinstance(param, slice) and param.step is None and param.stop is not
None
+ for param in params
+ )
+
+
+def _address_named_type_hoders(params: Any, is_index: bool) -> Any:
Review comment:
Can we pass `IndexNameTypeHolder` or `NameTypeHolder` explicitly instead
of the bool `is_index`?
##########
File path: python/pyspark/pandas/typedef/typehints.py
##########
@@ -696,98 +702,160 @@ def create_tuple_for_frame_type(params: Any) -> object:
Typing data columns with an index:
>>> ps.DataFrame[int, [int, int]] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, int, int]
+ typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[pdf.index.dtype, pdf.dtypes] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, numpy.int64]
+ typing.Tuple[...IndexNameType, ...NameType]
>>> ps.DataFrame[("index", int), [("id", int), ("A", int)]] #
doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
... # doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType]
+
+ Typing data columns with an Multi-index:
+ >>> arrays = [[1, 1, 2], ['red', 'blue', 'red']]
+ >>> idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
+ >>> pdf = pd.DataFrame({'a': range(3)}, index=idx)
+ >>> ps.DataFrame[[int, int], [int, int]] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[pdf.index.dtypes, pdf.dtypes] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
+ >>> ps.DataFrame[[("index-1", int), ("index-2", int)], [("id", int),
("A", int)]]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[zip(pdf.index.names, pdf.index.dtypes),
zip(pdf.columns, pdf.dtypes)]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
"""
- return Tuple[extract_types(params)]
+ return Tuple[_extract_types(params)]
-# TODO(SPARK-36708): numpy.typing (numpy 1.21+) support for nested types.
-def extract_types(params: Any) -> Tuple:
+def _extract_types(params: Any) -> Tuple:
origin = params
- if isinstance(params, zip):
- # Example:
- # DataFrame[zip(pdf.columns, pdf.dtypes)]
- params = tuple(slice(name, tpe) for name, tpe in params) # type:
ignore[misc, has-type]
- if isinstance(params, Iterable):
- params = tuple(params)
- else:
- params = (params,)
+ params = _to_tuple_of_params(params)
- if all(
- isinstance(param, slice)
- and param.start is not None
- and param.step is None
- and param.stop is not None
- for param in params
- ):
+ if _is_named_params(params):
# Example:
# DataFrame["id": int, "A": int]
- new_params = []
- for param in params:
- new_param = type("NameType", (NameTypeHolder,), {}) # type:
Type[NameTypeHolder]
- new_param.name = param.start
- # When the given argument is a numpy's dtype instance.
- new_param.tpe = param.stop.type if isinstance(param.stop,
np.dtype) else param.stop
- new_params.append(new_param)
-
+ new_params = _address_named_type_hoders(params, is_index=False)
return tuple(new_params)
elif len(params) == 2 and isinstance(params[1], (zip, list, pd.Series)):
# Example:
# DataFrame[int, [int, int]]
# DataFrame[pdf.index.dtype, pdf.dtypes]
# DataFrame[("index", int), [("id", int), ("A", int)]]
# DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
+ #
+ # DataFrame[[int, int], [int, int]]
+ # DataFrame[pdf.index.dtypes, pdf.dtypes]
+ # DataFrame[[("index", int), ("index-2", int)], [("id", int), ("A",
int)]]
+ # DataFrame[zip(pdf.index.names, pdf.index.dtypes), zip(pdf.columns,
pdf.dtypes)]
- index_param = params[0]
- index_type = type(
- "IndexNameType", (IndexNameTypeHolder,), {}
- ) # type: Type[IndexNameTypeHolder]
- if isinstance(index_param, tuple):
- if len(index_param) != 2:
- raise TypeError(
- "Type hints for index should be specified as "
- "DataFrame[('name', type), ...]; however, got %s" %
index_param
- )
- name, tpe = index_param
- else:
- name, tpe = None, index_param
+ index_params = params[0]
+
+ if isinstance(index_params, tuple) and len(index_params) == 2:
+ index_params = tuple([slice(*index_params)])
+
+ index_params = _convert_tuples_to_zip(index_params)
+ index_params = _to_tuple_of_params(index_params)
- index_type.name = name
- if isinstance(tpe, ExtensionDtype):
- index_type.tpe = tpe
+ if _is_named_params(index_params):
+ # Example:
+ # DataFrame[[("id", int), ("A", int)], [int, int]]
+ new_index_params = _address_named_type_hoders(index_params,
is_index=True)
+ index_types = tuple(new_index_params)
else:
- index_type.tpe = tpe.type if isinstance(tpe, np.dtype) else tpe
+ # Exaxmples:
+ # DataFrame[[float, float], [int, int]]
+ # DataFrame[pdf.dtypes, [int, int]]
+ index_types = _address_unnamed_type_holders(index_params, origin,
is_index=True)
data_types = params[1]
- if (
- isinstance(data_types, list)
- and len(data_types) >= 1
- and isinstance(data_types[0], tuple)
- ):
- # Example:
- # DataFrame[("index", int), [("id", int), ("A", int)]]
- data_types = zip((name for name, _ in data_types), (tpe for _, tpe
in data_types))
- return (index_type,) + extract_types(data_types)
- elif all(not isinstance(param, slice) and not isinstance(param, Iterable)
for param in params):
+ data_types = _convert_tuples_to_zip(data_types)
+
+ return index_types + _extract_types(data_types)
+
+ else:
# Exaxmples:
# DataFrame[float, float]
# DataFrame[pdf.dtypes]
+ return _address_unnamed_type_holders(params, origin, is_index=False)
+
+
+def _is_named_params(params: Any) -> Any:
+ return all(
+ isinstance(param, slice) and param.step is None and param.stop is not
None
+ for param in params
+ )
+
+
+def _address_named_type_hoders(params: Any, is_index: bool) -> Any:
+ # Example:
+ # params = (slice("id", int, None), slice("A", int, None))
+ new_params = []
+ for param in params:
+ new_param = (
+ type("IndexNameType", (IndexNameTypeHolder,), {})
+ if is_index
+ else type("NameType", (NameTypeHolder,), {})
+ ) # type: Union[Type[IndexNameTypeHolder], Type[NameTypeHolder]]
+ new_param.name = param.start
+ if isinstance(param.stop, ExtensionDtype):
+ new_param.tpe = param.stop
+ else:
+ # When the given argument is a numpy's dtype instance.
+ new_param.tpe = param.stop.type if isinstance(param.stop,
np.dtype) else param.stop
+ new_params.append(new_param)
+ return new_params
+
+
+def _to_tuple_of_params(params: Any) -> Any:
+ """
+ >>> _to_tuple_of_params(int)
+ (<class 'int'>,)
+
+ >>> _to_tuple_of_params([int, int, int])
+ (<class 'int'>, <class 'int'>, <class 'int'>)
+
+ >>> arrays = [[1, 1, 2], ['red', 'blue', 'red']]
+ >>> idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
+ >>> pdf = pd.DataFrame([[1, 2], [2, 3], [4, 5]], index=idx, columns=["a",
"b"])
+
+ >>> _to_tuple_of_params(zip(pdf.columns, pdf.dtypes))
+ (slice('a', dtype('int64'), None), slice('b', dtype('int64'), None))
+ >>> _to_tuple_of_params(zip(pdf.index.names, pdf.index.dtypes))
+ (slice('number', dtype('int64'), None), slice('color', dtype('O'), None))
+ """
+ if isinstance(params, zip):
+ params = tuple(slice(name, tpe) for name, tpe in params) # type:
ignore[misc, has-type]
+
+ if isinstance(params, Iterable):
+ params = tuple(params)
+ else:
+ params = (params,)
+ return params
+
+
+def _convert_tuples_to_zip(params: Any) -> Any:
+ if isinstance(params, list) and len(params) >= 1 and isinstance(params[0],
tuple):
+ return zip((name for name, _ in params), (tpe for _, tpe in params))
+ return params
+
+
+def _address_unnamed_type_holders(params: Any, origin: Any, is_index: bool) ->
Any:
Review comment:
```suggestion
def _convert_unnamed_params_to_holders(params: Any, origin: Any, is_index:
bool) -> Any:
```
##########
File path: python/pyspark/pandas/typedef/typehints.py
##########
@@ -696,98 +702,160 @@ def create_tuple_for_frame_type(params: Any) -> object:
Typing data columns with an index:
>>> ps.DataFrame[int, [int, int]] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, int, int]
+ typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[pdf.index.dtype, pdf.dtypes] # doctest: +ELLIPSIS
- typing.Tuple[...IndexNameType, numpy.int64]
+ typing.Tuple[...IndexNameType, ...NameType]
>>> ps.DataFrame[("index", int), [("id", int), ("A", int)]] #
doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType, ...NameType]
>>> ps.DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
... # doctest: +ELLIPSIS
typing.Tuple[...IndexNameType, ...NameType]
+
+ Typing data columns with an Multi-index:
+ >>> arrays = [[1, 1, 2], ['red', 'blue', 'red']]
+ >>> idx = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
+ >>> pdf = pd.DataFrame({'a': range(3)}, index=idx)
+ >>> ps.DataFrame[[int, int], [int, int]] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[pdf.index.dtypes, pdf.dtypes] # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
+ >>> ps.DataFrame[[("index-1", int), ("index-2", int)], [("id", int),
("A", int)]]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...IndexNameType, ...NameType,
...NameType]
+ >>> ps.DataFrame[zip(pdf.index.names, pdf.index.dtypes),
zip(pdf.columns, pdf.dtypes)]
+ ... # doctest: +ELLIPSIS
+ typing.Tuple[...IndexNameType, ...NameType]
"""
- return Tuple[extract_types(params)]
+ return Tuple[_extract_types(params)]
-# TODO(SPARK-36708): numpy.typing (numpy 1.21+) support for nested types.
-def extract_types(params: Any) -> Tuple:
+def _extract_types(params: Any) -> Tuple:
origin = params
- if isinstance(params, zip):
- # Example:
- # DataFrame[zip(pdf.columns, pdf.dtypes)]
- params = tuple(slice(name, tpe) for name, tpe in params) # type:
ignore[misc, has-type]
- if isinstance(params, Iterable):
- params = tuple(params)
- else:
- params = (params,)
+ params = _to_tuple_of_params(params)
- if all(
- isinstance(param, slice)
- and param.start is not None
- and param.step is None
- and param.stop is not None
- for param in params
- ):
+ if _is_named_params(params):
# Example:
# DataFrame["id": int, "A": int]
- new_params = []
- for param in params:
- new_param = type("NameType", (NameTypeHolder,), {}) # type:
Type[NameTypeHolder]
- new_param.name = param.start
- # When the given argument is a numpy's dtype instance.
- new_param.tpe = param.stop.type if isinstance(param.stop,
np.dtype) else param.stop
- new_params.append(new_param)
-
+ new_params = _address_named_type_hoders(params, is_index=False)
return tuple(new_params)
elif len(params) == 2 and isinstance(params[1], (zip, list, pd.Series)):
# Example:
# DataFrame[int, [int, int]]
# DataFrame[pdf.index.dtype, pdf.dtypes]
# DataFrame[("index", int), [("id", int), ("A", int)]]
# DataFrame[(pdf.index.name, pdf.index.dtype), zip(pdf.columns,
pdf.dtypes)]
+ #
+ # DataFrame[[int, int], [int, int]]
+ # DataFrame[pdf.index.dtypes, pdf.dtypes]
+ # DataFrame[[("index", int), ("index-2", int)], [("id", int), ("A",
int)]]
+ # DataFrame[zip(pdf.index.names, pdf.index.dtypes), zip(pdf.columns,
pdf.dtypes)]
- index_param = params[0]
- index_type = type(
- "IndexNameType", (IndexNameTypeHolder,), {}
- ) # type: Type[IndexNameTypeHolder]
- if isinstance(index_param, tuple):
- if len(index_param) != 2:
- raise TypeError(
- "Type hints for index should be specified as "
- "DataFrame[('name', type), ...]; however, got %s" %
index_param
- )
- name, tpe = index_param
- else:
- name, tpe = None, index_param
+ index_params = params[0]
+
+ if isinstance(index_params, tuple) and len(index_params) == 2:
+ index_params = tuple([slice(*index_params)])
+
+ index_params = _convert_tuples_to_zip(index_params)
+ index_params = _to_tuple_of_params(index_params)
- index_type.name = name
- if isinstance(tpe, ExtensionDtype):
- index_type.tpe = tpe
+ if _is_named_params(index_params):
+ # Example:
+ # DataFrame[[("id", int), ("A", int)], [int, int]]
+ new_index_params = _address_named_type_hoders(index_params,
is_index=True)
+ index_types = tuple(new_index_params)
else:
- index_type.tpe = tpe.type if isinstance(tpe, np.dtype) else tpe
+ # Exaxmples:
+ # DataFrame[[float, float], [int, int]]
+ # DataFrame[pdf.dtypes, [int, int]]
+ index_types = _address_unnamed_type_holders(index_params, origin,
is_index=True)
data_types = params[1]
- if (
- isinstance(data_types, list)
- and len(data_types) >= 1
- and isinstance(data_types[0], tuple)
- ):
- # Example:
- # DataFrame[("index", int), [("id", int), ("A", int)]]
- data_types = zip((name for name, _ in data_types), (tpe for _, tpe
in data_types))
- return (index_type,) + extract_types(data_types)
- elif all(not isinstance(param, slice) and not isinstance(param, Iterable)
for param in params):
+ data_types = _convert_tuples_to_zip(data_types)
+
+ return index_types + _extract_types(data_types)
+
+ else:
# Exaxmples:
# DataFrame[float, float]
# DataFrame[pdf.dtypes]
+ return _address_unnamed_type_holders(params, origin, is_index=False)
+
+
+def _is_named_params(params: Any) -> Any:
+ return all(
+ isinstance(param, slice) and param.step is None and param.stop is not
None
+ for param in params
+ )
+
+
+def _address_named_type_hoders(params: Any, is_index: bool) -> Any:
Review comment:
```suggestion
def _convert_named_params_to_holders(params: Any, is_index: bool) -> Any:
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]