Yicong-Huang commented on code in PR #53317:
URL: https://github.com/apache/spark/pull/53317#discussion_r2591439869


##########
python/pyspark/sql/tests/pandas/test_pandas_udf_grouped_agg.py:
##########
@@ -897,6 +897,161 @@ def sum_udf(v):
                     )
                     assert_frame_equal(expected, result)
 
+    def test_iterator_grouped_agg_basic(self):
+        """
+        Test basic functionality of iterator grouped agg pandas UDF with 
Iterator[pd.Series].
+        """
+        from typing import Iterator
+
+        df = self.spark.createDataFrame(
+            [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)], ("id", "v")
+        )
+
+        @pandas_udf("double")
+        def pandas_mean_iter(it: Iterator[pd.Series]) -> float:
+            sum_val = 0.0
+            cnt = 0
+            for series in it:
+                assert isinstance(series, pd.Series)
+                sum_val += series.sum()
+                cnt += len(series)
+            return sum_val / cnt if cnt > 0 else 0.0
+
+        result = 
df.groupby("id").agg(pandas_mean_iter(df["v"]).alias("mean")).sort("id").collect()
+
+        # Expected means:
+        # Group 1: (1.0 + 2.0) / 2 = 1.5
+        # Group 2: (3.0 + 5.0 + 10.0) / 3 = 6.0
+        expected = [(1, 1.5), (2, 6.0)]
+
+        self.assertEqual(len(result), len(expected))
+        for r, (exp_id, exp_mean) in zip(result, expected):
+            self.assertEqual(r["id"], exp_id)
+            self.assertAlmostEqual(r["mean"], exp_mean, places=5)

Review Comment:
   Thanks! has simplified it.



-- 
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]

Reply via email to