-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[pandas] Avoid flagging PD002 if pandas is not imported
#18963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MichaReiser
merged 4 commits into
astral-sh:main
from
jordyjwilliams:6432_pandas_on_non_pandas_fix
Jun 27, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
4 changes: 4 additions & 0 deletions
4
...les/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_no_import.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/pandas_vet/mod.rs | ||
| --- | ||
|
|
4 changes: 4 additions & 0 deletions
4
.../rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_polars.snap
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| source: crates/ruff_linter/src/rules/pandas_vet/mod.rs | ||
| --- | ||
|
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is now outdated.
Can you tell me more why we remove this check entirely?
CC: @dhruvmanila
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair call, I will remove the comment (7348b1c). I removed the check for the following reasons:
mainthese would fail, eg they would trigger thepandas linterissue.If there's something I'm missing here please let me know. Happy to add back in the check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EG with these changes we are using the
chekerhowever in a different way (as introduced in #14671.!checker.semantic().seen_module(Modules::PANDAS)we will nowskip the checkpandashas not been seen (used) within the offending code.Thus; this will allow for less false positives with similar codes on
in-placeoperations. egpl.dataframeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaReiser Isn't this change in line with what you did in #14671? In other words, why wasn't that change applied to this rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing check would only work when the method is used directly like
pandas.DataFrame.sort_valuesand not when used asdf = pandas.DataFrame(); df.sort_values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's the main difference. This PR removes some false positives (when panda isn't enabled at all) but introduces some new false positives (when using panda but the method doesn't come from pandas). This is different from my PRs where I only added the check.
However, I think this is still fine because it is in line with all other pandas rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaReiser good to merge from your point of view here? What I guess I was going for here was standardization.
And also basically looking to commit to this repo for the first time. @dhruvmanila's suggestion is what I was implementing here for robustness, completeness and to close out #6432 (comment)
In my experience people who would use
pandas.DataFramewould typically have these as variables and runmy_df.sort_values(in_place=True)or so... Thus I figured it's better to pick up on these cases (when we knowpandashas been seen)... And rather have allpdmethods work the same.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it's fine. Thank you for working on this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nws, thanks for the approval and merge.
Anything further required from my side after merge?
I have read the contributing guide just trying to make sure there's nothing I've missed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, all good from your side. This will go out in the next release