This could be one approach -
def numpy_fillna(data):
# Get lengths of each row of data
lens = np.array([len(i) for i in data])
# Mask of valid places in each row
mask = np.arange(lens.max()) < lens[:,None]
# Setup output array and put elements from data into masked positions
out = np.zeros(mask.shape, dtype=data.dtype)
out[mask] = np.concatenate(data)
return out
Sample input, output -
In [222]: # Input object dtype array
...: data = np.array([[1, 2, 3, 4],
...: [2, 3, 1],
...: [5, 5, 5, 5, 8 ,9 ,5],
...: [1, 1]])
In [223]: numpy_fillna(data)
Out[223]:
array([[1, 2, 3, 4, 0, 0, 0],
[2, 3, 1, 0, 0, 0, 0],
[5, 5, 5, 5, 8, 9, 5],
[1, 1, 0, 0, 0, 0, 0]], dtype=object)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…