Skip to content

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

Closed

Description

@atomicpirate

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

Activity

  1. ewmoore commented on Aug 21, 2015

    @ewmoore
    Contributor

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

  2. atomicpirate commented on Aug 21, 2015

    @atomicpirate
    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.

  3. pv commented on Aug 21, 2015

    @pv
    Member

    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'

  4. pv commented on Aug 21, 2015

    @pv
    Member

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

  5. pv commented on Aug 21, 2015

    @pv
    Member
  6. jaimefrio commented on Aug 21, 2015

    @jaimefrio
    Member

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

  7. pv commented on Aug 21, 2015

    @pv
    Member
  8. springcoil commented on Aug 26, 2015

    @springcoil

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

  9. charris commented on Feb 20, 2016

    @charris
    Member

    Closed by #6269.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions