4.8. polars: A fast, fancy pandas alternative#

Most data folks use pandas. However, there is an alternative that I just wanted to bring to your attention. polars is a faster and, perhaps, more modern way to handle data in Python. Still, pandas is ubiquitous, so I wanted to start with that.

Here’s the user’s guide. I’m not going to go through every command here. As always, think about what you want to do. Sketch it out. Then, look for the syntax to do the job.

Why not use pandas? Here’s the author of pandas on why pandas isn’t always the best tool for data manipulation. We’re getting more advanced here, worrying about speed, being closer to the “metal”, etc.

Some people, especially those coming to Python from other languages, are suggesting that you just start with polars instead.

[Coding for Economists] discusses alternatives to pandas, like polars.

We can insall polars using pip the usual way. Don’t forget to use ! pip in Google Colab.

pip install polars

You’ll see my basic import statement below.

# Set-up

import polars as pl
import numpy as np
import pandas as pd

df = pl.read_csv('https://raw.githubusercontent.com/aaiken1/fin-data-analysis-python/main/data/ncbreweries.csv')

type(df)
polars.dataframe.frame.DataFrame

See what I did there? That was pl.read_csv from polars. I’ve created a polars DataFrame.

Now, you can read the manual to find out all of things that you can do!

df.describe()
shape: (7, 8)
describeNameCityTypeBeer CountEstStatusURL
strstrstrstrf64f64strstr
"count""251""251""251"251.0251.0"251""251"
"null_count""0""0""0"0.00.0"0""0"
"mean"nullnullnull32.9601592012.155378nullnull
"std"nullnullnull43.7233858.749158nullnull
"min""217 Brew Works..."Aberdeen""Brewpub"1.01900.0"Active""https://www.ra...
"max""Zebulon Artisa..."Winston-Salem""Microbrewery"424.02018.0"Closed""https://www.ra...
"median"nullnullnull18.02014.0nullnull

Looks a little different. I like it.

You can select certain columns, as well. You can filter, do “group bys”. All the usual things.

df.select(
    pl.col(['Name', 'City'])
)
shape: (251, 2)
NameCity
strstr
"217 Brew Works..."Wilson"
"3rd Rock Brewi..."Trenton"
"7 Clans Brewin..."Cherokee"
"Andrews Brewin..."Andrews"
"Angry Troll Br..."Elkin"
"Appalachian Mo..."Boone"
"Archetype Brew..."Asheville"
"Asheville Brew..."Asheville"
"Ass Clown Brew..."Cornelius"
"Aviator Brewin..."Fuquay Varina"
"Barking Duck B..."Mint Hill"
"Barrel Culture..."Durham"
"Greenshields B..."Raleigh"
"Hams Restauran..."Greenville"
"Heinzelmannche..."Sylva"
"High Tide Brew..."Jacksonville"
"Hosanna Brewin..."Fuqauy Varina"
"Jack of the Wo..."Asheville"
"Loe's Brewing ..."Hickory"
"Sweet Taters""Rocky Mount"
"Triangle Brewi..."Durham"
"White Rabbit B..."Angier"
"Williamsville ..."Farmville"
"Wolf Beer Comp..."Wilmington"
df.filter(
    pl.col("Beer Count").is_between(10, 100))
