Skip to content

Commit

Permalink
fix column order issue in cast
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoestq committed Sep 29, 2020
1 parent a3576b4 commit 31ff95d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/datasets/arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,18 +600,19 @@ def cast_(self, features: Features):
Args:
features (:class:`datasets.Features`): New features to cast the dataset to.
The name and order of the fields in the features must match the current column names.
The name of the fields in the features must match the current column names.
The type of the data must also be convertible from one type to the other.
For non-trivial conversion, e.g. string <-> ClassLabel you should use :func:`map` to update the Dataset.
"""
if list(features) != self._data.column_names:
if sorted(features) != sorted(self._data.column_names):
raise ValueError(
f"The columns in features ({list(features)}) must be identical and in the same order "
f"The columns in features ({list(features)}) must be identical "
f"as the columns in the dataset: {self._data.column_names}"
)

self._info.features = features
schema = pa.schema(features.type)
type = features.type
schema = pa.schema({col_name: type[col_name].type for col_name in self._data.column_names})
self._data = self._data.cast(schema)

@fingerprint(inplace=True)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def reduce_ex(self):

datasets.arrow_dataset.logger.__reduce_ex__ = reduce_ex

def _create_dummy_dataset(self, in_memory: bool, tmp_dir: str, multiple_columns=False):
def _create_dummy_dataset(self, in_memory: bool, tmp_dir: str, multiple_columns=False) -> Dataset:
if multiple_columns:
data = {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]}
dset = Dataset.from_dict(data)
Expand Down Expand Up @@ -289,6 +289,7 @@ def test_cast_(self, in_memory):
dset = self._create_dummy_dataset(in_memory, tmp_dir, multiple_columns=True)
features = dset.features
features["col_1"] = Value("float64")
features = Features({k: features[k] for k in list(features)[::-1]})
fingerprint = dset._fingerprint
dset.cast_(features)
self.assertEqual(dset.num_columns, 2)
Expand Down

1 comment on commit 31ff95d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show benchmarks

PyArrow==0.17.1

Show updated benchmarks!

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.018716 / 0.011353 (0.007364) 0.015876 / 0.011008 (0.004867) 0.047936 / 0.038508 (0.009428) 0.030668 / 0.023109 (0.007558) 0.213584 / 0.275898 (-0.062314) 0.234660 / 0.323480 (-0.088819) 0.008590 / 0.007986 (0.000605) 0.004763 / 0.004328 (0.000435) 0.006761 / 0.004250 (0.002510) 0.046665 / 0.037052 (0.009612) 0.209357 / 0.258489 (-0.049132) 0.234106 / 0.293841 (-0.059735) 0.167923 / 0.128546 (0.039377) 0.132798 / 0.075646 (0.057151) 0.443962 / 0.419271 (0.024690) 0.526058 / 0.043533 (0.482526) 0.208584 / 0.255139 (-0.046555) 0.227891 / 0.283200 (-0.055309) 0.081118 / 0.141683 (-0.060565) 1.802240 / 1.452155 (0.350085) 1.947877 / 1.492716 (0.455161)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.039123 / 0.037411 (0.001712) 0.020798 / 0.014526 (0.006272) 0.046807 / 0.176557 (-0.129750) 0.090114 / 0.737135 (-0.647021) 0.026061 / 0.296338 (-0.270278)

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.219989 / 0.215209 (0.004780) 2.181316 / 2.077655 (0.103662) 1.281447 / 1.504120 (-0.222673) 1.146077 / 1.541195 (-0.395117) 1.185829 / 1.468490 (-0.282661) 7.063081 / 4.584777 (2.478304) 6.014878 / 3.745712 (2.269166) 8.455950 / 5.269862 (3.186088) 7.453085 / 4.565676 (2.887408) 0.713061 / 0.424275 (0.288786) 0.012209 / 0.007607 (0.004602) 0.244241 / 0.226044 (0.018197) 2.546959 / 2.268929 (0.278031) 1.751462 / 55.444624 (-53.693162) 1.580073 / 6.876477 (-5.296403) 1.597686 / 2.142072 (-0.544387) 7.085909 / 4.805227 (2.280682) 6.637451 / 6.500664 (0.136787) 7.338226 / 0.075469 (7.262757)

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) 14.779298 / 1.841788 (12.937511) 14.389284 / 8.074308 (6.314976) 16.584611 / 10.191392 (6.393219) 0.624289 / 0.680424 (-0.056135) 0.305167 / 0.534201 (-0.229034) 0.846988 / 0.579283 (0.267705) 0.646757 / 0.434364 (0.212393) 0.832189 / 0.540337 (0.291852) 1.667488 / 1.386936 (0.280552)
PyArrow==1.0
Show updated benchmarks!

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.021780 / 0.011353 (0.010427) 0.015887 / 0.011008 (0.004878) 0.049587 / 0.038508 (0.011078) 0.032394 / 0.023109 (0.009284) 0.346234 / 0.275898 (0.070336) 0.374229 / 0.323480 (0.050749) 0.006573 / 0.007986 (-0.001413) 0.004407 / 0.004328 (0.000079) 0.006731 / 0.004250 (0.002480) 0.049408 / 0.037052 (0.012356) 0.349687 / 0.258489 (0.091197) 0.372024 / 0.293841 (0.078184) 0.165505 / 0.128546 (0.036959) 0.131657 / 0.075646 (0.056010) 0.493773 / 0.419271 (0.074501) 0.436100 / 0.043533 (0.392568) 0.343061 / 0.255139 (0.087922) 0.354879 / 0.283200 (0.071679) 0.092500 / 0.141683 (-0.049183) 1.876001 / 1.452155 (0.423846) 1.897254 / 1.492716 (0.404538)

Benchmark: benchmark_indices_mapping.json

metric select shard shuffle sort train_test_split
new / old (diff) 0.043392 / 0.037411 (0.005981) 0.022992 / 0.014526 (0.008466) 0.053475 / 0.176557 (-0.123082) 0.084654 / 0.737135 (-0.652481) 0.044391 / 0.296338 (-0.251947)

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.285062 / 0.215209 (0.069853) 2.883263 / 2.077655 (0.805608) 1.986292 / 1.504120 (0.482173) 1.861221 / 1.541195 (0.320027) 1.857365 / 1.468490 (0.388875) 7.179135 / 4.584777 (2.594358) 6.026618 / 3.745712 (2.280906) 8.289552 / 5.269862 (3.019690) 7.234481 / 4.565676 (2.668805) 0.708698 / 0.424275 (0.284422) 0.012028 / 0.007607 (0.004421) 0.293227 / 0.226044 (0.067183) 3.183559 / 2.268929 (0.914630) 2.372377 / 55.444624 (-53.072248) 2.233443 / 6.876477 (-4.643033) 2.259325 / 2.142072 (0.117253) 7.076631 / 4.805227 (2.271403) 5.566036 / 6.500664 (-0.934628) 7.239083 / 0.075469 (7.163614)

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) 14.540051 / 1.841788 (12.698263) 15.645436 / 8.074308 (7.571128) 15.843777 / 10.191392 (5.652385) 0.781548 / 0.680424 (0.101124) 0.591969 / 0.534201 (0.057768) 0.828280 / 0.579283 (0.248997) 0.629743 / 0.434364 (0.195379) 0.801903 / 0.540337 (0.261566) 1.632661 / 1.386936 (0.245725)

Please sign in to comment.