Pandas Audio Methods
Audio methods for pandas dataframes using soundfile.
Features:
- Use
sf.SoundFileobjects in pandas dataframes - Call
sf.SoundFilemethods on a column, for example:.read().truncate().seek()
- Save dataframes with
sf.SoundFileobjects to Parquet - Process Audios in parallel with Dask
- Manipulate Audio datasets from Hugging Face
Installation
pip install pandas-audio-methods
Usage
You can open Audios as sf.SoundFile objects using the .open() method.
Once the Audios are opened, you can call any sf.SoundFile method:
import pandas as pd
from pandas_audio_methods import SFMethods
pd.api.extensions.register_series_accessor("sf")(SFMethods)
df = pd.DataFrame({"file_path": ["path/to/audio.wav"]})
df["audio"] = df["file_path"].sf.open()
# 0 SoundFile('path/to/audio.wav', mode='r', sampl...
# Name: audio, dtype: object, soundfile methods enabled
Use with librosa:
import librosa
df["audio"] = [librosa.load(audio, sr=16_000) for audio in df["audio"]]
df["audio"] = df["audio"].sf.write()
# 0 SoundFile(<_io.BytesIO object at 0x11b747ba0>,...
# Name: audio, dtype: object, soundfile methods enabled
Here is how to enable sf methods for sf.SoundFiles created manually:
df = pd.DataFrame({"audio": [sf.SoundFile.open("path/to/audio.wav")]})
df["audio"] = df["audio"].sf.enable()
# 0 SoundFile('path/to/audio.wav', mode='r', sampl...
# Name: Audio, dtype: object, soundfile methods enabled
Save
You can save a dataset of sf.SoundFiles to Parquet:
# Save
df = pd.DataFrame({"file_path": ["path/to/audio.wav"]})
df["audio"] = df["file_path"].sf.open()
df.to_parquet("data.parquet")
# Later
df = pd.read_parquet("data.parquet")
df["audio"] = df["audio"].sf.enable()
This doesn't just save the paths to the Audio files, but the actual Audios themselves !
Under the hood it saves dictionaries of {"bytes": <bytes of the Audio file>, "path": <path or name of the Audio file>}.
The Audios are saved as bytes using their Audio encoding or PNG by default. Anyone can load the Parquet data even without pandas-audio-methods since it doesn't rely on extension types.
Note: if you created the sf.SoundFiles manually, don't forget to enable the sf methods to enable saving to Parquet.
Run in parallel
Dask DataFrame parallelizes pandas to handle large datasets. It enables faster local processing with multiprocessing as well as distributed large scale processing. Dask mimics the pandas API:
import dask.dataframe as dd
from distributed import Client
from pandas_audio_methods import SFMethods
dd.extensions.register_series_accessor("sf")(SFMethods)
if __name__ == "__main__":
client = Client()
df = dd.read_csv("path/to/large/dataset.csv")
df = df.repartition(npartitions=1000) # divide the processing in 1000 jobs
df["audio"] = df["file_path"].sf.open()
df["audio"].head(1)
# 0 SoundFile('path/to/audio.wav', mode='r', sampl...
# Name: audio, dtype: object, soundfile methods enabled
df.to_parquet("data_folder")
Hugging Face support
Most Audio datasets in Parquet format on Hugging Face are compatible with pandas-audio-methods. For example you can load the microset of the People's Speech dataset:
df = pd.read_parquet("hf://datasets/MLCommons/peoples_speech/microset/train-00000-of-00001.parquet")
df["audio"] = df["audio"].sf.enable()
You can also use the datasets library, here is an example on the jlvdoorn/atco2-asr dataset for automatic speech recognition:
from datasets import load_dataset
df = load_dataset("jlvdoorn/atco2-asr", split="train").to_pandas()
df["audio"] = df["audio"].sf.enable()
Datasets created with pandas-audio-methods and saved to Parquet are also compatible with the Dataset Viewer on Hugging Face and the datasets library:
df.to_parquet("hf://datasets/username/dataset_name/train.parquet")
Metadata
Release files for pandas-audio-methods 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pandas_audio_methods-0.0.1.tar.gz | 8.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pandas_audio_methods-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.9 kB
Release files / pandas_audio_methods-0.0.1.tar.gz
| Download URL | pandas_audio_methods-0.0.1.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff2447f1f2f0429b4e365b9cc63852453292026bdc97d7a8607606c2337ead36
|
|
BLAKE2b-256 checksum How to use checksums |
5f8cbd3aa2ec4729e93a4a37cd99c1448a2efe7a136f3c7ea4581efc2addb160
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.12.2 Darwin/23.4.0
|
Release files / pandas_audio_methods-0.0.1-py3-none-any.whl
| Download URL | pandas_audio_methods-0.0.1-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5fb62d7be344803e1ae0881d7cc905626c2ac7140042dabc56bb499ceea3170b
|
|
BLAKE2b-256 checksum How to use checksums |
e7bc296ee2d31ad80de2d713dfd72c429578f11d142da4f32162f9b8c4db60d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.12.2 Darwin/23.4.0
|