[pandas]: Fix issue on non pandas dataframe in-place usage (PD002) (#18963)

This commit is contained in:
Jordy Williams 2025-06-27 16:56:13 +10:00 committed by GitHub
parent 18efe2ab46
commit 1874d52eda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 6 deletions

View file

@ -29,6 +29,21 @@ mod tests {
"#,
"PD002_fail"
)]
#[test_case(
r"
import polars as pl
x = pl.DataFrame()
x.drop(['a'], inplace=True)
",
"PD002_pass_polars"
)]
#[test_case(
r"
x = DataFrame()
x.drop(['a'], inplace=True)
",
"PD002_pass_no_import"
)]
#[test_case(
r"
import pandas as pd

View file

@ -9,6 +9,7 @@ use crate::Locator;
use crate::checkers::ast::Checker;
use crate::fix::edits::{Parentheses, remove_argument};
use crate::{Edit, Fix, FixAvailability, Violation};
use ruff_python_semantic::Modules;
/// ## What it does
/// Checks for `inplace=True` usages in `pandas` function and method
@ -52,12 +53,7 @@ impl Violation for PandasUseOfInplaceArgument {
/// PD002
pub(crate) fn inplace_argument(checker: &Checker, call: &ast::ExprCall) {
// If the function was imported from another module, and it's _not_ Pandas, abort.
if checker
.semantic()
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| !matches!(qualified_name.segments(), ["pandas", ..]))
{
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

View file

@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pandas_vet/mod.rs
---

View file

@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pandas_vet/mod.rs
---