HyukjinKwon commented on a change in pull request #34058:
URL: https://github.com/apache/spark/pull/34058#discussion_r715277100
##########
File path: python/pyspark/pandas/typedef/typehints.py
##########
@@ -690,98 +696,145 @@ 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): # type: ignore
- # Example:
- # DataFrame[zip(pdf.columns, pdf.dtypes)]
- params = tuple(slice(name, tpe) for name, tpe in params) # type:
ignore
- if isinstance(params, Iterable):
- params = tuple(params)
- else:
- params = (params,)
+ params = _prepare_a_tuple(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_valid_slices(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 = _convert_slices_to_holders(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)
+ if _is_valid_type_tuples(index_params)
+ else index_params
+ )
+ index_params = _prepare_a_tuple(index_params)
- index_type.name = name
- if isinstance(tpe, ExtensionDtype):
- index_type.tpe = tpe
+ if _is_valid_slices(index_params):
+ # Example:
+ # DataFrame[["id": int, "A": int], [int, int]]
+ new_index_params = _convert_slices_to_holders(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 = _handle_list_of_types(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)
- ): # type: ignore
- # 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) if
_is_valid_type_tuples(data_types) else data_types
+ )
+
+ return index_types + _extract_types(data_types)
+
+ else:
# Exaxmples:
# DataFrame[float, float]
# DataFrame[pdf.dtypes]
+ return _handle_list_of_types(params, origin, is_index=False)
+
+
+def _is_valid_slices(params: Any) -> Any:
+ return all(
+ isinstance(param, slice) and param.step is None and param.stop is not
None
+ for param in params
+ )
+
+
+def _convert_slices_to_holders(params: Any, is_index: bool) -> Any:
+ # Example:
+ # params = (slice("id", int, None), slice("A", int, None))
+ new_params = []
+ for param in params:
+ new_param = _get_holder(is_index)
Review comment:
`ExtensionDtype` handling seems missing here?
--
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]