numpy / numpy/numpy

np.median on a complex array return incorrect value for the imaginary part

Open
#12,943 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

00 - Bug 07 - Deprecation 15 - Discussion component: numpy.lib
Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

Calling np.median on a complex array return a complex number with correct real part, but incorrect imaginary part. Calling np.median on the real and imaginary part of the complex array separately return the correct values.

Reproducing code example:

d = (1 + 1j) + np.random.randn(10000) + 1j * np.random.randn(10000)
print 'median(d) : %.3f + %.3f i' % (np.median(d).real, np.median(d).imag)
print 'median(d.real): %.3f' % np.median(d.real)
print 'median(d.imag): %.3f' % np.median(d.imag)

median(d) : 1.002 + 0.124 i
median(d.real): 1.002
median(d.imag): 0.998

Numpy version: 1.15.4

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 by running the reproducing code against the reported NumPy version and compare np.median(d) with the medians of d.real and d.imag. Trace the median implementation and its existing tests for complex arrays; done means the complex-array result has the expected imaginary component and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.