Yikun commented on code in PR #36660:
URL: https://github.com/apache/spark/pull/36660#discussion_r887650580
##########
python/pyspark/pandas/groupby.py:
##########
@@ -753,6 +753,75 @@ def skew(self) -> FrameLike:
bool_to_numeric=True,
)
+ # TODO: 'axis', 'skipna', 'level' parameter should be implemented.
+ def mad(self) -> FrameLike:
+ """
+ Compute mean absolute deviation of groups, excluding missing values.
+
+ .. versionadded:: 3.4.0
+
+ Examples
+ --------
+ >>> df = ps.DataFrame({"A": [1, 2, 1, 1], "B": [True, False, False,
True],
+ ... "C": [3, 4, 3, 4], "D": ["a", "b", "b", "a"]})
+
+ >>> df.groupby("A").mad()
+ B C
+ A
+ 1 0.444444 0.444444
+ 2 0.000000 0.000000
+
+ >>> df.B.groupby(df.A).mad()
+ A
+ 1 0.444444
+ 2 0.000000
+ Name: B, dtype: float64
+
+ See Also
+ --------
+ pyspark.pandas.Series.groupby
+ pyspark.pandas.DataFrame.groupby
+ """
+ groupkey_names = [SPARK_INDEX_NAME_FORMAT(i) for i in
range(len(self._groupkeys))]
+ internal, agg_columns, sdf = self._prepare_reduce(
+ groupkey_names=groupkey_names,
+ accepted_spark_types=(NumericType, BooleanType),
+ bool_to_numeric=False,
+ )
+ psdf: DataFrame = DataFrame(internal)
+
+ if len(psdf._internal.column_labels) > 0:
+ window = Window.partitionBy(groupkey_names).rowsBetween(
+ Window.unboundedPreceding, Window.unboundedFollowing
+ )
+ new_agg_scols = {}
+ new_stat_scols = []
+ for agg_column in agg_columns:
+ agg_column_name =
agg_column._internal.data_spark_column_names[0]
+ new_agg_column_name =
"__tmp_agg_col_{}__".format(agg_column_name)
Review Comment:
```suggestion
new_agg_column_name = verify_temp_column_name(
psdf._internal.spark_frame,
"__tmp_agg_col_{}__".format(agg_column_name)
)
```
Maybe add a `verify_temp_column_name` check
--
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]