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

Wes McKinney wesmckinn@gmail....
Tue Mar 1 09:51:10 CST 2011


On Mon, Feb 28, 2011 at 10:52 PM, Wes McKinney <wesmckinn@gmail.com> wrote:
> On Mon, Feb 28, 2011 at 7:24 PM, Pierre GM <pgmdevlist@gmail.com> wrote:
>>
>> 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.
>>
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
> The ticket is exactly related to the problem at hand-- having
> __array_finalize__ defined won't help you as it never gets called.
>

Looks like this commit fixed the problem, so the ticket can be closed

https://github.com/numpy/numpy/commit/c9d1849332ae5bf73299ea1268f6a55f78624688#numpy/core/numeric.py


More information about the NumPy-Discussion mailing list