shape: (177, 7)
NameCityTypeBeer CountEstStatusURL
strstrstri64i64strstr
"217 Brew Works..."Wilson""Microbrewery"102017"Active""https://www.ra...
"3rd Rock Brewi..."Trenton""Microbrewery"122016"Active""https://www.ra...
"Andrews Brewin..."Andrews""Microbrewery"182014"Active""https://www.ra...
"Appalachian Mo..."Boone""Microbrewery"782013"Active""https://www.ra...
"Archetype Brew..."Asheville""Microbrewery"152017"Active""https://www.ra...
"Asheville Brew..."Asheville""Brewpub"872003"Active""https://www.ra...
"Aviator Brewin..."Fuquay Varina""Microbrewery"592008"Active""https://www.ra...
"Barking Duck B..."Mint Hill""Microbrewery"162014"Active""https://www.ra...
"Barrel Culture..."Durham""Microbrewery"292017"Active""https://www.ra...
"Bayne Brewing ..."Cornelius""Microbrewery"162014"Active""https://www.ra...
"BearWaters Bre..."Canton""Microbrewery"392012"Active""https://www.ra...
"Beer Army Comb..."Trenton""Microbrewery"112012"Active""https://www.ra...
"Chesapeake Bay..."Raleigh""Microbrewery"141999"Closed""https://www.ra...
"Craggie Brewin..."Asheville""Microbrewery"302009"Closed""https://www.ra...
"Draft Line Bre..."Fuquay-Varina""Microbrewery"192014"Closed""https://www.ra...
"Four Friends B..."Charlotte""Microbrewery"112009"Closed""https://www.ra...
"G2B Gastropub ..."Durham""Brewpub/Brewer...182015"Closed""https://www.ra...
"Greenshields B..."Raleigh""Microbrewery"151999"Closed""https://www.ra...
"Hams Restauran..."Greenville""Brewpub"262003"Closed""https://www.ra...
"Heinzelmannche..."Sylva""Microbrewery"182005"Closed""https://www.ra...
"Hosanna Brewin..."Fuqauy Varina""Brewpub"122013"Closed""https://www.ra...
"Jack of the Wo..."Asheville""Brewpub"132004"Closed""https://www.ra...
"Triangle Brewi..."Durham""Microbrewery"212007"Closed""https://www.ra...
"White Rabbit B..."Angier""Microbrewery"192013"Closed""https://www.ra...
df.filter(
    (pl.col('Beer Count') <= 10) & (pl.col('Status') != "Closed")
)
shape: (54, 7)
NameCityTypeBeer CountEstStatusURL
strstrstri64i64strstr
"217 Brew Works..."Wilson""Microbrewery"102017"Active""https://www.ra...
"7 Clans Brewin..."Cherokee""Client Brewer"12018"Active""https://www.ra...
"Angry Troll Br..."Elkin""Microbrewery"82017"Active""https://www.ra...
"Bear Creek Bre..."Bear Creek""Microbrewery"62012"Active""https://www.ra...
"Beech Mountain..."Beech Mountain..."Microbrewery"72014"Active""https://www.ra...
"Bill's Front P..."Wilmington""Brewpub/Brewer...102016"Active""https://www.ra...
"Biltmore Brewi..."Asheville""Client Brewer"42010"Active""https://www.ra...
"BottleTree Bee..."Tryon""Client Brewer"22010"Active""https://www.ra...
"Bright Light B..."Fayetteville""Microbrewery"52018"Active""https://www.ra...
"Broomtail Craf..."Wilmington""Microbrewery"102014"Active""https://www.ra...
"Bull City Cide..."Durham""Commercial Bre...92014"Active""https://www.ra...
"Bull Durham Be..."Durham""Microbrewery"72015"Active""https://www.ra...
"Slammin' Sam B..."Pinehurst""Client Brewer"12012"Active""https://www.ra...
"Southern Range..."Monroe""Microbrewery"62016"Active""https://www.ra...
"Tarboro Brewin..."Tarboro""Microbrewery"82016"Active""https://www.ra...
"Tek Mountain B..."Wilmington""Microbrewery"72016"Active""https://www.ra...
"The Mason Jar ..."Fuquay Varina""Microbrewery"52017"Active""https://www.ra...
"Thristy Souls ..."Mount Airy""Brewpub/Brewer...102018"Active""https://www.ra...
"Tobacco Road S..."Raleigh""Brewpub"72017"Active""https://www.ra...
"Valley River B..."Murphy""Brewpub"82017"Active""https://www.ra...
"Vicious Fishes..."Angier""Microbrewery"12017"Active""https://www.ra...
"Waterline Brew..."Wilmington""Microbrewery"62015"Active""https://www.ra...
"Winding Creek ..."Columbus""Microbrewery"92017"Active""https://www.ra...
"York Chester B..."Belmont""Microbrewery"82016"Active""https://www.ra...
df.groupby("Type").count().sort(by="count", descending=True)
shape: (5, 2)
Typecount
stru32
"Microbrewery"165
"Brewpub/Brewer...41
"Brewpub"33
"Client Brewer"9
"Commercial Bre...3

Not bad!