duan-n2d opened a new pull request, #60614:
URL: https://github.com/apache/spark/pull/60614

   ### What changes were proposed in this pull request?
   
   `Rolling.count` and `RollingGroupby.count` in pandas API on Spark ignored 
`min_periods`: they always returned the number of non-NaN values in the 
(possibly partial) window.
   
   This PR applies the same guard that the other rolling aggregations and 
`Expanding.count` already use, `row_number() >= min_periods`, in 
`RollingLike.count`. A result is now produced only when the window holds at 
least `min_periods` rows, whether or not they are NaN. This is what pandas 
does: its `Rolling.count` is `roll_sum` over the `notna` mask, so only the 
window length is compared with `min_periods`. When `min_periods` is not given 
it defaults to the window size, as in pandas.
   
   It also:
   - updates the `Rolling.count` and `RollingGroupby.count` doctests (and adds 
a `min_periods=1` example);
   - adds an entry to the PySpark migration guide, under "Upgrading from 
PySpark 4.4 to 5.0", since the results of a public API change;
   - removes the three `TODO(SPARK-43432)` workarounds in the tests, which 
compared against pandas with `min_periods=1`; they now compare with pandas 
directly;
   - adds `test_rolling_count_min_periods` and 
`test_groupby_rolling_count_min_periods`, which use NaN inputs and several 
`window` / `min_periods` combinations, for Series, DataFrame and groupby.
   
   ### Why are the changes needed?
   
   pandas API on Spark should match pandas. The minimum supported pandas is 
2.2, where `rolling(w).count()` returns NaN for the first `w - 1` rows and 
honors `min_periods` (pandas-dev/pandas#31302). Here it did neither.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, compared with released versions. For `s = ps.Series([2, 3, 
float("nan"), 10])`:
   
   Before:
   ```
   >>> s.rolling(3).count()
   0    1.0
   1    2.0
   2    2.0
   3    2.0
   dtype: float64
   ```
   
   After (the same as pandas):
   ```
   >>> s.rolling(3).count()
   0    NaN
   1    NaN
   2    2.0
   3    2.0
   dtype: float64
   ```
   
   `min_periods` is now honored, and `rolling(w, min_periods=1).count()` gives 
the previous results. Because this changes the results of a public API, it is 
noted in the migration guide and is intended for master only.
   
   ### How was this patch tested?
   
   Updated `pyspark.pandas.tests.window.test_rolling_count`, 
`pyspark.pandas.tests.window.test_groupby_rolling_count` and 
`pyspark.pandas.tests.diff_frames_ops.test_groupby_rolling_count` to compare 
with pandas directly, and added the two `min_periods` tests described above. 
The doctests of `Rolling.count` and `RollingGroupby.count` are updated.
   
   The CI run on my fork passes, including the Linters job, the 
`pyspark-pandas`, `pyspark-pandas-slow` and `pyspark-pandas-connect` jobs, the 
`pyspark.pandas.window` doctests and the Spark Connect parity tests of these 
two modules: https://github.com/duan-n2d/spark/actions/runs/44068903679
   
   I also checked the expected values in the doctests against the examples in 
the pandas documentation of `Rolling.count`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Opus 5.5)
   


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