Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Reading Parquet / Feather Files

Pandas Reading Parquet / Feather Files

Parquet and Feather are two efficient columnar data storage formats designed for big data analysis and fast read/write scenarios. Compared with CSV, they offer better compression ratios and query performance.


Why use Parquet and Feather

Main advantages of columnar storage formats:

Feature CSV Parquet Feather
Storage Format Row-based Column-based Column-based
Read speed Slow Very fast Extremely fast
Write speed Medium fast Extremely fast
Compression ratio Low High Medium
Cross-language support Universal Java/Python/R Python/R

Parquet is an Apache Foundation project widely used in the big data ecosystem (Hadoop, Spark, DuckDB, etc.). Feather is the native implementation of the Arrow project, focused on extremely fast read/write speed.


Parquet file operations

Install dependencies



pip install pyarrow fastparquet

Both PyArrow and FastParquet are Python implementations of Parquet; PyArrow is the default, while FastParquet is faster in some scenarios.

Read a Parquet file

Example

import

pandas

as

pd

Read a Parquet file (using pyarrow by default)

df

=

pd.

read_parquet

(

"data.parquet"

)

Specify a particular engine

df

=

pd.

read_parquet

(

"data.parquet"

,

engine

=

"pyarrow"

)

df

=

pd.

read_parquet

(

"data.parquet"

,

engine

=

"fastparquet"

)

Read a remote Parquet file

df

=

pd.

read_parquet

(

"s3://bucket/data.parquet"

)

Read specific columns of a file (column pruning for better performance)

df

=

pd.

read_parquet

(

"data.parquet"

,

columns

=

[

"name"

,

"age"

,

"city"

]

)

print

(

df.

head

(

)

)

Write a Parquet file

Example

import

pandas

as

pd

Prepare test data

df

=

pd.

DataFrame

(

{

   

"id"

:

range

(

1

,

10001

)

,

   

"name"

:

[

"User"

str

(

i

)

for

i

in

range

(

1

,

10001

)

]

,

   

"age"

:

[

20

,

25

,

30

,

35

]

2500

,

   

"city"

:

[

"Beijing"

,

"Shanghai"

,

"Guangzhou"

,

"Shenzhen"

]

2500

,

   

"score"

:

[

round

(

i *

0.1

,

2

)

for

i

in

range

(

1

,

10001

)

]

}

)

Write a Parquet file (using the pyarrow engine by default)

df.

to_parquet

(

"data.parquet"

,

index

=

False

)

Specify the compression method (snappy is fast, gzip compresses better)

df.

to_parquet

(

"data_snappy.parquet"

,

compression

=

"snappy"

,

index

=

False

)

df.

to_parquet

(

"data_gzip.parquet"

,

compression

=

"gzip"

,

index

=

False

)

df.

to_parquet

(

"data_none.parquet"

,

compression

=

None

,

index

=

False

)

 

No compression

View the file size

import

os

print

(

"File size comparison:"

)

print

(

f

"Original CSV estimate: {len(df) * 50 / 1024 / 1024:.2f} MB"

)

print

(

f

"snappy compressed: {os.path.getsize('data_snappy.parquet') / 1024 / 1024:.2f} MB"

)

print

(

f

"gzip compressed: {os.path.getsize('data_gzip.parquet') / 1024 / 1024:.2f} MB"

)

print

(

f

"uncompressed: {os.path.getsize('data_none.parquet') / 1024 / 1024:.2f} MB"

)

Use a fast Parquet engine

Example

import

pandas

as

pd

import

time

Test the write performance of different engines

df

=

pd.

DataFrame

(

{

   

"id"

:

range

(

1

,

100001

)

,

   

"value"

:

range

(

1

,

100001

)

}

)

Write with the pyarrow engine

start

=

time

.

time

(

)

df.

to_parquet

(

"test_pyarrow.parquet"

,

engine

=

"pyarrow"

,

compression

=

"snappy"

)

print

(

f

"pyarrow write time: {time.time() - start:.3f}s"

)

Write with the fastparquet engine

start

=

time

.

time

(

)

df.

to_parquet

(

"test_fastparquet.parquet"

,

engine

=

"fastparquet"

,

compression

=

"snappy"

)

print

(

f

"fastparquet write time: {time.time() - start:.3f}s"

)


Feather file operations

Feather is the Python implementation of the Arrow project, focused on extremely fast in-memory read/write speed.

Install dependencies



pip install pyarrow

Read a Feather file

Example

import

pandas

as

pd

Read a Feather file

df

=

pd.

read_feather

(

"data.feather"

)

Read a version 2 Feather file (more general)

df

=

pd.

read_feather

(

"data.feather"

,

version

=

"2.0"

)

Read specific columns

df

=

pd.

read_feather

