Skip to content

[Python] Fix DataFrame var, std, skew and kurtosis with skipna=False - #41874

Open
anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-moments-skipna-false
Open

anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-moments-skipna-false

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

Fixes #41873

var, skew and kurtosis compute partition moments with pandas std, sum and mean, which skip NaN, while the row count includes it. With skipna=True the NaN rows are dropped first, but with skipna=False a NaN that shares a partition with other values is ignored, so the result is a number where pandas gives NaN. With skipna=False these methods now return NaN when the Series has any NaN, and otherwise the same result as before. std and kurt go through var and kurtosis, and DataFrame.var/std(skipna=False) through the Series methods, so they are fixed too.

Added test_moments_with_na_and_skipna_false to frames_test.py (40 rows, so every NaN shares a test partition with other values). It fails on master. The apache_beam/dataframe tests and the Series, DataFrame, NDFrame and GroupBy pandas doctests pass.

@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Dec 2, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR fixes DeferredSeries.var (and transitively DeferredSeries.std, DeferredDataFrame.var, and DeferredDataFrame.std) to properly respect skipna=False by checking self.hasnans and returning NaN when any NaN values are present. The implementation is clean, consistent with existing moment aggregations (skew and kurtosis) in frames.py, and well-covered by unit tests.

🔍 General Feedback

  • Reusing the self.hasnans + result.apply(..., other=[has_na], name='handle_na') pattern from skew and kurtosis keeps the moment aggregation implementations uniform across DeferredSeries.
  • The unit tests in frames_test.py thoroughly cover both Series and DataFrame (including mixed columns with and without NaNs) for var and std.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.
Backup reviewer: claudevdm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@jrmccluskey jrmccluskey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Please rebase onto current master so we can get clean CI run before merging

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DataFrame var(), std(), skew() and kurt() ignore NaN with skipna=False

2 participants