I have a numpy object array containing several lists of index numbers:
>>> idxLsts = np.array([[1], [0, 2]], dtype=object)
I define a vectorized function to append a value to each list:
>>> idx = 99
>>> f = np.vectorize(lambda idxLst: idxLst.append(idx))
I invoke the function. I don’t care about the return value, just the side effect.
>>> f(idxLsts)
array([None, None], dtype=object)
The index 99 was added twice to the first list. Why? I’m stumped.
>>> idxLsts
array([[1, 99, 99], [0, 2, 99]], dtype=object)
With other values of idxLsts, it doesn’t happen:
>>> idxLsts = np.array([[1, 2], [0, 2, 4]], dtype=object)
>>> f(idxLsts)
array([None, None], dtype=object)
>>> idxLsts
array([[1, 2, 99], [0, 2, 4, 99]], dtype=object)
My suspicion is it’s related to the documentation which says: “Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a numpy array as output. The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy.”
From the
vectorizedocstring:And from the code:
This is an extra call to
thefunc, used to determine the output type. This is why the first element is getting two99s appended.This behavior happens in your second case as well:
yields
You could use
np.frompyfuncinstead ofnp.vectorize:yields