Skip to content

[Python] Fix DataFrame idxmin and idxmax on empty or all-NA partitions - #41663

Open
anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-idxmax-empty-partition
Open

anishmehta24 wants to merge 1 commit into
apache:masterfrom
anishmehta24:fix/dataframe-idxmax-empty-partition

Conversation

@anishmehta24

Copy link
Copy Markdown
Contributor

Fixes #41662

_idxmaxmin_helper runs pandas idxmax/idxmin on every partition before the final step. pandas raises ValueError on an empty partition (easy to get after a filter), and on an all-NA one for datetime, string and nullable dtypes, even when the whole column has values. compute_idx now passes such a partition on unchanged, so the final step sees its rows and gives the pandas result. If every partition is empty, the final step still raises, as pandas does.

Added test_idxmin_idxmax_all_na_partition to frames_test.py (each test partition holds one row there, so the NaT rows make all-NaT partitions) and test_idxmin_idxmax_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 25, 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 a ValueError in DeferredSeries and DeferredDataFrame idxmin()/idxmax() when an intermediate partition is empty (e.g., after filtering) or contains an all-NA/NaT column. By short-circuiting compute_idx when s.isna().all() or df.isna().all().any() is true, empty and all-NA partitions are safely forwarded to the final singleton aggregation stage.

🔍 General Feedback

  • The fix in sdks/python/apache_beam/dataframe/frames.py is clean, concise, and preserves the existing fallback behavior for NA results across both skipna=True and skipna=False.
  • Unit tests in frames_test.py and transforms_test.py thoroughly cover both all-NaT partitions and empty partitions resulting from filter operations.

💡 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: @damccorm for label python.
Backup reviewer: tvalentyn (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).

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 idxmin/idxmax fail when a partition is empty or all NaT

1 participant