2

I am working with a process that outputs a list of lists like this:

data_recieved = [[], [1, 5]]

There will always be a list of lists but null values are not included when data does not exist. This causes the array to evaluate as 1 dimensional.

print(np.ndim(np.array(data_recieved)))
1

But if I impute the missing values, it resolves to 2d.

modified_data = [[np.nan, np.nan], [1, 5]]
print(np.ndim(np.array(modified_data)))
2

I could write something to loop through each item and figure out the maximum sublist length and then loop through again to add the null values but that seems sloppy and inefficient. Is there a clean way to force this input into a 2d array?

Chris
  • 12,900
  • 12
  • 43
  • 65
  • More generally, exactly what output are you expecting for the sample input you gave? – Mad Physicist Jul 15 '16 at 16:23
  • @dashiell - updated the question to include an example. Thanks! – Chris Jul 15 '16 at 16:24
  • What about for `[[2], [1, 5]]`? – Mad Physicist Jul 15 '16 at 16:25
  • I would like `np.ndim` to resolve to 2 in this case. The input is a 2d array stored as a list but it does not include null value placeholders so numpy evaluates it as a 1d array. For `[[2], [1, 5]]` I would still want to convert it to a 2d array. – Chris Jul 15 '16 at 16:26
  • What I meant was, how would you convert `[[2], [1, 5]]`? What result would you expect? Is it even a possible input? – Mad Physicist Jul 15 '16 at 16:28
  • should `[[2], [1, 5]]` appear as `np.array[[nan, 2],[1, 5]]` or `np.array[[2, nan],[1, 5]]`? – user3820991 Jul 15 '16 at 16:31
  • @user3820991 great point, in this case it doesn't matter, either would be fine as long as it is consistent. – Chris Jul 15 '16 at 16:34
  • Is there a reason you can't just remove the empty arrays? For example: `[val for val in data_received if val]` – dashiell Jul 15 '16 at 16:34
  • They can be partially empty or fully empty but they still need to be there to represent a category of data for indexing purposes. – Chris Jul 15 '16 at 16:37
  • I think in this case the answer is "don't use arrays for this, Chris". The other answer that was flagged outlines how I would do it but there is no obvious simple solution I was missing so I am comfortable doing the loop thing. Thanks for the help everyone! – Chris Jul 15 '16 at 16:46
  • You can avoid looping with a broadcasting based approach, something along the lines shown in this post : http://stackoverflow.com/a/34208731/3293881 – Divakar Jul 15 '16 at 18:11

0 Answers0