Unable to cast a column with `Image()` by using the `cast_column()` feature
COLLABORATOR
Hi, thanks for reporting! A PR (https://github.com/huggingface/datasets/pull/4614) has already been opened to address this issue.
2022-07-19T12:59:09Z
https://github.com/huggingface/datasets/issues/4692#issuecomment-1189022322
https://api.github.com/repos/huggingface/datasets/issues/4692
IC_kwDODunzps5G3wpy
https://api.github.com/repos/huggingface/datasets/issues/comments/1189022322/reactions
2022-07-19T12:59:09Z
https://api.github.com/repos/huggingface/datasets/issues/comments/1189022322
https://avatars.githubusercontent.com/u/47462742?v=4
https://api.github.com/users/mariosasko/events{/privacy}
https://api.github.com/users/mariosasko/followers
https://api.github.com/users/mariosasko/following{/other_user}
https://api.github.com/users/mariosasko/gists{/gist_id}
https://github.com/mariosasko
mariosasko
MDQ6VXNlcjQ3NDYyNzQy
https://api.github.com/users/mariosasko/orgs
https://api.github.com/users/mariosasko/received_events
https://api.github.com/users/mariosasko/repos
https://api.github.com/users/mariosasko/starred{/owner}{/repo}
https://api.github.com/users/mariosasko/subscriptions
User
https://api.github.com/users/mariosasko
public
## 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
