[GH-2768] Replace len(self)==0 with cheaper _is_empty() check in GeoSeries#2770
Merged
[GH-2768] Replace len(self)==0 with cheaper _is_empty() check in GeoSeries#2770
Conversation
…eries Replace all 6 occurrences of len(self) == 0 in GeoSeries with a new _is_empty() helper that uses spark_frame.take(1) instead of DataFrame.count(). This avoids triggering a full Spark scan just to check if the series is empty. Affected methods: crs (getter), build_area(), polygonize(), union_all(), intersection_all(), total_bounds.
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes empty-input guards in GeoSeries to avoid triggering expensive Spark count() scans when checking for emptiness in Pandas-on-Spark-backed series.
Changes:
- Added a private
GeoSeries._is_empty()helper that usesspark_frame.take(1)for short-circuit emptiness checks. - Replaced
len(self) == 0guards withself._is_empty()in 6 GeoSeries methods/properties (crs,build_area,polygonize,union_all,intersection_all,total_bounds).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…mpty returns In build_area() and polygonize(), the early-return for empty input used crs=self.crs, which triggers the crs getter and calls _is_empty() again. Use crs=None instead since the series is known to be empty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Did you read the Contributor Guide?
Is this PR related to a ticket?
[GH-XXX] my subject. Closes GeoSeries: len(self) == 0 checks trigger full Spark scans #2768What changes were proposed in this PR?
Several GeoSeries methods use
len(self) == 0as an early-return guard for empty input. Under the hood,len()on a Pandas-on-Spark Series callsDataFrame.count(), which triggers a full Spark scan of all rows.This PR adds a private
_is_empty()helper method that usesself._internal.spark_frame.take(1)instead, which short-circuits after finding a single row rather than counting all rows.All 6 occurrences of
len(self) == 0ingeoseries.pyare replaced:crs(getter)build_area()polygonize()union_all()intersection_all()total_boundsHow was this patch tested?
Existing tests for all affected methods (
test_build_area,test_polygonize,test_union_all,test_intersection_all,test_total_bounds,test_crs,test_empty_list) were run and pass.Did this PR include necessary documentation updates?