Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy.sign behaves oddly when dtype is numpy.object #6229

Closed
atomicpirate opened this issue Aug 21, 2015 · 9 comments
Closed

numpy.sign behaves oddly when dtype is numpy.object #6229

atomicpirate opened this issue Aug 21, 2015 · 9 comments

Comments

@atomicpirate
Copy link

In [1]: import numpy

In [2]: foo = numpy.array([-.1, 0, .1])

In [3]: numpy.sign(foo)
Out[3]: array([-1., 0., 1.])

In [4]: numpy.sign(foo.astype(numpy.object))
Out[4]: array([1, -1, -1], dtype=object)

The underlying data is still just a float:
In [5]: type(foo.astype(numpy.object)[0])
Out[5]: float

This result when using an array of numpy.object does not seem desirable. Either it should operate correctly on the underlying numbers or if called with invalid data (strings, etc) it should throw an exception. This way I could populate an array with heterogeneous data, say a mix of integers and floats, and still get a valid result.

I tested this on:
Mac OS 10.6.8
Python 3.4.2
numpy 1.9.2

@ewmoore
Copy link
Contributor

ewmoore commented Aug 21, 2015

Looks like OBJECT_sign is missing the logic to return 0 for 0. Compare with BYTE_sign.

@atomicpirate
Copy link
Author

Right--zero didn't get a zero result, negative results in +1, positive results in -1. The results for numpy.object seem incorrect on all counts.

@pv
Copy link
Member

pv commented Aug 21, 2015

I can't reproduce your example. get:
.
In [7]: numpy.sign(foo.astype(numpy.object))
array([-1, 0, 1], dtype=object)
In [12]: numpy.version
'1.9.2'

@pv
Copy link
Member

pv commented Aug 21, 2015

Ah ok, the incorrect results occur on Python 3 only.

@pv
Copy link
Member

pv commented Aug 21, 2015

Hah, it's a stupid bug, the == 0 should be == 1 here https://github.com/numpy/numpy/blob/master/numpy/core/include/numpy/npy_3kcompat.h#L328

@jaimefrio
Copy link
Member

There, and it the two other checks below it, right?

@pv
Copy link
Member

pv commented Aug 21, 2015 via email

@springcoil
Copy link

Is this still open - it looks like an easy fix could be done.

@charris
Copy link
Member

charris commented Feb 20, 2016

Closed by #6269.

@charris charris closed this as completed Feb 20, 2016
jaimefrio pushed a commit to jaimefrio/numpy that referenced this issue Mar 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants