r/Python • u/silicon_turfsurfer • 1d 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?
80
u/Gigawrench 1d 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.
33
u/lolcrunchy 1d 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.
13
u/Justbehind 1d 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.
6
u/funkdefied 1d ago
I use Polars not for the speed, but for the API. I don’t know what the perfect data language looks like. But I do know that Polars Expressions are part of that perfect language.
4
u/justanothersnek 1d ago edited 1d ago
Polars is awesome. If you have to work with PySpark on the regular, there's sqlframe, which allows you to use PySpark dataframe API on most SQL backends.
EDIT: Looks like polars maintainer just recently is interested in getting Polars support with it too!
12
u/Late-Photograph-1954 1d ago
I moved to DuckDb and SQL. My auditor much prefers to review Sql and AI crafts the most brilliant SQL queries instantly.
2
u/s4nc3s 1d ago
I been using polars for 3 years. Processing hundreds of millions of rows. Can process using the lazy frame, from ingestion of one or more large datasets to output, using one of the sink* methods, avoiding memory constraints. Know that polars also has a sql interface, but if you wish you can use DuckDB on top of a polars data frame.
9
u/king_escobar 1d ago
Pandas is obsolete technology, nobody should be using Pandas anymore. Polars is that much better.
1
u/Mcletters 1d ago
Any tips on ways to learn, tutorials, etc?
1
u/quocphu1905 1d ago
As a former pandas user that now uses polars/spark i gotta say congrats! I feel like the dev experience with polars/spark is a huge improvement over pandas once you get used to it.
Just did a migration from pandas to spark for one of our pipeline and the code is so much cleaner (there was a triple for loop in the old code to iterate over the pandas df ugh)
1
u/TheHighlander52 1d ago
As others have mentioned, heavily lean into the LazyFrame utilization. I think the regular eager frame uses similar functionality, but the lazy iterator has an incredible query optimizer that uses both predicate and projection push down that helps a lot when it comes time to actually collect the data.
1
u/srs96 6h ago
Polars is to pandas what uv is to pip
0
u/Independent-Shoe543 1d ago
How are people using polar with SQL databases as I understand it still needs converting back to a data frame before writing?
-2
u/Actual__Wizard 1d ago edited 1d ago
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.
-2
u/Alternative_Act_6548 1d ago
I still like pandas, I find the polars syntax verbose. Pandas is better for timeseries, a million rows doesn't seem like it should be a problem. Were you using pyarrow and cleaning up your datatypes? I certainly find much more reference material for pandas than polars