`IndexError` when indexing `Sequence` of `Array2D` with `None` values
State: closed | Author: qgallouedec (MEMBER)

### Describe the bug

Having `None` values in a `Sequence` of `ArrayND` fails.


### Steps to reproduce the bug

```python
from datasets import Array2D, Dataset, Features, Sequence

data = [
    [
        [[0]],
        None,
        None,
    ]
]
feature = Sequence(Array2D((1, 1), dtype="int64"))
dataset = Dataset.from_dict({"a": data}, features=Features({"a": feature}))

dataset[0]  # error raised only when indexing
```

```
Traceback (most recent call last):
  File "/Users/quentingallouedec/gia/c.py", line 13, in <module>
    dataset[0]  # error raised only when indexing
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/arrow_dataset.py", line 2658, in __getitem__
    return self._getitem(key)
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/arrow_dataset.py", line 2643, in _getitem
    formatted_output = format_table(
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/formatting/formatting.py", line 634, in format_table
    return formatter(pa_table, query_type=query_type)
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/formatting/formatting.py", line 406, in __call__
    return self.format_row(pa_table)
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/formatting/formatting.py", line 441, in format_row
    row = self.python_arrow_extractor().extract_row(pa_table)
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/formatting/formatting.py", line 144, in extract_row
    return _unnest(pa_table.to_pydict())
  File "pyarrow/table.pxi", line 4146, in pyarrow.lib.Table.to_pydict
  File "pyarrow/table.pxi", line 1312, in pyarrow.lib.ChunkedArray.to_pylist
  File "pyarrow/array.pxi", line 1521, in pyarrow.lib.Array.to_pylist
  File "pyarrow/scalar.pxi", line 675, in pyarrow.lib.ListScalar.as_py
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/features/features.py", line 760, in to_pylist
    return self.to_numpy(zero_copy_only=zero_copy_only).tolist()
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/datasets/features/features.py", line 725, in to_numpy
    numpy_arr = np.insert(numpy_arr.astype(np.float64), null_indices, np.nan, axis=0)
  File "<__array_function__ internals>", line 200, in insert
  File "/Users/quentingallouedec/gia/env/lib/python3.10/site-packages/numpy/lib/function_base.py", line 5426, in insert
    old_mask[indices] = False
IndexError: index 3 is out of bounds for axis 0 with size 3
```

AFAIK, the problem only occurs when you use a `Sequence` of `ArrayND`.

I strongly suspect that the problem comes from this line, or `np.insert` is misused:

https://github.com/huggingface/datasets/blob/02ee418831aba68d0be93227bce8b3f42ef8980f/src/datasets/features/features.py#L729

To put t simply, you want something that do that:

```python
import numpy as np
numpy_arr = np.zeros((1, 1, 1))
null_indices = np.array([1, 2])
np.insert(numpy_arr, null_indices, np.nan, axis=0)
# raise an error, instead of outputting 
# array([[[ 0.]],
#        [[nan]],
#        [[nan]]])
```



### Expected behavior

The previous code should not raise an error.

### Environment info

- Python 3.10.11
- datasets 2.10.0
- pyarrow 12.0.0

Comments: Easy fix would be to add:

```python
null_indices -= np.arange(len(null_indices))
```

before L279, but I'm not sure it's the most intuitive way to fix it.

Same issue here:

https://github.com/huggingface/datasets/blob/7fcbe5b1575c8d162b65b9397b3dfda995a4e048/src/datasets/features/features.py#L1398

Fixed in #5948