Skip to content

[Python] Fix DataFrame min and max on empty or all-NA partitions - #41733

Merged
shunping merged 5 commits into
apache:masterfrom
anishmehta24:fix/dataframe-min-max-na-partition
Dec 2, 2026
Merged

shunping merged 5 commits into
apache:masterfrom
anishmehta24:fix/dataframe-min-max-na-partition

Conversation

@anishmehta24

@anishmehta24 anishmehta24 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #41732

For min and max, DeferredSeries.aggregate wraps each partition's result in pd.Series(...). A partition with only missing values gives NaT or NA, and pd.Series makes that datetime64 or object, so the final step fails on timedelta, tz-aware datetime, string and nullable columns. An empty partition gives NaN, which turns a skipna=False result into NaN. The per-partition step now keeps the input dtype and passes nothing on for an empty partition. Other aggregations are unchanged.

The min and max proxy was also wrong: it was computed from the empty proxy, where min and max give NaN, so it was float64 or NaT instead of the column's type. It is now computed from a one-row sample of the column dtype (object and categorical columns keep the old proxy), so the min/max aggregation tests check proxies again and their TODOs are removed.

Added test_min_max_all_na_partition to frames_test.py (each test partition holds one row there, so the NA rows make all-NA partitions) and test_min_max_skipna_false_with_empty_partition to transforms_test.py (a pipeline where the filter leaves one batch empty). Both fail on master. The apache_beam/dataframe tests and the DataFrame and Series pandas doctests pass.

@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Nov 28, 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 cleanly fixes DeferredSeries.aggregate for min and max when encountering empty or all-NA partitions by preserving the input Series dtype in pre_aggregate and emitting an empty Series for empty partitions. The intermediate proxy is preserved via to_series(self._expr.proxy()) to avoid breaking existing proxy expectations, and unit tests in frames_test.py and transforms_test.py thoroughly cover the affected column types and skipna=False scenarios.

🔍 General Feedback

  • The fix is well-targeted and properly handles both scalar (func='min') and single-element Series (func=['min']) aggregation outputs while maintaining compatibility across Pandas dtypes (timedelta64, tz-aware datetime64, nullable Int64, and string).
  • Test coverage is comprehensive and directly exercises both all-NA partitions and empty partitions resulting from filtering.

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

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.
Backup reviewer: jrmccluskey (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).

'int': pd.array([None, 5, None, 2], dtype='Int64'),
'str': pd.array([None, 'b', None, 'a'], dtype='string'),
})
# TODO(https://github.com/apache/beam/issues/20926): max and min produce

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Anything else we need to address this TODO?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the proxy itself was still wrong: it is computed from the empty proxy, where min and max give NaN, so it came out float64 or NaT instead of the column's type. 1c3cf69 now computes the final proxy from a one-row sample of the column dtype, so this TODO and the four in the agg tests above are removed. The check stays off only for agg(['min', 'max'], skipna=False) here, where both results are NA and pandas picks the dtype from those NA values.

@github-actions github-actions Bot added size/m M: 50-249 lines changed and removed size/s S: 10-49 lines changed labels Nov 30, 2026
@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.12%. Comparing base (7dc9d7b) to head (0dac59f).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/dataframe/frames.py 91.66% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41733      +/-   ##
============================================
- Coverage     58.86%   58.12%   -0.74%     
- Complexity     2333     2842     +509     
============================================
  Files          1136     1211      +75     
  Lines        171012   173599    +2587     
  Branches       1552     2329     +777     
============================================
+ Hits         100662   100910     +248     
- Misses        67796    69973    +2177     
- Partials       2554     2716     +162     
Flag Coverage Δ
python 80.75% <91.66%> (+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.

@shunping shunping left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. LGTM!

@shunping
shunping merged commit 3d379d7 into apache:master Dec 2, 2026
48 of 60 checks passed
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 min() and max() fail or give NaN when a partition is empty or all-NA

2 participants