(

"data.feather"

,

columns

=

[

"name"

,

"age"

]

)

print

(

df.

head

(

)

)

Write a Feather file

Example

import

pandas

as

pd

Prepare test data

df

=

pd.

DataFrame

(

{

   

"id"

:

range

(

1

,

10001

)

,

   

"name"

:

[

"User"

str

(

i

)

for

i

in

range

(

1

,

10001

)

]

,

   

"value"

:

[

round

(

i *

0.1

,

2

)

for

i

in

range

(

1

,

10001

)

]

}

)

Write a Feather file

df.

to_feather

(

"data.feather"

)

LZ4 compression (faster)

df.

to_feather

(

"data_lz4.feather"

,

compression

=

"lz4"

)

ZSTD compression (higher compression ratio)

df.

to_feather

(

"data_zstd.feather"

,

compression

=

"zstd"

)

View the file size

import

os

print

(

f

"uncompressed: {os.path.getsize('data.feather') / 1024:.2f} KB"

)

print

(

f

"LZ4: {os.path.getsize('data_lz4.feather') / 1024:.2f} KB"

)

print

(

f

"ZSTD: {os.path.getsize('data_zstd.feather') / 1024:.2f} KB"

)


Performance comparison test

Example

import

pandas

as

pd

import

numpy

as

np

import

time

import

os

import

tempfile

Create test data

np.

random

.

seed

(

42

)

n_rows

=

1000000

df

=

pd.

DataFrame

(

{

   

"id"

:

range

(

n_rows

)

,

   

"category"

: np.

random

.

choice

(

[

"A"

,

"B"

,

"C"

,

"D"

]

,

n_rows

)

,

   

"value"

: np.

random

.

randn

(

n_rows

)

,

   

"flag"

: np.

random

.

choice

(

[

True

,

False

]

,

n_rows

)

,

   

"date"

: pd.

date_range

(

"2020-01-01"

,

periods

=

n_rows

,

freq

=

"1min"

)

}

)

Create a temporary directory

with

tempfile

.

TemporaryDirectory

(

)

as

tmpdir:

   

CSV write

    start

=

time

.

time

(

)

    df.

to_csv

(

f

"{tmpdir}/data.csv"

,

index

=

False

)

    csv_write

=

time

.

time

(

)

    start

=

time

.

time

(

)

    df_csv

=

pd.

read_csv

(

f

"{tmpdir}/data.csv"

)

    csv_read

=

time

.

time

(

)

   

Parquet write

    start

=

time

.

time

(

)

    df.

to_parquet

(

f

"{tmpdir}/data.parquet"

,

index

=

False

)

    pq_write

=

time

.

time

(

)

    start

=

time

.

time

(

)

    df_pq

=

pd.

read_parquet

(

f

"{tmpdir}/data.parquet"

)

    pq_read

=

time

.

time

(

)

   

Feather write

    start

=

time

.

time

(

)

    df.

to_feather

(

f

"{tmpdir}/data.feather"

)

    feather_write

=

time

.

time

(

)

    start

=

time

.

time

(

)

    df_feather

=

pd.

read_feather

(

f

"{tmpdir}/data.feather"

)

    feather_read

=

time

.

time

(

)

   

Output result

   

print

(

f

"{'format':<10} {'write time':<12} {'read time':<12} {'file size':<12}"

)

   

print

(

"-"

50

)

   

print

(

f

"{'CSV':<10} {csv_write:.3f}s{'':<5} {csv_read:.3f}s{'':<5} {os.path.getsize(f'{tmpdir}/data.csv')/1024/1024:.2f}MB"

)

   

print

(

f

"{'Parquet':<10} {pq_write:.3f}s{'':<5} {pq_read:.3f}s{'':<5} {os.path.getsize(f'{tmpdir}/data.parquet')/1024/1024:.2f}MB"

)

   

print

(

f

"{'Feather':<10} {feather_write:.3f}s{'':<5} {feather_read:.3f}s{'':<5} {os.path.getsize(f'{tmpdir}/data.feather')/1024/1024:.2f}MB"

)


Use cases and selection advice

Scenario Recommended format Reason
Big data analysis Parquet High compression ratio, fast columnar queries, broad ecosystem
Python/R data transfer Feather Extremely fast read/write, native Arrow support
Spark/Hadoop integration Parquet Standard format in the big data ecosystem
Data backup/archiving Parquet High compression ratio, supports schema evolution
Temporary data caching Feather Fastest read/write

Common questions

1. Error reading Parquet

Make sure pyarrow or fastparquet is installed:pip install pyarrow

2. Corrupted file

When writing, ensure data is fully flushed to disk; it is recommended to verify the file is readable afterward.

3. Version compatibility

Different versions of Parquet may have compatibility issues; it is recommended to pin a fixed version in your project.