np.asscalar should pass through scalars #4701

Closed
mforbes opened this Issue May 12, 2014 · 0 comments

Comments

Projects
None yet
2 participants

mforbes commented May 12, 2014

Suggestion
It would be very convenient if np.asscalar() would pass through scalars. The use-case is to ensure that an argument is a scalar, regardless what is passed in (as long as it behaves as a scalar).

>>> np.asarray(1.0)
1.0
>>> np.asarray(1j)
1j

Presently these fail as the implementation simply looks for the .item() method, which scalars do not possess:

>>> np.asarray(1.0)
AttributeError                            Traceback (most recent call last)
<ipython-input-13-c6bc08be135e> in <module>()
----> 1 np.asscalar(1.0)

.../python2.7/site-packages/numpy/lib/type_check.pyc in asscalar(a)
    461 
    462     """
--> 463     return a.item()
    464 
    465 #-----------------------------------------------------------------------------

AttributeError: 'float' object has no attribute 'item'```

Rational
It is common to use np.asarray() to ensure that the argument is an array (in the case that it might be a list etc.). In an analogous use case, it would be useful to be able to use np.asscalar() to ensure that an argument is a scalar (as long as it can be converted to a scalar).

Workaround
Convert the argument to an array first.

np.asscalar(np.asarray(x))

Resolution
Perhaps code like the following would work:

def asscalar(a):
    try:
        return a.item()
    except AttributeError, e:
        return np.asarray(a).item()

@liufei11111 liufei11111 added a commit to liufei11111/numpy that referenced this issue Nov 6, 2014

@liufei11111 liufei11111 BUG: pmt() method of financial.py will yield error because the two pa…
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
fc5de3b

@liufei11111 liufei11111 added a commit to liufei11111/numpy that referenced this issue Nov 6, 2014

@liufei11111 liufei11111 BUG: pmt() method of financial.py will yield error because the two pa…
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
af91c17

@liufei11111 liufei11111 added a commit to liufei11111/numpy that referenced this issue Nov 6, 2014

@liufei11111 liufei11111 BUG: pmt() method of financial.py will yield error because the two pa…
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
70f29b6

@liufei11111 liufei11111 added a commit to liufei11111/numpy that referenced this issue Nov 6, 2014

@liufei11111 liufei11111 BUG: pmt() method of financial.py will yield error because the two pa…
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
da93986

@liufei11111 liufei11111 added a commit to liufei11111/numpy that referenced this issue Nov 6, 2014

@liufei11111 liufei11111 BUG: pmt() method of financial.py will yield error because the two pa…
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
7d7746c

@liufei11111 liufei11111 added a commit to liufei11111/numpy that referenced this issue Nov 7, 2014

@liufei11111 liufei11111 BUG: pmt() method of financial.py will yield error because the two pa…
…rameters passed in to np.where() is evaluated first before going into the function however, the denominator can be zero at that time. I allocated the intermediate result based on different formulas for rate == 0.0 and not equal to and fixes #5046. Also, a easy fix for asocial doesn’t take integer because it assumes passing in an np.array. I convert everything to np.array first and then retrieve the item. Fixed #4701
586620c

charris closed this in 97917ac Feb 18, 2015

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