fix CastError pickling
State: closed | Author: lhoestq (MEMBER)

reported in https://discuss.huggingface.co/t/datasetdict-save-to-disk-with-num-proc-1-seems-to-hang-with-error/75595

Comments: The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6712). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

<details>
<summary>Show benchmarks</summary>

PyArrow==8.0.0

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005151 / 0.011353 (-0.006202) | 0.003813 / 0.011008 (-0.007196) | 0.062957 / 0.038508 (0.024449) | 0.028282 / 0.023109 (0.005173) | 0.246036 / 0.275898 (-0.029862) | 0.290024 / 0.323480 (-0.033456) | 0.004009 / 0.007986 (-0.003977) | 0.002749 / 0.004328 (-0.001580) | 0.049351 / 0.004250 (0.045101) | 0.041143 / 0.037052 (0.004090) | 0.264782 / 0.258489 (0.006293) | 0.290711 / 0.293841 (-0.003130) | 0.027248 / 0.128546 (-0.101298) | 0.010691 / 0.075646 (-0.064955) | 0.205926 / 0.419271 (-0.213345) | 0.035652 / 0.043533 (-0.007880) | 0.246357 / 0.255139 (-0.008782) | 0.267851 / 0.283200 (-0.015348) | 0.018498 / 0.141683 (-0.123185) | 1.135996 / 1.452155 (-0.316159) | 1.181841 / 1.492716 (-0.310875) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.094054 / 0.018006 (0.076048) | 0.305470 / 0.000490 (0.304980) | 0.000225 / 0.000200 (0.000025) | 0.000043 / 0.000054 (-0.000012) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018842 / 0.037411 (-0.018569) | 0.061532 / 0.014526 (0.047006) | 0.073483 / 0.176557 (-0.103073) | 0.119426 / 0.737135 (-0.617709) | 0.075385 / 0.296338 (-0.220954) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.285544 / 0.215209 (0.070335) | 2.774256 / 2.077655 (0.696601) | 1.475719 / 1.504120 (-0.028401) | 1.353841 / 1.541195 (-0.187353) | 1.381891 / 1.468490 (-0.086599) | 0.570619 / 4.584777 (-4.014158) | 2.380300 / 3.745712 (-1.365412) | 2.788767 / 5.269862 (-2.481095) | 1.741790 / 4.565676 (-2.823886) | 0.061810 / 0.424275 (-0.362465) | 0.005004 / 0.007607 (-0.002603) | 0.334963 / 0.226044 (0.108918) | 3.286388 / 2.268929 (1.017459) | 1.831669 / 55.444624 (-53.612955) | 1.523372 / 6.876477 (-5.353105) | 1.581551 / 2.142072 (-0.560521) | 0.639642 / 4.805227 (-4.165585) | 0.117356 / 6.500664 (-6.383308) | 0.043277 / 0.075469 (-0.032192) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.973005 / 1.841788 (-0.868782) | 11.590148 / 8.074308 (3.515839) | 9.521262 / 10.191392 (-0.670130) | 0.143243 / 0.680424 (-0.537181) | 0.013529 / 0.534201 (-0.520672) | 0.285724 / 0.579283 (-0.293559) | 0.265642 / 0.434364 (-0.168721) | 0.366098 / 0.540337 (-0.174239) | 0.444410 / 1.386936 (-0.942526) |

</details>
PyArrow==latest

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005347 / 0.011353 (-0.006006) | 0.003797 / 0.011008 (-0.007212) | 0.050441 / 0.038508 (0.011933) | 0.032812 / 0.023109 (0.009703) | 0.281278 / 0.275898 (0.005379) | 0.304524 / 0.323480 (-0.018956) | 0.005039 / 0.007986 (-0.002946) | 0.002735 / 0.004328 (-0.001594) | 0.049184 / 0.004250 (0.044933) | 0.046751 / 0.037052 (0.009698) | 0.292093 / 0.258489 (0.033604) | 0.322087 / 0.293841 (0.028246) | 0.029775 / 0.128546 (-0.098771) | 0.010540 / 0.075646 (-0.065106) | 0.057927 / 0.419271 (-0.361345) | 0.054240 / 0.043533 (0.010707) | 0.281537 / 0.255139 (0.026398) | 0.298386 / 0.283200 (0.015186) | 0.019773 / 0.141683 (-0.121910) | 1.157161 / 1.452155 (-0.294994) | 1.210395 / 1.492716 (-0.282321) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.095098 / 0.018006 (0.077091) | 0.306952 / 0.000490 (0.306462) | 0.000211 / 0.000200 (0.000011) | 0.000050 / 0.000054 (-0.000004) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.022602 / 0.037411 (-0.014809) | 0.075242 / 0.014526 (0.060716) | 0.087134 / 0.176557 (-0.089422) | 0.127923 / 0.737135 (-0.609212) | 0.088645 / 0.296338 (-0.207693) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.304187 / 0.215209 (0.088978) | 2.977120 / 2.077655 (0.899465) | 1.663592 / 1.504120 (0.159473) | 1.527601 / 1.541195 (-0.013594) | 1.540121 / 1.468490 (0.071631) | 0.562492 / 4.584777 (-4.022285) | 2.473836 / 3.745712 (-1.271876) | 2.656782 / 5.269862 (-2.613080) | 1.754212 / 4.565676 (-2.811464) | 0.062330 / 0.424275 (-0.361945) | 0.005149 / 0.007607 (-0.002459) | 0.354905 / 0.226044 (0.128860) | 3.503587 / 2.268929 (1.234659) | 2.015682 / 55.444624 (-53.428943) | 1.744421 / 6.876477 (-5.132056) | 1.923120 / 2.142072 (-0.218952) | 0.652209 / 4.805227 (-4.153018) | 0.119406 / 6.500664 (-6.381258) | 0.042840 / 0.075469 (-0.032630) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 1.009164 / 1.841788 (-0.832624) | 12.379654 / 8.074308 (4.305346) | 10.408696 / 10.191392 (0.217304) | 0.141674 / 0.680424 (-0.538750) | 0.016815 / 0.534201 (-0.517386) | 0.292453 / 0.579283 (-0.286830) | 0.277577 / 0.434364 (-0.156787) | 0.325024 / 0.540337 (-0.215313) | 0.433181 / 1.386936 (-0.953755) |

</details>
</details>

![](https://cml.dev/watermark.png#b7a16a08c940e65397305aec5f1b484d91cee75a "CML watermark")