fix(pandas): resolve SettingWithCopyWarning across post-processing utils#39293
Open
abhyudaytomar wants to merge 1 commit intoapache:masterfrom
Open
fix(pandas): resolve SettingWithCopyWarning across post-processing utils#39293abhyudaytomar wants to merge 1 commit intoapache:masterfrom
abhyudaytomar wants to merge 1 commit intoapache:masterfrom
Conversation
Add explicit .copy() calls before mutating DataFrames that may be views/slices, preventing pandas SettingWithCopyWarning in logs. Files fixed: - histogram.py: copy before to_numeric assignment - boxplot.py: copy before to_numeric loop - rank.py: copy before rank column assignment - pivot.py: copy before fillna on columns - prophet.py: copy before tz_convert - geography.py: copy before geohash operations - compare.py: copy before in-place rename Fixes apache#36530 Made-with: Cursor
Contributor
Code Review Agent Run #bccc0dActionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
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.
Summary
Fixes #36530
When loading dashboards with Histogram charts, a
SettingWithCopyWarningappears in Python logs because DataFrame mutation operations are performed on potential views/slices without making an explicit copy first.This PR adds
.copy()calls before mutating DataFrames across all affected pandas post-processing utilities:df = df.copy()beforeto_numericassignmentdf = df.copy()beforeto_numericloop over metricsdf = df.copy()before rank column assignmentdf = df.copy()beforefillnaon columnsdf = df.copy()before timezone conversion.copy()before geohash operations.copy()before in-place renameRoot Cause
The pandas
SettingWithCopyWarningis triggered when assigning to a column of a DataFrame that may be a slice/view rather than an owned copy. While functionally correct in most cases, this is a pandas anti-pattern that can mask subtle bugs and pollutes application logs.Test Plan
pytest tests/unit_tests/pandas_postprocessing/)SettingWithCopyWarningin logsMade with Cursor