-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DEPR: non-keyword arguments in any #44896
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
Conversation
If I understand the error correctly: any is a method of NDArray, but your overloads do now only cover DataFrame and Series - NDFrame is not covered by the overloads (since neither of them is a super-class for NDFrame). I think you will need a third overload that is compatible with the return type of the Series and DataFrame overload. Not sure whether this works: @overload
def any(
self: NDFrame,
axis: Axis = ...,
bool_only: bool_t | None = ...,
skipna: bool_t = ...,
level: Level | None = ...,
**kwargs,
) -> Series | bool_t:
...
|
@twoertwein Thanks for the suggestion. |
Why not define |
Thanks @MarcoGorelli |
If the mypy error is a false-positive, I would prefer adding two ignore comments compared to "implementing" At least in the toy example of python/mypy#11759, the return types are correctly inferred even though mypy complains. |
@twoertwein true, but there's the same construct for
Sure, but even then, what's the advantage of doing this via overloads rather than by overriding Even without the In terms of lines of code which need adding, overriding |
Thank you @MarcoGorelli , I agree |
Nice, thanks! Could you also post screenshots of the docs for You can use
|
There is a mismatch between the doc-string and the type annotations: The type annotations say that DataFrame.any returns a Series (doc: Series or DataFrame) and Seriesn.any returns a bool (doc: Series or scalar). |
Yes, you're right! So, the this needs some overloads too, to indicate that the return type depends on whether |
|
ah, it's because So, I'd suggest:
This has been a bit more challenging that I originally thought, but I hope you find it worthwhile and learn a few new things! |
|
f5412ed
to
831481c
Compare
@MarcoGorelli |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! And yes, this needs a whatsnew note (and ideally, a couple of little tests too)
pandas/core/generic.py
Outdated
) -> Series | bool_t: | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you keep the return type here? -> DataFrame | Series | bool_t
?
Also, as this is only used internally, we can make its arguments keyword-only right away without warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By making arguments keyword-only, do you mean placing arguments after ,*,
? (context: https://medium.com/analytics-vidhya/keyword-only-arguments-in-python-3c1c00051720)
What needs to be changed for the failing checks? |
You'll need to update those tests such that they don't throw a warning
So, change |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What needs to be changed for the failing checks?
You'll need to update those tests such that they don't throw a warning
=================================== FAILURES ===================================
__________________ TestDataFrameAnalytics.test_any_all_extra ___________________
[gw1] linux -- Python 3.10.4 /usr/share/miniconda/envs/pandas-dev/bin/python
self = <pandas.tests.frame.test_reductions.TestDataFrameAnalytics object at 0x7feb4634aa40>
def test_any_all_extra(self):
df = DataFrame(
{
"A": [True, False, False],
"B": [True, True, False],
"C": [True, True, True],
},
index=["a", "b", "c"],
)
> result = df[["A", "B"]].any(1)
pandas/tests/frame/test_reductions.py:1058:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
args = ( A B
a True True
b False True
c False False, 1)
kwargs = {}, arguments = ''
@wraps(func)
def wrapper(*args, **kwargs):
arguments = _format_argument_list(allow_args)
if len(args) > num_allow_args:
> warnings.warn(
msg.format(arguments=arguments),
FutureWarning,
stacklevel=stacklevel,
)
E FutureWarning: In a future version of pandas all arguments of DataFrame.any and Series.any will be keyword-only.
pandas/util/_decorators.py:[31](https://github.com/pandas-dev/pandas/runs/5830982510?check_suite_focus=true#step:11:31)2: FutureWarning
So, change result = df[["A", "B"]].any(1)
to result = df[["A", "B"]].any(axis=1)
, and likewise for the others
Looks like there's also a warning in the docs that needs updating:
WARNING:
>>>-------------------------------------------------------------------------
looking for now-outdated files... none found
Warning in /home/runner/work/pandas/pandas/doc/source/whatsnew/v0.13.0.rst at block ending on line 682
Specify :okwarning: as an option in the ipython:: block to suppress this message
----------------------------------------------------------------------------
<ipython-input-92-c0a7f4d519f6>:1: FutureWarning: In a future version of pandas all arguments of DataFrame.any and Series.any will be keyword-only.
dfi[mask.any(1)]
<<<-------------------------------------------------------------------------
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'll also need to change the .any usage in https://github.com/pandas-dev/pandas/blob/361021b56f3159afb71d690fac3a1f3b381b0da6/doc/source/whatsnew/v0.13.0.rst
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still got some failing tests
https://github.com/pandas-dev/pandas/runs/5869362121?check_suite_focus=true
What is the tool used in the tests for docstring validation? |
numpydoc There was a failure on the main branch but it's been fixed now (see #46690) |
I don't know how to solve the current failing checks. |
They're unrelated, don't worry about them |
@deprecate_nonkeyword_arguments( | ||
version=None, | ||
allowed_args=["self"], | ||
stacklevel=find_stack_level() - 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok i c, yeah we out to fix this but ok for here
lgm @MarcoGorelli over to you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @MarcoGorelli @twoertwein @jreback for the reviews. 😄 |
* tighten return type in any * correct overload definitions * add overload def for NDFrame input * remove overload defs and define function any in sub-classes * add overload defs for level * correct default val in overload defs * deprecate non-keyword args * add whatsnew note * modify return types and add tests * move non-keyword deprecation to generic * correct deprecation decorators * remove imports in test assertions * place deprecate_nonkeyword at correct place * remove changes from frame.py, series.py * readd changes in frame, series without actual implementations * place deprecate_nonkeyword at other place * add name argument to deprecate_non_keyword_args decorator * add test for name in deprecate_nonkeyword_args * remove changes from frame.py, series.py * correct stacklevel in warning * correct stacklevel * set stacklevel to default * move deprecation message to whatsnew v1.5.0.rst * add name parameter in deprecate_non_keyword_args docstring * correct whitespace in deprecate_nonkeyword_args docstring * update any non-keyword args in other tests * update any in doc * update remaining any() calls in pandas/core * correct docstring of isocalendar in pandas/core/indexes/accessors.py Co-authored-by: Marco Edward Gorelli <[email protected]>
From pandas 2.0 any only accepts keyworkd arguments ref pandas-dev/pandas#44896
* Transform positional argument into keyword argument From pandas 2.0 any only accepts keyworkd arguments ref pandas-dev/pandas#44896 * Change how reciprocal is computed I have not fully understood why this solve the problem, but splitting the operation in 2 lines does not seem to work * Catch warnings from pandas.to_datetime Now pandas.to_datetime raises a warning when the column cannot be converted * check_dtype=False in tests datetime features Pandas dataframes created from python integers are created with int column types `int64` but the operation tested returns `int32` which caused issues * Use droplevel before merging Merging dfs with different column lelvels has been disallowed ref pandas-dev/pandas#34862 * Change expected values for months I am not sure why this caused an issue, maybe due to type casting? * run black * run black on tests * isort _variable_type_checks.py * Fix datetime_subtraction --------- Co-authored-by: Claudio Salvatore Arcidiacono <[email protected]>
precursor to #44802
What does this implement/fix? Explain your changes.
I used
@overload
definitions to specify thatDataFrame.any
should return typeSeries
andSeries.any
should have return typebool
.Any other comments?
I am currently getting errors on checking with
mypy
,The erased type of self "pandas.core.frame.DataFrame" is not a supertype of its class "pandas.core.generic.NDFrame"
.So I am currently trying to figure out how to resolve this error and whether using
@overload
is the correct direction to do this. Mentioning this pull request as WIP.