Is it possible or how to define a `datasets.Sequence` that could potentially be either a dict, a str, or None?
Opened by gitforziio (closed)

### Feature request

Hello! Apologies if my question sounds naive:

I was wondering if it’s possible, or how one would go about defining a 'datasets.Sequence' element in datasets.Features that could potentially be either a dict, a str, or None?

Specifically, I’d like to define a feature for a list that contains 18 elements, each of which has been pre-defined as either a `dict or None` or `str or None` - as demonstrated in the slightly misaligned data provided below:

```json
[
  [
    {"text":"老妇人","idxes":[0,1,2]},null,{"text":"跪","idxes":[3]},null,null,null,null,{"text":"在那坑里","idxes":[4,5,6,7]},null,null,null,null,null,null,null,null,null,null],
  [
    {"text":"那些水","idxes":[13,14,15]},null,{"text":"舀","idxes":[11]},null,null,null,null,null,{"text":"在那坑里","idxes":[4,5,6,7]},null,{"text":"出","idxes":[12]},null,null,null,null,null,null,null],
  [
    {"text":"水","idxes":[38]},
    null,
    {"text":"舀","idxes":[40]},
    "假",  // note this is just a standalone string
    null,null,null,{"text":"坑里","idxes":[35,36]},null,null,null,null,null,null,null,null,null,null]]
```

### Motivation

I'm currently working with a dataset of the following structure and I couldn't find a solution in the [documentation](https://huggingface.co/docs/datasets/v2.11.0/en/package_reference/main_classes#datasets.Features).

```json
{"qid":"3-train-1058","context":"桑桑害怕了。从玉米地里走到田埂上，他遥望着他家那幢草房子里的灯光，知道母亲没有让他回家的意思，很伤感，有点想哭。但没哭，转身朝阿恕家走去。","corefs":[[{"text":"桑桑","idxes":[0,1]},{"text":"他","idxes":[17]}]],"non_corefs":[],"outputs":[[{"text":"他","idxes":[17]},null,{"text":"走","idxes":[11]},null,null,null,null,null,{"text":"从玉米地里","idxes":[6,7,8,9,10]},{"text":"到田埂上","idxes":[12,13,14,15]},null,null,null,null,null,null,null,null],[{"text":"他","idxes":[17]},null,{"text":"走","idxes":[66]},null,null,null,null,null,null,null,{"text":"转身朝阿恕家去","idxes":[60,61,62,63,64,65,67]},null,null,null,null,null,null,null],[{"text":"灯光","idxes":[30,31]},null,null,null,null,null,null,{"text":"草房子里","idxes":[25,26,27,28]},null,null,null,null,null,null,null,null,null,null],[{"text":"他","idxes":[17]},{"text":"他家那幢草房子","idxes":[21,22,23,24,25,26,27]},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"远"],[{"text":"他","idxes":[17]},{"text":"阿恕家","idxes":[63,64,65]},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"变近"]]}
```

### Your contribution

I'm going to provide the dataset at https://huggingface.co/datasets/2030NLP/SpaCE2022 .

Comments:
Hi ! `datasets` uses Apache Arrow as backend to store the data, and it requires each column to have a fixed type. Therefore a column can't have a mix of dicts/lists/strings.

Though it's possible to have one (nullable) field for each type:
```python
features = Features({
    "text_alone": Value("string"),
    "text_with_idxes": {
        "text": Value("string"),
        "idxes": Value("int64")
    }
})
```

but you'd have to reformat your data fiels or define a [dataset loading script](https://huggingface.co/docs/datasets/dataset_script) to apply the appropriate parsing.

Alternatively we could explore supporting the Arrow [Union](https://arrow.apache.org/docs/python/generated/pyarrow.UnionType.html) type which could solve this issue, but I don't know if it's well supported in python and with the rest of the ecosystem like Parquet

@lhoestq  Thank you! I further wonder if it's possible to use list subscripts as keys of a feature? Like
```python
features = Features({
    0: Value("string"),
    1: {
        "text": Value("string"),
        "idxes": [Value("int64")]
    },
    2: Value("string"),
    # ...
})
```

Column names need to be strings, so you could use "1", "2", etc. or give appropriate column names

@lhoestq Got it. Thank you!