Repository navigation
[Python] Fix DataFrame min and max on empty or all-NA partitions - #41733
Conversation
There was a problem hiding this comment.
📋 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-awaredatetime64, nullableInt64, andstring). - 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.
|
Assigning reviewers: R: @shunping for label python. Note: If you would like to opt out of this review, comment Available commands:
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 |
There was a problem hiding this comment.
Anything else we need to address this TODO?
There was a problem hiding this comment.
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.
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #41732
For
minandmax,DeferredSeries.aggregatewraps each partition's result inpd.Series(...). A partition with only missing values givesNaTorNA, andpd.Seriesmakes that datetime64 or object, so the final step fails on timedelta, tz-aware datetime,stringand nullable columns. An empty partition gives NaN, which turns askipna=Falseresult into NaN. The per-partition step now keeps the input dtype and passes nothing on for an empty partition. Other aggregations are unchanged.The
minandmaxproxy 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_partitionto frames_test.py (each test partition holds one row there, so the NA rows make all-NA partitions) andtest_min_max_skipna_false_with_empty_partitionto 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.