Polars
Polars is a fast DataFrame library written in Rust with Arrow as its foundation.
π‘ Learn more about how to get the dataset URLs in the List Parquet files guide.
Letβs start by grabbing the URLs to the train
split of the barilan/blog_authorship_corpus
dataset from the dataset viewer API:
r = requests.get("https://datasets-server.huggingface.co/parquet?dataset=barilan/blog_authorship_corpus")
j = r.json()
urls = [f['url'] for f in j['parquet_files'] if f['split'] == 'train']
urls
['https://huggingface.co./datasets/barilan/blog_authorship_corpus/resolve/refs%2Fconvert%2Fparquet/blog_authorship_corpus/train/0000.parquet',
'https://huggingface.co./datasets/barilan/blog_authorship_corpus/resolve/refs%2Fconvert%2Fparquet/blog_authorship_corpus/train/0001.parquet']
To read from a single Parquet file, use the read_parquet
function to read it into a DataFrame and then execute your query:
import polars as pl
df = (
pl.read_parquet("https://huggingface.co./datasets/barilan/blog_authorship_corpus/resolve/refs%2Fconvert%2Fparquet/blog_authorship_corpus/train/0000.parquet")
.groupby("horoscope")
.agg(
[
pl.count(),
pl.col("text").str.n_chars().mean().alias("avg_blog_length")
]
)
.sort("avg_blog_length", descending=True)
.limit(5)
)
print(df)
shape: (5, 3)
βββββββββββββ¬ββββββββ¬ββββββββββββββββββ
β horoscope β count β avg_blog_length β
β --- β --- β --- β
β str β u32 β f64 β
βββββββββββββͺββββββββͺββββββββββββββββββ‘
β Aquarius β 34062 β 1129.218836 β
β Cancer β 41509 β 1098.366812 β
β Capricorn β 33961 β 1073.2002 β
β Libra β 40302 β 1072.071833 β
β Leo β 40587 β 1064.053687 β
βββββββββββββ΄ββββββββ΄ββββββββββββββββββ
To read multiple Parquet files - for example, if the dataset is sharded - youβll need to use the concat
function to concatenate the files into a single DataFrame:
import polars as pl
df = (
pl.concat([pl.read_parquet(url) for url in urls])
.groupby("horoscope")
.agg(
[
pl.count(),
pl.col("text").str.n_chars().mean().alias("avg_blog_length")
]
)
.sort("avg_blog_length", descending=True)
.limit(5)
)
print(df)
shape: (5, 3)
βββββββββββββββ¬ββββββββ¬ββββββββββββββββββ
β horoscope β count β avg_blog_length β
β --- β --- β --- β
β str β u32 β f64 β
βββββββββββββββͺββββββββͺββββββββββββββββββ‘
β Aquarius β 49568 β 1125.830677 β
β Cancer β 63512 β 1097.956087 β
β Libra β 60304 β 1060.611054 β
β Capricorn β 49402 β 1059.555261 β
β Sagittarius β 50431 β 1057.458984 β
βββββββββββββββ΄ββββββββ΄ββββββββββββββββββ
Lazy API
Polars offers a lazy API that is more performant and memory-efficient for large Parquet files. The LazyFrame API keeps track of what you want to do, and itβll only execute the entire query when youβre ready. This way, the lazy API doesnβt load everything into RAM beforehand, and it allows you to work with datasets larger than your available RAM.
To lazily read a Parquet file, use the scan_parquet
function instead. Then, execute the entire query with the collect
function:
import polars as pl
q = (
pl.scan_parquet("https://huggingface.co./datasets/barilan/blog_authorship_corpus/resolve/refs%2Fconvert%2Fparquet/blog_authorship_corpus/train/0000.parquet")
.groupby("horoscope")
.agg(
[
pl.count(),
pl.col("text").str.n_chars().mean().alias("avg_blog_length")
]
)
.sort("avg_blog_length", descending=True)
.limit(5)
)
df = q.collect()