Skip to content

Commit 1874d52

Browse files
[pandas]: Fix issue on non pandas dataframe in-place usage (PD002) (#18963)
1 parent 18efe2a commit 1874d52

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

crates/ruff_linter/src/rules/pandas_vet/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ mod tests {
2929
"#,
3030
"PD002_fail"
3131
)]
32+
#[test_case(
33+
r"
34+
import polars as pl
35+
x = pl.DataFrame()
36+
x.drop(['a'], inplace=True)
37+
",
38+
"PD002_pass_polars"
39+
)]
40+
#[test_case(
41+
r"
42+
x = DataFrame()
43+
x.drop(['a'], inplace=True)
44+
",
45+
"PD002_pass_no_import"
46+
)]
3247
#[test_case(
3348
r"
3449
import pandas as pd

crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::Locator;
99
use crate::checkers::ast::Checker;
1010
use crate::fix::edits::{Parentheses, remove_argument};
1111
use crate::{Edit, Fix, FixAvailability, Violation};
12+
use ruff_python_semantic::Modules;
1213

1314
/// ## What it does
1415
/// Checks for `inplace=True` usages in `pandas` function and method
@@ -52,12 +53,7 @@ impl Violation for PandasUseOfInplaceArgument {
5253

5354
/// PD002
5455
pub(crate) fn inplace_argument(checker: &Checker, call: &ast::ExprCall) {
55-
// If the function was imported from another module, and it's _not_ Pandas, abort.
56-
if checker
57-
.semantic()
58-
.resolve_qualified_name(&call.func)
59-
.is_some_and(|qualified_name| !matches!(qualified_name.segments(), ["pandas", ..]))
60-
{
56+
if !checker.semantic().seen_module(Modules::PANDAS) {
6157
return;
6258
}
6359

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/pandas_vet/mod.rs
3+
---
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/pandas_vet/mod.rs
3+
---
4+

0 commit comments

Comments
 (0)