1

So my data looks like:

[array([0], dtype=int64),
 array([1], dtype=int64),
 array([1], dtype=int64),
 array([2], dtype=int64),
 array([3], dtype=int64),
 array([3], dtype=int64),
 array([4], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([], dtype=int64),
 array([6], dtype=int64) ...

While using np.concatenate(list_1) does concatenate the arrays but skips the empty arrays. And in tthe resultant array 6 is the next element to 4 and intermediate empty arrays does not appear in the list. This is what np.concateneate does but I do not want it that way.

I want to combine those arrays into a single array having same length as the list but NaN values in place of empty arrays. How can I achieve that?

2 Answers 2

2

One way:

np.concatenate([a if a.size else np.array([np.nan]) for a in array_list])

I would hazard a guess though there is probably a better way to load your data.

As a side note, concatenate does not skip any array - it concatenates the arrays, that is, puts the elements for each one after the other. Placing 0 elements is just not noticeable.

Sign up to request clarification or add additional context in comments.

4 Comments

This is likely a very good way. You have to flag your missing items somehow one way or another
There been some errors. I was able to run your snippet with following changes: np.concatenate([a if a.size else np.asarray([np.nan]) for a in array_list])
@User5 np.array is enough though it shouldn't matter. I indeed had spelling mistakes (forgot size is not a function), thanks.
Thanks! @kabanus that worked though and I cannot think of a better way to do it!
0

You can pre-allocate the output and assign to it directly:

result = np.full(len(list_1), np.nan)
for i, v in enumerate(list_1):
    if v.size:
        result[i] = v.item()

Alternatively, you can do

result = np.empty(len(list_1))
for i, v in enumerate(list_1):
    result[i] = v.item() if v.size else np.nan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.