[Numpy-discussion] dtype=object behavior change from 0.9.6 to beta 1
Christopher Barker
Chris.Barker at noaa.gov
Thu Aug 31 14:08:51 CDT 2006
Tom Denniston wrote:
> So my question is what is the _advantage_ of the new semantics?
what if the list don't have the same length, and therefor can not be
made into an array, now you get a weird result:
>>>N.array([N.array([1,'A',None],dtype=object),N.array([2,2,'Somestring',5],dtype=object)]).shape
()
Now you get an Object scalar.
but:
>>>N.array([N.array([1,'A',None],dtype=object),N.array([2,2,'Somestring',5],dtype=object)],dtype=object).shape
(2,)
Now you get a length 2 array, just like before: far more consistent.
With the old semantics, if you test your code with arrays of different
lengths, you'll get one thing, but if they then happen to be the same
length in some production use, the whole thing breaks -- this is a Bad Idea.
Object arrays are just plain weird, there is nothing you can do that
will satisfy every need. I think it's best for the array constructor to
not try to guess at what the hierarchy of sequences you *meant* to use.
You can (and probably should) always be explicit with:
>>> A = N.empty((2,), dtype=object)
>>> A
array([None, None], dtype=object)
>>> A[:] = [N.array([1,'A', None],
dtype=object),N.array([2,2,'Somestring',5],dtype=object)]
>>> A
array([[1 A None], [2 2 Somestring 5]], dtype=object)
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the Numpy-discussion
mailing list