I am surprised by the following behavior:
>>> import numpy as np >>> from collections import Counter >>> my_list = [1,2,2, np.nan, np.nan] >>> Counter(my_list) Counter({nan: 2, 2: 2, 1: 1})
What's going on here?
When I use float('nan') instead of np.nan , I get the expected behavior:
>>> my_list = [1,2,2, float('nan'), float('nan')] >>> Counter(my_list) Counter({2: 2, nan: 1, 1: 1, nan: 1})
I am using python 2.7.3 and numpy 1.8.1 .
Edit:
If I do this:
>>> a = 300 >>> b = 300 >>> a is b False >>> Counter([a, b]) Counter({300: 2})
So, Counter or any python dict considers two objects X and Y not the same if:
X == Y -> False and X is Y -> False
right?
source share