GitHub issue #4692: Unable to cast a column with `Image()` by using the `cast_column()` feature
State: closed | Created: 2022-07-15T22:56:03Z

## Describe the bug
A clear and concise description of what the bug is.

When I create a dataset, then add a column to the created dataset through the `dataset.add_column` feature and then try to cast a column of the dataset (this column contains image paths) with `Image()` by using the `cast_column()` feature, I get the following error - ``` TypeError: Couldn't cast array of type
string
to
{'bytes': Value(dtype='binary', id=None), 'path': Value(dtype='string', id=None)} ```

When I try and cast the same column, but without doing the `add_column` in the previous step, it works as expected.

## Steps to reproduce the bug
```python
from datasets import Dataset, Image


data_dict = {
    "img_path": ["https://picsum.photos/200/300"]
}

dataset = Dataset.from_dict(data_dict)

#NOTE Comment out this line and use cast_column and it works properly
dataset = dataset.add_column("yeet", [1])

#NOTE This line fails to execute properly if `add_column` is called before
dataset = dataset.cast_column("img_path", Image())

# #NOTE This is my current workaround. This seems to work fine with/without `add_column`. While
# # running this, make sure to comment out the `cast_column` line
# new_features = dataset.features.copy()
# new_features["img_path"] = Image()
# dataset = dataset.cast(new_features)


print(dataset)
print(dataset.features)
print(dataset[0])
```

## Expected results
A clear and concise description of the expected results.

Able to successfully use `cast_column` to cast a column containing img_paths to now be Image() features after modifying the dataset using `add_column` in a previous step

## Actual results
Specify the actual results or traceback.

```
Traceback (most recent call last):
  File "/home/surya/Desktop/hf_bug_test.py", line 14, in <module>
    dataset = dataset.cast_column("img_path", Image())
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/fingerprint.py", line 458, in wrapper
    out = func(self, *args, **kwargs)
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/arrow_dataset.py", line 1580, in cast_column
    dataset._data = dataset._data.cast(dataset.features.arrow_schema)
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1487, in cast
    new_tables.append(subtable.cast(subschema, *args, **kwargs))
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 834, in cast
    return InMemoryTable(table_cast(self.table, *args, **kwargs))
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1897, in table_cast
    return cast_table_to_schema(table, schema)
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1880, in cast_table_to_schema
    arrays = [cast_array_to_feature(table[name], feature) for name, feature in features.items()]
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1880, in <listcomp>
    arrays = [cast_array_to_feature(table[name], feature) for name, feature in features.items()]
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1673, in wrapper
    return pa.chunked_array([func(chunk, *args, **kwargs) for chunk in array.chunks])
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1673, in <listcomp>
    return pa.chunked_array([func(chunk, *args, **kwargs) for chunk in array.chunks])
  File "/home/surya/anaconda3/envs/snap_test/lib/python3.9/site-packages/datasets/table.py", line 1846, in cast_array_to_feature
    raise TypeError(f"Couldn't cast array of type\n{array.type}\nto\n{feature}")
TypeError: Couldn't cast array of type
string
to
{'bytes': Value(dtype='binary', id=None), 'path': Value(dtype='string', id=None)}
```

## Environment info
<!-- You can run the command `datasets-cli env` and copy-and-paste its output below. -->
- `datasets` version: 2.3.2
- Platform: Ubuntu 20.04.3 LTS
- Python version: 3.9.7
- PyArrow version: 7.0.0