GitHub issue #5936: Sequence of array not supported for most dtype
State: closed | Created: 2023-06-08T18:18:07Z

### Describe the bug

Create a dataset composed of sequence of array fails for most dtypes (see code below).

### Steps to reproduce the bug

```python
from datasets import Sequence, Array2D, Features, Dataset

import numpy as np

for dtype in [
    "bool",  # ok
    "int8",  # failed
    "int16",  # failed
    "int32",  # failed
    "int64",  # ok
    "uint8",  # failed
    "uint16",  # failed
    "uint32",  # failed
    "uint64",  # failed
    "float16",  # failed
    "float32",  # failed
    "float64",  # ok
]:
    features = Features({"foo": Sequence(Array2D(dtype=dtype, shape=(2, 2)))})
    sequence = [
        [[1.0, 2.0], [3.0, 4.0]],
        [[5.0, 6.0], [7.0, 8.0]],
    ]
    array = np.array(sequence, dtype=dtype)
    try:
        dataset = Dataset.from_dict({"foo": [array]}, features=features)
    except Exception as e:
        print(f"Failed for dtype={dtype}")
```

Traceback for `dtype="int8"`:

```
Traceback (most recent call last):
  File "/home/qgallouedec/datasets/a.py", line 29, in <module>
    raise e
  File "/home/qgallouedec/datasets/a.py", line 26, in <module>
    dataset = Dataset.from_dict({"foo": [array]}, features=features)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/arrow_dataset.py", line 899, in from_dict
    pa_table = InMemoryTable.from_pydict(mapping=mapping)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 799, in from_pydict
    return cls(pa.Table.from_pydict(*args, **kwargs))
  File "pyarrow/table.pxi", line 3725, in pyarrow.lib.Table.from_pydict
  File "pyarrow/table.pxi", line 5254, in pyarrow.lib._from_pydict
  File "pyarrow/array.pxi", line 350, in pyarrow.lib.asarray
  File "pyarrow/array.pxi", line 236, in pyarrow.lib.array
  File "pyarrow/array.pxi", line 110, in pyarrow.lib._handle_arrow_array_protocol
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/arrow_writer.py", line 204, in __arrow_array__
    out = cast_array_to_feature(out, type, allow_number_to_str=not self.trying_type)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 1833, in wrapper
    return func(array, *args, **kwargs)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 2091, in cast_array_to_feature
    casted_values = _c(array.values, feature.feature)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 1833, in wrapper
    return func(array, *args, **kwargs)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 2139, in cast_array_to_feature
    return array_cast(array, feature(), allow_number_to_str=allow_number_to_str)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 1833, in wrapper
    return func(array, *args, **kwargs)
  File "/home/qgallouedec/env/lib/python3.10/site-packages/datasets/table.py", line 1967, in array_cast
    return pa_type.wrap_array(array)
  File "pyarrow/types.pxi", line 879, in pyarrow.lib.BaseExtensionType.wrap_array
TypeError: Incompatible storage type for extension<arrow.py_extension_type<Array2DExtensionType>>: expected list<item: list<item: int8>>, got list<item: list<item: int64>>
```

### Expected behavior

Not to fail.

### Environment info


- Python 3.10.6
- datasets: master branch
- Numpy: 1.23.4