numpy / numpy/numpy

assert_array_equal does not compare NaNs as numbers when dtype is object

Open
#9,331 4 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

01 - Enhancement 62 - Python API component: numpy._core
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

OS: macOS Sierra (10.12.5)
Python version: 2.7.12
numpy version: 1.13.0

I notice that numpy.testing.assert_array_equal should compare NaNs as numbers unlike elsewhere in numpy, which makes sense to me. I verify this with the following test:

>>> import numpy.testing as npt
>>> import numpy as np
>>> a = np.array([1, 2, np.nan])
>>> npt.assert_array_equal(a, a)

However, this does not work with arrays that have dtype object:

>>> b = np.array([1, 2, 'a', np.nan], dtype=object)
>>> npt.assert_array_equal(b, b)
AssertionError: 
Arrays are not equal

(mismatch 25.0%)
 x: array([1, 2, 'a', nan], dtype=object)
 y: array([1, 2, 'a', nan], dtype=object)

Is this the desired behaviour? The only workaround in my case is to cast the array as dtype str.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at numpy.testing.assert_array_equal and reproduce the two examples from the issue, comparing regular and object-dtype arrays containing NaN. Trace the object-array comparison path and add a regression test showing the intended NaN comparison behavior, then run the relevant NumPy testing tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.