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

Make Index.all compatible with numpy.all #40180

Merged
merged 8 commits into from Mar 5, 2021

Conversation

pauldmccarthy
Copy link
Contributor

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

Due to a bug in my own code, I discovered that pandas.Index objects cannot be passed to the numpy.all function:

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: idx = pd.Int64Index([1,2,3,4,5])

In [4]: np.all(idx)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-597ac8c43e48> in <module>
----> 1 np.all(idx)

<__array_function__ internals> in all(*args, **kwargs)

~/venvs/funpack.venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py in all(a, axis, out, keepdims, where)
   2437     """
   2438     return _wrapreduction(a, np.logical_and, 'all', axis, None, out,
-> 2439                           keepdims=keepdims, where=where)
   2440
   2441

~/venvs/funpack.venv/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     83                 return reduction(axis=axis, dtype=dtype, out=out, **passkwargs)
     84             else:
---> 85                 return reduction(axis=axis, out=out, **passkwargs)
     86
     87     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)

TypeError: all() got an unexpected keyword argument 'axis'

Using this PR as precedent, the fix is quite straightforward.

@@ -35,7 +35,7 @@ Bug fixes
Other
~~~~~

-
- Allow :class:`Index` to be passed to the numpy ``all`` function.
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 1.3 Bug Fixes in reshaping section, add the PR number as the issue number, can you reference np.all here I think :func:numpy.all works or similar

@@ -550,6 +550,13 @@ def test_min_max_categorical(self):
assert ci.min() == "c"
assert ci.max() == "b"

def test_numpy_any_all(self):
idx = Index([0, 1, 2])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the issue number as a comment

Copy link
Contributor

Choose a reason for hiding this comment

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

can you move this near other tests similar to def test_any_all

Copy link
Contributor

Choose a reason for hiding this comment

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

can you parameterize over Index & Series

@@ -550,6 +550,13 @@ def test_min_max_categorical(self):
assert ci.min() == "c"
assert ci.max() == "b"

def test_numpy_any_all(self):
idx = Index([0, 1, 2])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you parameterize over Index & Series

@gfyoung gfyoung added Compat pandas objects compatability with Numpy or Python functions Index Related to the Index class or subclasses Bug labels Mar 4, 2021
@pauldmccarthy
Copy link
Contributor Author

@jreback Requested changes made - thanks!

@jreback jreback added this to the 1.3 milestone Mar 5, 2021
@jreback jreback merged commit 539b813 into pandas-dev:master Mar 5, 2021
@jreback
Copy link
Contributor

jreback commented Mar 5, 2021

thanks @pauldmccarthy very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Index Related to the Index class or subclasses
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants