[issue19161] collections Counter handles nan strangely

Adam Davison report at bugs.python.org
Fri Oct 4 12:07:23 CEST 2013


New submission from Adam Davison:

If you pass an array containing nan to collections.Counter, rather than counting the number of 'nan's it outputs "'nan': 1" n times into the dictionary. I appreciate using this on an array of floats is a bit of an unusual case but I don't think this is the expected behaviour based on the documentation.

To reproduce, try e.g.:
a = [1, 1, 1, 2, 'nan', 'nan', 'nan']
collections.Counter(map(float, a))

Based on the documentation I expected to see:
{1.0: 3, 2.0: 1, nan: 3}

But it actually returns:
{1.0: 3, 2.0: 1, nan: 1, nan: 1, nan: 1}

Presumably this relates to the fact that nan != nan. I'm not 100% sure if this is a bug or maybe just something that should be mentioned in the documentation... Certainly it's not what I wanted it to do :)

Thanks,

Adam

----------
components: Library (Lib)
messages: 198938
nosy: Adam.Davison
priority: normal
severity: normal
status: open
title: collections Counter handles nan strangely
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19161>
_______________________________________


More information about the Python-bugs-list mailing list