Skip to content

DEPR: deprecate inplace keyword in the DataFrame.drop() method#65683

Open
tamhahn wants to merge 17 commits into
pandas-dev:mainfrom
tamhahn:depr_inplace_drop_v2
Open

DEPR: deprecate inplace keyword in the DataFrame.drop() method#65683
tamhahn wants to merge 17 commits into
pandas-dev:mainfrom
tamhahn:depr_inplace_drop_v2

Conversation

@tamhahn
Copy link
Copy Markdown
Contributor

@tamhahn tamhahn commented May 19, 2026

Greetings!

This PR addresses the tracker at Issue #63207 for DataFrame.drop()

It adds a deprecation warning (Pandas4Warning) for DataFrame.drop(), a deprecation note in docstring, a test for the warning, edits to other affected test files, and whatsnew update.

Adding this warning to DataFrame.drop affected quite a few tests (about 1000 initially failed), so I did 3 things:

  1. Added filterwarning decorator to filter out the warning for tests to several test files.
  2. Edited _drop_labels_or_levels in generic.py to not use inplace in 2 instances. Other methods (melt and merge) call this and it affected their tests.
  3. There was one particular test (test_inplace_drop_and_operation in test_drop.py) where adding the pytest.mark.filterwarning decorator did not filter out the warning, and I made changes to the test itself, which I was trying to avoid. Please let me know if that's not the correct way to address this.

I ran pytest pandas -n 4 -m "not slow and not network and not db and not single_cpu" -r sxX.
The result are the same 7 failures I also get when running that same command on my local main branch (no diffs to upstream).

@tamhahn
Copy link
Copy Markdown
Contributor Author

tamhahn commented May 19, 2026

Sorry, a test failed right away. I will look into it and update. Could you please disregard this PR for now.

@tamhahn tamhahn marked this pull request as draft May 19, 2026 03:31
@jorisvandenbossche jorisvandenbossche added Deprecate Functionality to remove in pandas inplace Relating to inplace parameter or equivalent labels May 20, 2026
@jorisvandenbossche jorisvandenbossche added this to the 3.1 milestone May 20, 2026
Copy link
Copy Markdown
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Looks good!

return df.drop_duplicates(subset=cols[:-1])


@pytest.mark.filterwarnings("ignore:The inplace keyword in DataFrame.drop is")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think for this test, you could also update the test to not use inplace (from a quick look, I think the test is not really about the inplace drop, but it is using that to construct the expected result of the indexing operation)

@jorisvandenbossche
Copy link
Copy Markdown
Member

2. Edited _drop_labels_or_levels in generic.py to not use inplace in 2 instances. Other methods (melt and merge) call this and it affected their tests.

Yes, good call (we should not be using inplace internally, so those kind of cases can indeed be updated)

Sorry, a test failed right away. I will look into it and update. Could you please disregard this PR for now.

No worries, currently CI is failing for pyarrow and numpy nightly, and that is not related to the changes here and also happening on other PRs

@tamhahn tamhahn marked this pull request as ready for review May 25, 2026 03:38
@tamhahn
Copy link
Copy Markdown
Contributor Author

tamhahn commented May 25, 2026

Thank you very much, Joris!

I have also removed the use of drop with inplace in the test file you highlighted ([pandas/tests/indexing/multiindex/test_indexing_slow.py as you suggested.

The test did have an assert statement ensuring that df.drop with inplace=true returns None but that was already covered in other tests, so unless there is a special interaction with the MultiIndex-related Issues listed in the test ( # GH7724, GH2646), it is a bit of a redundant test. If I missed it, I'll put it back in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Deprecate Functionality to remove in pandas inplace Relating to inplace parameter or equivalent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants