ueshin commented on code in PR #36382:
URL: https://github.com/apache/spark/pull/36382#discussion_r862038192
##########
python/pyspark/pandas/groupby.py:
##########
@@ -2732,34 +2741,33 @@ def stat_function(col: Column) -> Column:
return F.percentile_approx(col, 0.5, accuracy)
return self._reduce_for_stat_function(
- stat_function, only_numeric=numeric_only, bool_to_numeric=True
+ stat_function,
+ accepted_spark_types=(NumericType, BooleanType) if numeric_only
else None,
+ bool_to_numeric=True,
)
def _reduce_for_stat_function(
self,
sfun: Callable[[Column], Column],
- only_numeric: Optional[bool] = None,
- bool_as_numeric: bool = False,
+ accepted_spark_types: Optional[Tuple[Type[DataType], ...]] = None,
bool_to_numeric: bool = False,
) -> FrameLike:
"""Apply an aggregate function `sfun` per column and reduce to a
FrameLike.
Parameters
----------
sfun : The aggregate function to apply per column
- only_numeric: If True, only numeric columns are involved
- bool_as_numeric: If True, boolean columns are seen as numeric columns
(following pandas)
+ accepted_spark_types: Accepted spark types of columns to be aggregated;
+ default None means all spark types are accepted
bool_to_numeric: If True, boolean columns are converted to numeric
columns
"""
groupkey_names = [SPARK_INDEX_NAME_FORMAT(i) for i in
range(len(self._groupkeys))]
groupkey_scols = [s.alias(name) for s, name in
zip(self._groupkeys_scols, groupkey_names)]
agg_columns = []
for psser in self._agg_columns:
- if (
- isinstance(psser.spark.data_type, NumericType)
- or (bool_as_numeric and isinstance(psser.spark.data_type,
BooleanType))
- or not only_numeric
+ if (accepted_spark_types is None) or isinstance(
+ psser.spark.data_type, accepted_spark_types
):
agg_columns.append(psser)
elif bool_to_numeric and isinstance(psser.spark.data_type,
BooleanType):
Review Comment:
We should also fix the above example to be clearer?
Now we don't need to specify `BooleanType` in `accepted_spark_types`, right?
--
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]