question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deprecate non-keyword arguments for methods with `inplace`

See original GitHub issue

Background

Having many parameters with defaults makes static typing a lot harder when one of them changes the return type, as they require many* overloads.

There’s still no broad agreement about whether or not to deprecate the inplace argument, so in the meanwhile, if we force default-arguments to be keyword-only, then we can at least set the correct return types for these methods without requiring all these overloads (as many as 38 in one case!).

What to do

  1. Choose a method from the following list:
  • pandas/core/computation/eval.py:164:0, eval (skip for now)
  • pandas/core/frame.py:4718:4, set_axis
  • pandas/core/frame.py:4743:4, drop
  • pandas/core/frame.py:5134:4, fillna
  • pandas/core/frame.py:5306:4, set_index
  • pandas/core/frame.py:5572:4, reset_index
  • pandas/core/frame.py:5809:4, dropna
  • pandas/core/frame.py:5958:4, drop_duplicates
  • pandas/core/frame.py:6196:4, sort_values
  • pandas/core/frame.py:6267:4, sort_index
  • pandas/core/generic.py:6432:4, ffill
  • pandas/core/generic.py:6495:4, bfill
  • pandas/core/generic.py:6700:4, interpolate
  • pandas/core/generic.py:7473:4, clip
  • pandas/core/generic.py:9080:4, where
  • pandas/core/generic.py:9234:4, mask
  • pandas/core/indexes/base.py:1530:4, set_names
  • pandas/core/indexes/multi.py:810:4, set_levels
  • pandas/core/indexes/multi.py:997:4, set_codes
  • pandas/core/resample.py:831:4, interpolate
  • pandas/core/series.py:1279:4, reset_index
  • pandas/core/series.py:2028:4, drop_duplicates
  • pandas/core/series.py:3228:4, sort_values
  • pandas/core/series.py:3438:4, sort_index
  • pandas/core/series.py:4475:4, set_axis
  • pandas/core/series.py:4490:4, drop
  • pandas/core/series.py:4713:4, fillna
  • pandas/core/series.py:5064:4, dropna
  • read_csv
  • read_table
  • concat
  1. Use pandas.util._decorators.deprecate_nonkeyword_arguments to issue a FutureWarning that the method’s default arguments will be keyword-only in the next release.

  2. Fixup any tests / internal code which uses such default arguments positionally. See this gist for a helper-script which can help identify cases which need fixing, just replace "drop" with whatever method you’re working on and then run it as python visitor.py.

  3. Add a new test, checking that a FutureWarning is raised if this method’s default arguments are used positionally.

  4. Add a note to doc/source/whatsnew/v1.3.0.rst

See https://github.com/pandas-dev/pandas/pull/41511 for an example of how to do this.

In most of these methods, the first argument is probably fine to be kept as is, it’s the ones after that (such as axis) which should become keyword-only.


*(2^{number of default types preceding the one to overload} +2)


It’s really common to call replace with both value and to_replace as positional, so I think we can make an exception for that one. It’ll only require 6 overloads, which I’d argue isn’t too many, especially if no other method requires more than 3 or 4


TODO: unify the whatsnew entries

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:35 (35 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, May 24, 2021

yup, that’s the one

easiest way to find where things are defined is probably to find the function in the documentation, then click “source”

Screenshot 2021-05-24 at 18 06 39

1reaction
MarcoGorellicommented, May 24, 2021

@Anupam-USP Thanks!

This wasn’t part of the original issue, but read_csv also has keyword argument which changes the return type, if you wanted to make a PR for that one too that would be welcome!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update pandas DataFrame.drop() for Future Warning ...
If the warning is telling you that in the future some arguments will be keyword-only, then presumably the way to avoid the warning...
Read more >
Why You Should Probably Never Use pandas inplace=True
This article will explain what the pandas inplace=True keyword means, how it behaves, and why you should probably never use it.
Read more >
What's new in 1.4.0 (January 22, 2022) - Pandas
The keyword arguments level and names have been added to Styler.hide() (and implicitly to the deprecated methods Styler.hide_index() and ...
Read more >
typing — Support for type hints — Python 3.11.1 documentation
The deprecated types will be removed from the typing module in the first Python version ... There is no syntax to indicate optional...
Read more >
Separation of positional and keyword arguments in Ruby 3.0
foo() passes no arguments, but target receives an empty hash argument in Ruby 2.6. This is because the method foo delegates keywords (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found