Repository: pandas-dev/pandas
Pull request: TST: GH 32431 check print label indexing on nan
- [x] closes #32431 
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [x] whatsnew entry --> not applicable

This is my first contribution to pandas. I added a simple test that was requested in one of the open issues.

Problem statement:
TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [nan] of <class 'float'>
```py
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.

Hints from issue discussion:
This works on master, what pandas version are you using?
@jbrockmendel 

1.0.1

<img width="901" alt="1" src="https://user-images.githubusercontent.com/11925432/75945525-6abc6d00-5ed5-11ea-84c1-b25dec8c470d.png">

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'>
This works in 1.1.0
@jbrockmendel i've labelled this as needs tests. Any idea which commit fixed this and whether additional tests are needed.
I think that commit d4293f0d232590c61d6deb517c0eea42035d1df3 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 d4293f0d232590c61d6deb517c0eea42035d1df3 all works fine and on the next commit 58df0ac699a7b60ac5724da689c930e5b58134e6 starts errors.
Thanks @alexhtn I think #31962 may also have contributed to fixing the issue. (commit before this `KeyError: nan` was raised)

d2064793087ccce7265829d9bb54ec6e0bbb0446 is the first fixed commit
commit d2064793087ccce7265829d9bb54ec6e0bbb0446
Author: jbrockmendel <jbrockmendel@gmail.com>
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

Commit message: TST: GH 32431 check print label indexing on nan

Patch to review:
diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py
index 0cc61cd7df389..39e01dd69e121 100644
--- a/pandas/tests/indexing/test_indexing.py
+++ b/pandas/tests/indexing/test_indexing.py
@@ -868,6 +868,13 @@ def test_no_reference_cycle(self):
         del df
         assert wr() is None
 
+    def test_label_indexing_on_nan(self):
+        # GH 32431
+        df = pd.Series([1, "{1,2}", 1, None])
+        vc = df.value_counts(dropna=False)
+        print(vc.loc[np.nan])
+        print(vc[np.nan])
+
 
 class TestSeriesNoneCoercion:
     EXPECTED_RESULTS = [


Code review comments:
diff_hunk: @@ -868,6 +868,13 @@ def test_no_reference_cycle(self):
         del df
         assert wr() is None
 
+    def test_label_indexing_on_nan(self):
+        # GH 32431
+        df = pd.Series([1, "{1,2}", 1, None])
+        vc = df.value_counts(dropna=False)
+        print(vc.loc[np.nan])
path: pandas/tests/indexing/test_indexing.py
text: @user1:
Rather than just printing you should construct the expected result and use `tm.assert_series_equal(result, expected)` - you should see this idiom throughout the test modules if you need a reference

@author:
Thanks for the feedback! I constructed the expected result and expected an `int`. So I compared the results just with a normal assert, since I couldn't find a method on tm to assert ints. Please let me know if this satisfies your request