DONT-MERGE GSOC26: Improve performance of replace_with_mask()#476
Draft
s7g4 wants to merge 1 commit intolincc-frameworks:mainfrom
Draft
DONT-MERGE GSOC26: Improve performance of replace_with_mask()#476s7g4 wants to merge 1 commit intolincc-frameworks:mainfrom
s7g4 wants to merge 1 commit intolincc-frameworks:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #476 +/- ##
=======================================
Coverage 96.03% 96.04%
=======================================
Files 20 20
Lines 2247 2249 +2
=======================================
+ Hits 2158 2160 +2
Misses 89 89 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Click here to view all benchmarks. |
| # TODO: performance optimization | ||
| # https://github.com/lincc-frameworks/nested-pandas/issues/52 | ||
| # Fast path for scalars using native broadcasting | ||
| if isinstance(value, pa.Scalar): |
Contributor
There was a problem hiding this comment.
Please include unit tests for the new functionality. Existing test_replace_with_mask only provides list test data.
|
|
||
|
|
||
| def replace_with_mask(array: pa.ChunkedArray, mask: pa.BooleanArray, value: pa.Array) -> pa.ChunkedArray: | ||
| def replace_with_mask(array: pa.ChunkedArray, mask: pa.BooleanArray, value: pa.Array | pa.Scalar) -> pa.ChunkedArray: |
Contributor
There was a problem hiding this comment.
Pre-commit fails for line too long.
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.
Change Description
Closes #52
Optimized
NestedExtensionArray.__setitem__and the internalreplace_with_maskutility to handlepyarrow.Scalarvalues directly. This avoids the significant overhead caused by expanding a single replacement scalar into a full-sizedpyarrow.Array(matching the length of the target array).Solution Description
The optimization involves two main changes in
src/nested_pandas/series/ext_array.py:NestedExtensionArray.__setitem__: Now detects if the replacement value can be boxed as apa.Scalar. If so, it passes the scalar directly instead of creating alen(self)-sized array.replace_with_mask: Updated to handlepa.Scalarinputs. When a scalar is detected, it leverages PyArrow's nativepa.compute.if_elsekernel for efficient broadcasting.Benchmarks (1,000,000 rows):
Code Quality