You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by gu...@apache.org on 2022/05/06 01:40:19 UTC
[spark] branch master updated: [SPARK-39095][PYTHON] Adjust `GroupBy.std` to match pandas 1.4
This is an automated email from the ASF dual-hosted git repository.
gurwls223 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new 2d175986906 [SPARK-39095][PYTHON] Adjust `GroupBy.std` to match pandas 1.4
2d175986906 is described below
commit 2d175986906b9ddf4b10b2b50d635b8bc07908fd
Author: Xinrong Meng <xi...@databricks.com>
AuthorDate: Fri May 6 10:40:08 2022 +0900
[SPARK-39095][PYTHON] Adjust `GroupBy.std` to match pandas 1.4
### What changes were proposed in this pull request?
Adjust `GroupBy.std` to match pandas 1.4.
Specifically, raise the TypeError when all aggregation columns are of unaccepted data types.
### Why are the changes needed?
Improve API compatibility with pandas.
### Does this PR introduce _any_ user-facing change?
Yes.
```py
>>> psdf = ps.DataFrame(
... {
... "A": [1, 2, 1, 2],
... "B": [3.1, 4.1, 4.1, 3.1],
... "C": ["a", "b", "b", "a"],
... "D": [True, False, False, True],
... }
... )
>>> psdf
A B C D
0 1 3.1 a True
1 2 4.1 b False
2 1 4.1 b False
3 2 3.1 a True
### Before
>>> psdf.groupby('A')[['C']].std()
Empty DataFrame
Columns: []
Index: [1, 2]
### After
>>> psdf.groupby('A')[['C']].std()
...
TypeError: Unaccepted data types of aggregation columns; numeric or bool expected.
```
### How was this patch tested?
Unit tests.
Closes #36444 from xinrong-databricks/groupby.std.
Authored-by: Xinrong Meng <xi...@databricks.com>
Signed-off-by: Hyukjin Kwon <gu...@apache.org>
---
python/pyspark/pandas/groupby.py | 15 +++++++++++++--
python/pyspark/pandas/tests/test_groupby.py | 19 +++++++++++++++----
2 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/python/pyspark/pandas/groupby.py b/python/pyspark/pandas/groupby.py
index 386b24c1916..20f7ec55660 100644
--- a/python/pyspark/pandas/groupby.py
+++ b/python/pyspark/pandas/groupby.py
@@ -640,6 +640,17 @@ class GroupBy(Generic[FrameLike], metaclass=ABCMeta):
"""
assert ddof in (0, 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:
+ raise TypeError(
+ "Unaccepted data types of aggregation columns; numeric or bool expected."
+ )
+
return self._reduce_for_stat_function(
F.stddev_pop if ddof == 0 else F.stddev_samp,
accepted_spark_types=(NumericType,),
@@ -2756,9 +2767,9 @@ class GroupBy(Generic[FrameLike], metaclass=ABCMeta):
Parameters
----------
- sfun : The aggregate function to apply per column
+ sfun : The aggregate function to apply per column.
accepted_spark_types: Accepted spark types of columns to be aggregated;
- default None means all spark types are accepted
+ default None means all spark types are accepted.
bool_to_numeric: If True, boolean columns are converted to numeric columns, which
are accepted for all statistical functions regardless of
`accepted_spark_types`.
diff --git a/python/pyspark/pandas/tests/test_groupby.py b/python/pyspark/pandas/tests/test_groupby.py
index f645373eb3c..33f24a5e2be 100644
--- a/python/pyspark/pandas/tests/test_groupby.py
+++ b/python/pyspark/pandas/tests/test_groupby.py
@@ -1286,13 +1286,24 @@ class GroupByTest(PandasOnSparkTestCase, TestUtils):
ps.DataFrame({"B": [3.1, 3.1], "D": [0, 0]}, index=pd.Index([1, 2], name="A")),
)
- # TODO: fix bug of `std` and re-enable the test below
- # self._test_stat_func(lambda groupby_obj: groupby_obj.std(), check_exact=False)
- self.assert_eq(psdf.groupby("A").std(), pdf.groupby("A").std(), check_exact=False)
+ with self.assertRaisesRegex(
+ TypeError, "Unaccepted data types of aggregation columns; numeric or bool expected."
+ ):
+ psdf.groupby("A")[["C"]].std()
+
+ self.assert_eq(
+ psdf.groupby("A").std().sort_index(),
+ pdf.groupby("A").std().sort_index(),
+ check_exact=False,
+ )
# TODO: fix bug of `sum` and re-enable the test below
# self._test_stat_func(lambda groupby_obj: groupby_obj.sum(), check_exact=False)
- self.assert_eq(psdf.groupby("A").sum(), pdf.groupby("A").sum(), check_exact=False)
+ self.assert_eq(
+ psdf.groupby("A").sum().sort_index(),
+ pdf.groupby("A").sum().sort_index(),
+ check_exact=False,
+ )
def test_min(self):
self._test_stat_func(lambda groupby_obj: groupby_obj.min())
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org