Skip to content

Commit b8138ba

Browse files
authored
Use __tuple_params__ instead of __args__ for Python 3.5.2 (#592)
With Python 3.5.2, Koalas DataFrame type hint is being failed: ```python import databricks.koalas as ks import numpy as np df = ks.DataFrame({'A': 'a a b'.split(), 'B': [1, 2, 3], 'C': [4, 6, 5]}, columns=['A', 'B', 'C']) g = df.groupby('A') def pandas_div_sum(x) -> ks.DataFrame[float, float]: return x[['B', 'C']] / x[['B', 'C']].sum() g.apply(pandas_div_sum) ``` ``` Traceback (most recent call last) <command-3464524554752451> in <module>() 8 def pandas_div_sum(x) -> ks.DataFrame[float, float]: 9 return x[['B', 'C']] / x[['B', 'C']].sum() ---> 10 g.apply(pandas_div_sum) /databricks/python/lib/python3.5/site-packages/databricks/koalas/usage_logging/__init__.py in wrapper(*args, **kwargs) 129 start = time.perf_counter() 130 try: --> 131 res = func(*args, **kwargs) 132 logger.log_success( 133 class_name, function_name, time.perf_counter() - start, signature) /databricks/python/lib/python3.5/site-packages/databricks/koalas/groupby.py in apply(self, func) 424 raise ValueError("Given function must have return type hint; however, not found.") 425 --> 426 return_schema = _infer_return_type(func).tpe 427 428 index_columns = self._kdf._internal.index_columns /databricks/python/lib/python3.5/site-packages/databricks/koalas/typedef.py in _infer_return_type(f, return_col, return_scalar) 406 return _Scalar(inner) 407 if return_sig is not None: --> 408 return _to_stype(return_sig) 409 assert False /databricks/python/lib/python3.5/site-packages/databricks/koalas/typedef.py in _to_stype(tpe) 85 return _Series(inner) 86 if hasattr(tpe, "__origin__") and tpe.__origin__ == ks.DataFrame: ---> 87 return _DataFrame([as_spark_type(t) for t in tpe.__args__[0].__args__]) 88 inner = as_spark_type(tpe) 89 if inner is None: AttributeError: type object 'Tuple' has no attribute '__args__' ``` See https://github.com/python/cpython/blob/v3.5.2/Lib/typing.py.
1 parent fec5d68 commit b8138ba

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

databricks/koalas/typedef.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ def _to_stype(tpe) -> X:
8484
inner = as_spark_type(tpe.__args__[0])
8585
return _Series(inner)
8686
if hasattr(tpe, "__origin__") and tpe.__origin__ == ks.DataFrame:
87-
return _DataFrame([as_spark_type(t) for t in tpe.__args__[0].__args__])
87+
tuple_type = tpe.__args__[0]
88+
if hasattr(tuple_type, "__tuple_params__"):
89+
# Python 3.5.0 to 3.5.2 has '__tuple_params__' instead.
90+
# See https://github.com/python/cpython/blob/v3.5.2/Lib/typing.py
91+
parameters = getattr(tuple_type, "__tuple_params__")
92+
else:
93+
parameters = getattr(tuple_type, "__args__")
94+
return _DataFrame([as_spark_type(t) for t in parameters])
8895
inner = as_spark_type(tpe)
8996
if inner is None:
9097
return _Unknown(tpe)

0 commit comments

Comments
 (0)