Skip to content

[SPARK-43432][PS] Fix min_periods for Rolling.count to work the same as pandas - #60614

Open
duan-n2d wants to merge 1 commit into
apache:masterfrom
duan-n2d:SPARK-43432-rolling-count-min-periods
Open

duan-n2d wants to merge 1 commit into
apache:masterfrom
duan-n2d:SPARK-43432-rolling-count-min-periods

Conversation

@duan-n2d

Copy link
Copy Markdown

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)

…ame as pandas

`Rolling.count` and `RollingGroupby.count` in pandas API on Spark ignored `min_periods`
and always counted the non-NaN values of the partial window. pandas produces a result only
when the window holds at least `min_periods` rows, whether or not they are NaN, and since
pandas 2.0 `min_periods` defaults to the window size.

Apply the same row-count guard that the other rolling aggregations and
`Expanding.count` already use, update the doctests and the migration guide, and
compare the tests with pandas directly instead of `min_periods=1`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant