Arrow also provides support for various formats to get those tabular data in and out of disk and networks. Most commonly used formats are Parquet (Reading and Writing the Apache Parquet Format) and the IPC format (Streaming, Serialization, and IPC).
Creating Arrays and Tables¶
Arrays in Arrow are collections of data of uniform type. That allows Arrow to use the best performing implementation to store the data and perform computations on it. So each array is meant to have data and a type
In : import pyarrow as pa In : days = pa.array([1, 12, 17, 23, 28], type=pa.int8())
Multiple arrays can be combined in tables to form the columns in tabular data when attached to a column name
In : months = pa.array([1, 3, 5, 7, 1], type=pa.int8()) In : years = pa.array([1990, 2000, 1995, 2000, 1995], type=pa.int16()) In : birthdays_table = pa.table([days, months, years], ...: names=["days", "months", "years"]) ...: In : birthdays_table Out: pyarrow.Table days: int8 months: int8 years: int16 ---- days: [[1,12,17,23,28]] months: [[1,3,5,7,1]] years: [[1990,2000,1995,2000,1995]]
See Data Types and In-Memory Data Model for more details.
Saving and Loading Tables¶
Once you have tabular data, Arrow provides out of the box the features to save and restore that data for common formats like Parquet:
In : import pyarrow.parquet as pq In : pq.write_table(birthdays_table, 'birthdays.parquet')
Once you have your data on disk, loading it back is a single function call, and Arrow is heavily optimized for memory and speed so loading data will be as quick as possible
In : reloaded_birthdays = pq.read_table('birthdays.parquet') In : reloaded_birthdays Out: pyarrow.Table days: int8 months: int8 years: int16 ---- days: [[1,12,17,23,28]] months: [[1,3,5,7,1]] years: [[1990,2000,1995,2000,1995]]
Arrow ships with a bunch of compute functions that can be applied to its arrays and tables, so through the compute functions it’s possible to apply transformations to the data
In : import pyarrow.compute as pc In : pc.value_counts(birthdays_table["years"]) Out: <pyarrow.lib.StructArray object at 0x7f9cbc5d0280> -- is_valid: all not null -- child 0 type: int16 [ 1990, 2000, 1995 ] -- child 1 type: int64 [ 1, 2, 2 ]
See Compute Functions for a list of available compute functions and how to use them.
Working with large data¶
Arrow also provides the
pyarrow.dataset API to work with
large data, which will handle for you partitioning of your data in
In : import pyarrow.dataset as ds In : ds.write_dataset(birthdays_table, "savedir", format="parquet", ....: partitioning=ds.partitioning( ....: pa.schema([birthdays_table.schema.field("years")]) ....: )) ....:
Loading back the partitioned dataset will detect the chunks
In : birthdays_dataset = ds.dataset("savedir", format="parquet", partitioning=["years"]) In : birthdays_dataset.files Out: ['savedir/1990/part-0.parquet', 'savedir/1995/part-0.parquet', 'savedir/2000/part-0.parquet']
and will lazily load chunks of data only when iterating over them
In : import datetime In : current_year = datetime.datetime.utcnow().year In : for table_chunk in birthdays_dataset.to_batches(): ....: print("AGES", pc.subtract(current_year, table_chunk["years"])) ....: AGES [ 32 ] AGES [ 27, 27 ] AGES [ 22, 22 ]
For further details on how to work with big datasets, how to filter them, how to project them, etc., refer to Tabular Datasets documentation.