Repository navigation
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Rolling.countandRollingGroupby.countin pandas API on Spark ignoredmin_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.countalready use,row_number() >= min_periods, inRollingLike.count. A result is now produced only when the window holds at leastmin_periodsrows, whether or not they are NaN. This is what pandas does: itsRolling.countisroll_sumover thenotnamask, so only the window length is compared withmin_periods. Whenmin_periodsis not given it defaults to the window size, as in pandas.It also:
Rolling.countandRollingGroupby.countdoctests (and adds amin_periods=1example);TODO(SPARK-43432)workarounds in the tests, which compared against pandas withmin_periods=1; they now compare with pandas directly;test_rolling_count_min_periodsandtest_groupby_rolling_count_min_periods, which use NaN inputs and severalwindow/min_periodscombinations, 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 firstw - 1rows and honorsmin_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:
After (the same as pandas):
min_periodsis now honored, androlling(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_countandpyspark.pandas.tests.diff_frames_ops.test_groupby_rolling_countto compare with pandas directly, and added the twomin_periodstests described above. The doctests ofRolling.countandRollingGroupby.countare updated.The CI run on my fork passes, including the Linters job, the
pyspark-pandas,pyspark-pandas-slowandpyspark-pandas-connectjobs, thepyspark.pandas.windowdoctests and the Spark Connect parity tests of these two modules: https://github.com/duan-n2d/spark/actions/runs/44068903679I 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)