Skip to content

BUG: np.argwhere on pandas series #35331

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

Closed
2 of 3 tasks
rossbar opened this issue Jul 17, 2020 · 4 comments · Fixed by #53381
Closed
2 of 3 tasks

BUG: np.argwhere on pandas series #35331

rossbar opened this issue Jul 17, 2020 · 4 comments · Fixed by #53381
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@rossbar
Copy link

rossbar commented Jul 17, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


numpy/numpy#15555 reports an issue with np.argwhere on pandas Series. Reporting here for visibility.

MRE:

>>> import numpy as np
>>> import pandas as pd
>>> s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
>>> np.argwhere(s < 0)

which, with numpy.__version__ ==1.20.0.dev0+046a736 gives:
pd.version == 0.25.3:

FutureWarning: Series.nonzero() is deprecated and will be removed in a future version.Use Series.to_numpy().nonzero() instead
array([[3]])

pd.version == 1.0.5:

ValueError: Length of passed values is 1, index implies 5.
@rossbar rossbar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 17, 2020
@simonjayhawkins simonjayhawkins added Compat pandas objects compatability with Numpy or Python functions Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 18, 2020
@simonjayhawkins
Copy link
Member

Thanks @rossbar for drawing our attention to this issue.

@ArchangeGabriel
Copy link

I’ve run into this while trying to execute a code that used to work back in 2019. The workaround for anyone stumbling upon this is thus to .to_numpy() the series in the np.argwhere() call, e.g. for the above example:

>>> np.argwhere(s.to_numpy() < 0)

@mroeschke
Copy link
Member

This looks to work on main now. Could use a test

In [18]: >>> import numpy as np
    ...: >>> import pandas as pd
    ...: >>> s = pd.Series(range(5), index=['a', 'b', 'c', 'd', 'e'])
    ...: >>> np.argwhere(s < 3)
Out[18]: 
array([[0],
       [1],
       [2]])

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Regression Functionality that used to work in a prior pandas version Compat pandas objects compatability with Numpy or Python functions labels May 15, 2023
@mcgeestocks
Copy link
Contributor

I can jump in and add a test for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
5 participants