GitHub issue: AttributeError: module 'numpy' has no attribute 'object'. in Kaggle Notebook
Author: petrov826 (NONE)
State: closed | Created: 2024-04-05 14:31:48

### Describe the bug

# problem
I can't resample audio dataset in Kaggle Notebook. It looks like some code in `datasets` library use aliases that were deprecated in NumPy 1.20.

## code for resampling
```
from datasets import load_dataset, Audio
from transformers import AutoFeatureExtractor
from transformers import AutoModelForAudioClassification, TrainingArguments, Trainer

minds = load_dataset("PolyAI/minds14", name="en-US", split="train")

feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base")

def preprocess_function(examples):
    audio_arrays = [x["array"] for x in examples["audio"]]
    inputs = feature_extractor(
        audio_arrays, sampling_rate=feature_extractor.sampling_rate, max_length=16000, truncation=True
    )
    return inputs

dataset = dataset.map(preprocess_function, remove_columns="audio", batched=True, batch_size=100)
```

## the error I got
<details>
  <summary>Click to expand</summary>

```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[20], line 1
----> 1 dataset = dataset.map(preprocess_function, remove_columns="audio", batched=True, batch_size=100)
      2 dataset

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_dataset.py:1955, in Dataset.map(self, function, with_indices, with_rank, input_columns, batched, batch_size, drop_last_batch, remove_columns, keep_in_memory, load_from_cache_file, cache_file_name, writer_batch_size, features, disable_nullable, fn_kwargs, num_proc, suffix_template, new_fingerprint, desc)
   1952 disable_tqdm = not logging.is_progress_bar_enabled()
   1954 if num_proc is None or num_proc == 1:
-> 1955     return self._map_single(
   1956         function=function,
   1957         with_indices=with_indices,
   1958         with_rank=with_rank,
   1959         input_columns=input_columns,
   1960         batched=batched,
   1961         batch_size=batch_size,
   1962         drop_last_batch=drop_last_batch,
   1963         remove_columns=remove_columns,
   1964         keep_in_memory=keep_in_memory,
   1965         load_from_cache_file=load_from_cache_file,
   1966         cache_file_name=cache_file_name,
   1967         writer_batch_size=writer_batch_size,
   1968         features=features,
   1969         disable_nullable=disable_nullable,
   1970         fn_kwargs=fn_kwargs,
   1971         new_fingerprint=new_fingerprint,
   1972         disable_tqdm=disable_tqdm,
   1973         desc=desc,
   1974     )
   1975 else:
   1977     def format_cache_file_name(cache_file_name, rank):

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_dataset.py:520, in transmit_tasks.<locals>.wrapper(*args, **kwargs)
    518     self: "Dataset" = kwargs.pop("self")
    519 # apply actual function
--> 520 out: Union["Dataset", "DatasetDict"] = func(self, *args, **kwargs)
    521 datasets: List["Dataset"] = list(out.values()) if isinstance(out, dict) else [out]
    522 for dataset in datasets:
    523     # Remove task templates if a column mapping of the template is no longer valid

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_dataset.py:487, in transmit_format.<locals>.wrapper(*args, **kwargs)
    480 self_format = {
    481     "type": self._format_type,
    482     "format_kwargs": self._format_kwargs,
    483     "columns": self._format_columns,
    484     "output_all_columns": self._output_all_columns,
    485 }
    486 # apply actual function
--> 487 out: Union["Dataset", "DatasetDict"] = func(self, *args, **kwargs)
    488 datasets: List["Dataset"] = list(out.values()) if isinstance(out, dict) else [out]
    489 # re-apply format to the output

File /opt/conda/lib/python3.10/site-packages/datasets/fingerprint.py:458, in fingerprint_transform.<locals>._fingerprint.<locals>.wrapper(*args, **kwargs)
    452             kwargs[fingerprint_name] = update_fingerprint(
    453                 self._fingerprint, transform, kwargs_for_fingerprint
    454             )
    456 # Call actual function
--> 458 out = func(self, *args, **kwargs)
    460 # Update fingerprint of in-place transforms + update in-place history of transforms
    462 if inplace:  # update after calling func so that the fingerprint doesn't change if the function fails

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_dataset.py:2356, in Dataset._map_single(self, function, with_indices, with_rank, input_columns, batched, batch_size, drop_last_batch, remove_columns, keep_in_memory, load_from_cache_file, cache_file_name, writer_batch_size, features, disable_nullable, fn_kwargs, new_fingerprint, rank, offset, disable_tqdm, desc, cache_only)
   2354                 writer.write_table(batch)
   2355             else:
-> 2356                 writer.write_batch(batch)
   2357 if update_data and writer is not None:
   2358     writer.finalize()  # close_stream=bool(buf_writer is None))  # We only close if we are writing in a file

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_writer.py:507, in ArrowWriter.write_batch(self, batch_examples, writer_batch_size)
    505     col_try_type = try_features[col] if try_features is not None and col in try_features else None
    506     typed_sequence = OptimizedTypedSequence(batch_examples[col], type=col_type, try_type=col_try_type, col=col)
--> 507     arrays.append(pa.array(typed_sequence))
    508     inferred_features[col] = typed_sequence.get_inferred_type()
    509 schema = inferred_features.arrow_schema if self.pa_writer is None else self.schema

File /opt/conda/lib/python3.10/site-packages/pyarrow/array.pxi:236, in pyarrow.lib.array()

File /opt/conda/lib/python3.10/site-packages/pyarrow/array.pxi:110, in pyarrow.lib._handle_arrow_array_protocol()

File /opt/conda/lib/python3.10/site-packages/datasets/arrow_writer.py:184, in TypedSequence.__arrow_array__(self, type)
    182     out = numpy_to_pyarrow_listarray(data)
    183 elif isinstance(data, list) and data and isinstance(first_non_null_value(data)[1], np.ndarray):
--> 184     out = list_of_np_array_to_pyarrow_listarray(data)
    185 else:
    186     trying_cast_to_python_objects = True

File /opt/conda/lib/python3.10/site-packages/datasets/features/features.py:1174, in list_of_np_array_to_pyarrow_listarray(l_arr, type)
   1172 """Build a PyArrow ListArray from a possibly nested list of NumPy arrays"""
   1173 if len(l_arr) > 0:
-> 1174     return list_of_pa_arrays_to_pyarrow_listarray(
   1175         [numpy_to_pyarrow_listarray(arr, type=type) if arr is not None else None for arr in l_arr]
   1176     )
   1177 else:
   1178     return pa.array([], type=type)

File /opt/conda/lib/python3.10/site-packages/datasets/features/features.py:1163, in list_of_pa_arrays_to_pyarrow_listarray(l_arr)
   1160 null_indices = [i for i, arr in enumerate(l_arr) if arr is None]
   1161 l_arr = [arr for arr in l_arr if arr is not None]
   1162 offsets = np.cumsum(
-> 1163     [0] + [len(arr) for arr in l_arr], dtype=np.object
   1164 )  # convert to dtype object to allow None insertion
   1165 offsets = np.insert(offsets, null_indices, None)
   1166 offsets = pa.array(offsets, type=pa.int32())

File /opt/conda/lib/python3.10/site-packages/numpy/__init__.py:324, in __getattr__(attr)
    319     warnings.warn(
    320         f"In the future `np.{attr}` will be defined as the "
    321         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    323 if attr in __former_attrs__:
--> 324     raise AttributeError(__former_attrs__[attr])
    326 if attr == 'testing':
    327     import numpy.testing as testing

AttributeError: module 'numpy' has no attribute 'object'.
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe. 
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
```

</details>

### Steps to reproduce the bug

Run above code in Kaggle Notebook.

### Expected behavior

I can resample audio data without fail.

### Environment info

- `datasets` version: 2.1.0
- Platform: Linux-5.15.133+-x86_64-with-glibc2.31
- Python version: 3.10.13
- PyArrow version: 11.0.0
- Pandas version: 2.2.1