You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/09/01 11:41:58 UTC
[GitHub] [spark] zhengruifeng opened a new pull request, #37756: [SPARK-40305][PS] Implement Groupby.sem
zhengruifeng opened a new pull request, #37756:
URL: https://github.com/apache/spark/pull/37756
### What changes were proposed in this pull request?
Implement Groupby.sem
### Why are the changes needed?
to increase API coverage
### Does this PR introduce _any_ user-facing change?
yes, new API
### How was this patch tested?
added UT
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #37756:
URL: https://github.com/apache/spark/pull/37756#issuecomment-1236269753
Merged into master, thank you @xinrong-meng for reivews
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] xinrong-meng commented on a diff in pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
xinrong-meng commented on code in PR #37756:
URL: https://github.com/apache/spark/pull/37756#discussion_r961271308
##########
python/pyspark/pandas/groupby.py:
##########
@@ -827,6 +827,76 @@ def mad(self) -> FrameLike:
return self._prepare_return(DataFrame(internal))
+ def sem(self, ddof: int = 1) -> FrameLike:
+ """
+ Compute standard error of the mean of groups, excluding missing values.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ ddof : int, default 1
+ Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
+ where N represents the number of elements.
+
+ Examples
+ --------
+ >>> df = ps.DataFrame({"A": [1, 2, 1, 1], "B": [True, False, False, True],
+ ... "C": [3, None, 3, 4], "D": ["a", "b", "b", "a"]})
+
+ >>> df.groupby("A").sem()
+ B C
+ A
+ 1 0.333333 0.333333
+ 2 NaN NaN
+
+ >>> df.groupby("D").sem(ddof=1)
+ A B C
+ D
+ a 0.0 0.0 0.5
+ b 0.5 0.0 NaN
+
+ >>> df.B.groupby(df.A).sem()
+ A
+ 1 0.333333
+ 2 NaN
+ Name: B, dtype: float64
+
+ See Also
+ --------
+ pyspark.pandas.Series.sem
+ pyspark.pandas.DataFrame.sem
+ """
+ if ddof not in [0, 1]:
+ raise TypeError("ddof must be 0 or 1")
+
+ # Raise the TypeError when all aggregation columns are of unaccepted data types
+ all_unaccepted = True
+ for _agg_col in self._agg_columns:
+ if isinstance(_agg_col.spark.data_type, (NumericType, BooleanType)):
+ all_unaccepted = False
+ break
+ if all_unaccepted:
Review Comment:
nit:
```
any_accepted = any(isinstance(_agg_col.spark.data_type, (NumericType, BooleanType)) for _agg_col in self._agg_columns)
if not any_accepted:
raise TypeError("...")
```
?
Feel free to keep the existing approach if you prefer :)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] xinrong-meng commented on a diff in pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
xinrong-meng commented on code in PR #37756:
URL: https://github.com/apache/spark/pull/37756#discussion_r961275433
##########
python/pyspark/pandas/tests/test_groupby.py:
##########
@@ -3055,6 +3065,17 @@ def test_ddof(self):
psdf.groupby("a")["b"].var(ddof=ddof).sort_index(),
check_exact=False,
)
+ # var
Review Comment:
nit: `# sem`
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] xinrong-meng commented on a diff in pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
xinrong-meng commented on code in PR #37756:
URL: https://github.com/apache/spark/pull/37756#discussion_r961275185
##########
python/pyspark/pandas/generic.py:
##########
@@ -2189,7 +2189,7 @@ def std(psser: "Series") -> Column:
return F.stddev_samp(spark_column)
def sem(psser: "Series") -> Column:
- return std(psser) / pow(Frame._count_expr(psser), 0.5)
+ return std(psser) / F.sqrt(Frame._count_expr(psser))
Review Comment:
Nice!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng closed pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
zhengruifeng closed pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
URL: https://github.com/apache/spark/pull/37756
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] itholic commented on pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
itholic commented on PR #37756:
URL: https://github.com/apache/spark/pull/37756#issuecomment-1236587702
Just switched this issues (SPARK-40305) from "improvement" to "sub-task" of [Increase pandas API coverage for pandas API on Spark](https://issues.apache.org/jira/browse/SPARK-40327) in the ticket.
Please feel free to revert it if it's not a correct place.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on a diff in pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on code in PR #37756:
URL: https://github.com/apache/spark/pull/37756#discussion_r961280658
##########
python/pyspark/pandas/groupby.py:
##########
@@ -827,6 +827,76 @@ def mad(self) -> FrameLike:
return self._prepare_return(DataFrame(internal))
+ def sem(self, ddof: int = 1) -> FrameLike:
+ """
+ Compute standard error of the mean of groups, excluding missing values.
+
+ .. versionadded:: 3.4.0
+
+ Parameters
+ ----------
+ ddof : int, default 1
+ Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
+ where N represents the number of elements.
+
+ Examples
+ --------
+ >>> df = ps.DataFrame({"A": [1, 2, 1, 1], "B": [True, False, False, True],
+ ... "C": [3, None, 3, 4], "D": ["a", "b", "b", "a"]})
+
+ >>> df.groupby("A").sem()
+ B C
+ A
+ 1 0.333333 0.333333
+ 2 NaN NaN
+
+ >>> df.groupby("D").sem(ddof=1)
+ A B C
+ D
+ a 0.0 0.0 0.5
+ b 0.5 0.0 NaN
+
+ >>> df.B.groupby(df.A).sem()
+ A
+ 1 0.333333
+ 2 NaN
+ Name: B, dtype: float64
+
+ See Also
+ --------
+ pyspark.pandas.Series.sem
+ pyspark.pandas.DataFrame.sem
+ """
+ if ddof not in [0, 1]:
+ raise TypeError("ddof must be 0 or 1")
+
+ # Raise the TypeError when all aggregation columns are of unaccepted data types
+ all_unaccepted = True
+ for _agg_col in self._agg_columns:
+ if isinstance(_agg_col.spark.data_type, (NumericType, BooleanType)):
+ all_unaccepted = False
+ break
+ if all_unaccepted:
Review Comment:
nice, this is more concise. I just copied it from other places
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #37756:
URL: https://github.com/apache/spark/pull/37756#issuecomment-1235010056
cc @itholic @xinrong-meng @HyukjinKwon
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] zhengruifeng commented on pull request #37756: [SPARK-40305][PS] Implement Groupby.sem
Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on PR #37756:
URL: https://github.com/apache/spark/pull/37756#issuecomment-1236609854
> Just switched this issues ([SPARK-40305](https://issues.apache.org/jira/browse/SPARK-40305)) from "improvement" to "sub-task" of [Increase pandas API coverage for pandas API on Spark](https://issues.apache.org/jira/browse/SPARK-40327).
>
> Please feel free to revert if it's not a correct switching!
Sure, that is fine!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org