r/Python • • 4h ago

Discussion Polars for data-heavy datasets

A few months back I switched from pandas to polars because some of the datasets I needed to work with were hitting the million row mark and beyond. It has taken quite some time to get used to the more verbose polars dataframe methods but I have to say I am really pleased with the execution speed improvement.

I also moved away from applying filters and calculating, migrating to the group_by.agg() for performing row calculations - HUGE difference in speed.

For those of you who haven't heard of polars, it is multi-threaded whereas pandas is single-threaded, so for large datasets it is faster. I would never have made the switch were it not for large datasets, and I frequently convert to pandas dataframes when debugging.

Anyone else have similar experiences or have resisted the urge to migrate away from pandas?

38 Upvotes

12 comments sorted by

38

u/Gigawrench 4h ago

My experience is that, once you get familiar with the polars APIs, the developer ergonomics for dataframe manipulation are so much more intuitive and expressive than pandas. Big fan of the lazyframe functionality where you can lean on the built-in optimisation engine to do a lot of the performance tuning for a given operation.

4

u/silicon_turfsurfer 3h ago edited 3h ago

Coming from pandas I haven't yet used the lazyframes, but after doing a little reading I will look into it. I may have unwittingly used it during the application of the group_by.agg() calculations (maybe not though because I was using a dataframe and not lazyframe).

3

u/TheFlamingDiceAgain 1h ago

I also highly recommend using LazyFrames, they're what brought me to polars in the first place. They enable you to work with datasets that are larger than memory, in my case a couple TB, and they usually faster for in-memory operations

16

u/lolcrunchy 3h ago

This is very timely. I just rewrote a pandas data process to polars LazyFrames with collect_all() at the end. The execution time went from 4-5 hours to a few minutes.

4

u/Justbehind 1h ago

Polars is superior in every way. Easier to write and read, and performance is so much better.

We are working with datasets in the 100s of millions of rows on our laptops. Effectively.

5

u/king_escobar 3h ago

Pandas is obsolete technology, nobody should be using Pandas anymore. Polars is that much better.

8

u/sinnayre 3h ago

Eh. Beg to differ. Geospatial operations just moved out of beta; multiple libraries now, but that’s a recent development. Prior to this, your only real option was to stick to pandas. There’s a whole lot of legacy code for geospatial companies to migrate. And if you work with one of the major players in this space, ESRI, there’s still no roadmap to polars support.

3

u/Late-Photograph-1954 2h ago

I moved to DuckDb and SQL. My auditor much prefers to review Sql and AI crafts the most brilliant SQL queries instantly.

1

u/Mcletters 3h ago

Any tips on ways to learn, tutorials, etc?

2

u/TheFlamingDiceAgain 1h ago

I found the Polars documentation to be pretty good. That got me through the basics and then I can just look up specific things when I need it

1

u/Actual__Wizard 1h ago edited 1h ago

A few months back I switched from pandas to polars because some of the datasets

Yes, I'm working with data that is deep into the trillions of rows.

After years of working with both pandas and polars, dealing with them breaking under the load of data that big, or being mega slow, I built my own database tech from the ground up to handle tables up to int(64) in length and the system supports up to int(64) columns in theory.

A lot of sacrifices had to be made to get the performance level up to "feels like it's instant."

I want to be clear, that I felt like Polars was fine up until you get into the multiple millions of rows, or at least in my application, the performance is "not good enough for my application with data sets beyond like 10m+ rows." I need like 1t rows/sec traverse speed (bsearch does that) w/ like 100k+ rows/sec of retrieval (structured data does that.) So, it's "double structured" and it was a massive pain in the butt to build it.