Ufunc generates unintuitive AttributeError on object arrays (Trac #1013) #1611

Open
thouis opened this Issue Oct 19, 2012 · 11 comments

Comments

Projects
None yet
7 participants
Contributor

thouis commented Oct 19, 2012

Original ticket http://projects.scipy.org/numpy/ticket/1013 on 2009-02-20 by @wesm, assigned to unknown.

This error is very unintuitive for end-users, arrays formed from SQL query results can frequently end up as object arrays by accident.

In [15]: arr = np.random.randn(100).astype(object)

In [16]: np.log(arr)

AttributeError Traceback (most recent call last)

H:\workspace\Python\src in ()

AttributeError: log

Same AttributeError is raised for other ufuncs

Contributor

thouis commented Oct 19, 2012

@rkern wrote on 2009-02-20

Can you suggest an error message text that you would have been more helpful?

Contributor

thouis commented Oct 19, 2012

Milestone changed to Unscheduled by @mwiebe on 2011-03-24

Contributor

thouis commented Oct 19, 2012

Title changed from Ufunc generates AttributeError on object arrays to Ufunc generates unintuitive AttributeError on object arrays by @mwiebe on 2011-03-24

hayd commented Jun 6, 2013

Perhaps a better error message is:

TypeError("%s requires an array with numeric dtype but was passed %s" % ufunc, x.dtype)

(I think it should TypeError rather than an AttributeError...?)

When passing an array of strings to numpy.log you get:

TypeError: Not implemented for this type

I think this would also be improved with the above - more descriptive - message.

Owner

njsmith commented Jun 6, 2013

The reason for the AttributeError is that as a last ditch attempt, np.log
and friends on an object array will try doing obj.log(), and it's this last
ditch attempt that's failing.

I agree the message is tremendously confusing though and it's bitten me
several times. If you want to submit a pull request then many people would
be grateful.
On 6 Jun 2013 20:53, "Andy Hayden" notifications@github.com wrote:

Perhaps a better error message is:

TypeError("%s requires an array with numeric dtype but was passed %s" % ufunc, x.dtype)

(I think it should TypeError rather than an AttributeError...?)

When passing an array of strings to numpy.log you get:

TypeError: Not implemented for this type

I think this would also be improved with the above - more descriptive -
message.


Reply to this email directly or view it on GitHubhttps://github.com/numpy/numpy/issues/1611#issuecomment-19069832
.

hayd commented Jun 6, 2013

Will have a look into it (probably sometime next week)...

Owner

charris commented Feb 20, 2014

@hayd Still pursuing this?

Contributor

ewmoore commented Feb 20, 2014

#368 seems related / duplicate.

danielballan referenced this issue in lmfit/lmfit-py Sep 11, 2014

Merged

Scalars and list #126

Can anyone point me and/or @hayd to where in the code this can be fixed. I spent some time looking, but I don't understand the numpy internals well enough yet....

Contributor

juliantaylor commented Sep 30, 2014

PyUFunc_O_O_method in ./numpy/core/src/umath/loops.c.src looks like the right spot

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