Skip to content

TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [nan] of <class 'float'> #32431

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
kzhui125 opened this issue Mar 4, 2020 · 10 comments · Fixed by #36635
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@kzhui125
Copy link

kzhui125 commented Mar 4, 2020

import pandas as pd
import numpy as np

df = pd.Series([1, '{1,2}', 1, None])
vc = df.value_counts(dropna=False)
print(vc.loc[np.nan])
print(vc[np.nan])
TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [nan] of <class 'float'>

Hello, now I have trouble getting null value count, help.

@jbrockmendel
Copy link
Member

This works on master, what pandas version are you using?

@kzhui125
Copy link
Author

kzhui125 commented Mar 5, 2020

@jbrockmendel

1.0.1

1

@NicolasCwy
Copy link

NicolasCwy commented Jul 26, 2020

Facing the same problem Pandas 1.0.5. What code should I be providing?

TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [nan] of <class 'float'>

@jbrockmendel
Copy link
Member

This works in 1.1.0

@simonjayhawkins simonjayhawkins added good first issue Needs Tests Unit test(s) needed to prevent regressions labels Jul 31, 2020
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Jul 31, 2020
@simonjayhawkins
Copy link
Member

@jbrockmendel i've labelled this as needs tests. Any idea which commit fixed this and whether additional tests are needed.

@alexhtn
Copy link
Contributor

alexhtn commented Aug 3, 2020

I think that commit d4293f0 fixed this issue. But I'm not sure about how I find it. I took commits from git log --pretty=oneline and in automated mode go from newest to oldest until code from first post started raising errors. On commit d4293f0 all works fine and on the next commit 58df0ac starts errors.

@simonjayhawkins
Copy link
Member

Thanks @alexhtn I think #31962 may also have contributed to fixing the issue. (commit before this KeyError: nan was raised)

d206479 is the first fixed commit
commit d206479
Author: jbrockmendel [email protected]
Date: Tue Mar 3 07:27:17 2020 -0800

REF: remove _convert_scalar_indexer (#31962)

@jbrockmendel i've labelled this as needs tests. Any idea which commit fixed this and whether additional tests are needed.

no tests were added in #31962 and the test added in #31866 are for error checking so safe to say should add a test for the code sample in OP to close issue

But I'm not sure about how I find it.

~/test.py

import numpy as np
import pandas as pd
print(pd.__version__)
s = pd.Series([1, "{1,2}", 1, None])
vc = s.value_counts(dropna=False)
print(vc)

try:
    res = vc.loc[np.nan]
    print(res)
except:
    pass
else:
    sys.exit(1)

and in shell

git bisect start HEAD v1.0.5 --term-new fixed --term-old broken
git bisect run /bin/bash -c "python setup.py build_ext -i;python ~/test.py"

see #35685

@wesleyboelrijk
Copy link
Contributor

take

@jreback jreback modified the milestones: Contributions Welcome, 1.2 Oct 31, 2020
@callmekofi
Copy link

facing the same proble
Screenshot 2021-12-19 at 12 50 00 AM
m

@jbrockmendel
Copy link
Member

@callmekofi what version of pandas are you using? for me the example in the OP currently raises KeyError, which I think is correct

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
Development

Successfully merging a pull request may close this issue.

8 participants