Skip to content
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

Add PD002 use-of-inplace-argument documentation #2799

Merged
merged 8 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ For more, see [pandas-vet](https://pypi.org/project/pandas-vet/) on PyPI.

| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| PD002 | use-of-inplace-argument | `inplace=True` should be avoided; it has inconsistent behavior | 🛠 |
| PD002 | [use-of-inplace-argument](https://github.com/charliermarsh/ruff/blob/main/docs/rules/use-of-inplace-argument.md) | `inplace=True` should be avoided; it has inconsistent behavior | 🛠 |
| PD003 | use-of-dot-is-null | `.isna` is preferred to `.isnull`; functionality is equivalent | |
| PD004 | use-of-dot-not-null | `.notna` is preferred to `.notnull`; functionality is equivalent | |
| PD007 | use-of-dot-ix | `.ix` is deprecated; use more explicit `.loc` or `.iloc` | |
Expand Down
23 changes: 23 additions & 0 deletions crates/ruff/src/rules/pandas_vet/rules/inplace_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ use crate::rules::pandas_vet::fixes::fix_inplace_argument;
use crate::violation::AlwaysAutofixableViolation;

define_violation!(
/// ## What it does
/// Checks for `inplace=True` in code using the `pandas` library.
///
/// ## Why is this bad?
/// - `inplace=True` often does not provide a performance benefit. It is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: In the other rule documentations we have been using * as a list marker rather than - ... it would be nice to be consistent.

/// likely to copy dataframes in the background.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Markdown lists look a bit better if the wrapped text is indented as much as the first line of the list item.

/// - It encourages mutation rather than immutable data, which is harder to
/// reason about and may cause bugs.
/// - It removes the ability to use the method chaining style for `pandas`
/// code.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have clarified ... I care in particular about the ruff rule output not being longer than 80 chars.

Since the /// is stripped from that the actual lines here can in fact be 88 chars long, so I think code. can be still on the previous line.

///
/// ## Example
/// ```python
/// df.sort_values("col1", inplace=True)
/// ```
///
/// Use instead:
/// ```python
/// sorted_df = df.sort_values("col1")
/// ```
///
/// ## References
/// - [Why You Should Probably Never Use pandas inplace=True](https://towardsdatascience.com/why-you-should-probably-never-use-pandas-inplace-true-9f9f211849e4)
pub struct UseOfInplaceArgument;
);
impl AlwaysAutofixableViolation for UseOfInplaceArgument {
Expand Down
29 changes: 29 additions & 0 deletions docs/rules/use-of-inplace-argument.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# use-of-inplace-argument (PD002)

Derived from the **pandas-vet** linter.

Autofix is always available.

## What it does
Checks for `inplace=True` in code using the `pandas` library.

## Why is this bad?
- `inplace=True` often does not provide a performance benefit. It is
likely to copy dataframes in the background.
- It encourages mutation rather than immutable data, which is harder to
reason about and may cause bugs.
- It removes the ability to use the method chaining style for `pandas`
code.

## Example
```python
df.sort_values("col1", inplace=True)
```

Use instead:
```python
sorted_df = df.sort_values("col1")
```

## References
- [Why You Should Probably Never Use pandas inplace=True](https://towardsdatascience.com/why-you-should-probably-never-use-pandas-inplace-true-9f9f211849e4)