[Numpy-discussion] {zeros, empty, ...}_like functions behavior with subclasses

Pierre GM pgmdevlist@gmail....
Mon Feb 28 18:24:07 CST 2011


On Mar 1, 2011, at 1:05 AM, Bruce Southey wrote:

> On Mon, Feb 28, 2011 at 4:52 PM, Wes McKinney <wesmckinn@gmail.com> wrote:
>> I'm having some trouble with the zeros_like function via np.fix:
>> 
>> def zeros_like(a):
>>    if isinstance(a, ndarray):
>>        res = ndarray.__new__(type(a), a.shape, a.dtype, order=a.flags.fnc)
>>        res.fill(0)
>>        return res
>>    try:
>>        wrap = a.__array_wrap__
>>    except AttributeError:
>>        wrap = None
>>    a = asarray(a)
>>    res = zeros(a.shape, a.dtype)
>>    if wrap:
>>        res = wrap(res)
>>    return res
>> 
>> As you can see this is going to discard any metadata stored in a
>> subtype. I'm not sure whether this is a bug or a feature but wanted to
>> bring it up.
>> 
>> Thanks,
>> Wes
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> 
> 
> I guess this is ticket 929.
> http://projects.scipy.org/numpy/ticket/929
> 
> I was looking at it today but was not sure what is really desired
> here. I considered that this just meant shape and dtype but not sure
> about masked or record arrays behavior. So:
> What is the value of having the metadata?
> What is the meaning of 'like' here?

Well, that depends on what you wanna do, of course. To handle metadata, I use some kind of dictionary updated in the __array_finalize__. Check numpy.ma.MaskedArray and its subclasses (like scikits.timeseries.TimeSeries) for the details.
Now that you could store some extra data in the dtype (if I remmbr and understand correctly), it might be worth considering a proper way to deal with that.




More information about the NumPy-Discussion mailing list