Skip to content

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

Merged
jrmccluskey merged 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-moments-skipna-false
Dec 3, 2026
Merged

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

@anishmehta24
anishmehta24 force-pushed the fix/dataframe-moments-skipna-false branch from 0677d52 to 1a01062 Compare December 2, 2026 22:35
@jrmccluskey
jrmccluskey merged commit 00c948c into apache:master Dec 3, 2026
77 of 78 checks passed
@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.64%. Comparing base (a204d40) to head (1a01062).
⚠️ Report is 20 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41874      +/-   ##
============================================
- Coverage     59.65%   59.64%   -0.01%     
  Complexity     5188     5188              
============================================
  Files          1471     1471              
  Lines        188543   188548       +5     
  Branches       3885     3885              
============================================
+ Hits         112467   112468       +1     
- Misses        72665    72669       +4     
  Partials       3411     3411              
Flag Coverage Δ
python 80.83% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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