r/Python • u/silicon_turfsurfer • 17h 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?
28
u/lolcrunchy 17h 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/JambaJuiceIsAverage 9h ago
Same. Iterated over a folder full of gigantic CSV files to get the most recent record received for my unique key. Took so long in pandas I wasn't sure if it was even running. Polars? 15 seconds.
6
u/funkdefied 7h 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.
10
u/Justbehind 15h 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.
11
u/Late-Photograph-1954 15h ago
I moved to DuckDb and SQL. My auditor much prefers to review Sql and AI crafts the most brilliant SQL queries instantly.
8
u/nightcracker 8h ago
Polars also supports SQL now, with rapidly improving support with each release.
3
2
u/s4nc3s 10h 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.
2
8
u/king_escobar 17h ago
Pandas is obsolete technology, nobody should be using Pandas anymore. Polars is that much better.
13
u/sinnayre 16h 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.
1
u/CaptainFoyle 11h ago
There's no geopolars yet, is there?
3
u/johnnymo1 11h ago
There is, but it’s very early stages. Blocked by upstream issues for a while, now unblocked with development being taken over by the core polars team.
1
1
u/Mcletters 16h ago
Any tips on ways to learn, tutorials, etc?
5
u/TheFlamingDiceAgain 15h 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/Megatron_McLargeHuge 4h ago
The AI sidebar in a Marimo notebook makes it easy to ask for things or to convert pandas to polars.
1
u/Top-Bathroom8480 1h ago
Start straight from the official Polars User Guide, specifically the Coming from Pandas section, and force yourself to stop reaching for .apply(). If you rewrite one real messy pandas script you already understand using .lazy(), pl.col(), and .with_columns(), everything clicks in an afternoon because you can compare the outputs side by side.
1
u/quocphu1905 8h 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/justanothersnek 8h ago
You'll probably be interested in sqlframe since you work with pyspark a lot.
1
1
u/TheHighlander52 8h 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/Actual__Wizard 14h ago edited 14h 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.
4
u/CaptainFoyle 11h ago
Ok
0
u/Actual__Wizard 11h ago
That's a really weird comment. We live in an era where things like atomic scale simulations of cells are totally possible, but we don't have data tech to do stuff like that. It breaks instead of doing what we need it to do. It's really frustrating. It seems like people designed these systems based upon their goals and not based upon what exists in reality.
1
1
u/justanothersnek 9h ago
There's also sqlframe you could use too. It is PySpark API on top of popular SQL backends.
0
u/Independent-Shoe543 12h ago
How are people using polar with SQL databases as I understand it still needs converting back to a data frame before writing?
1
u/SV-97 1h ago
Polars isn't directly involved with getting data in and out of the actual database memory, it only interfaces with the DB indirectly via a database engine. Polars uses apache arrow as a data format internally and whatever an engine does with that is up to the engine, and just what exactly that engine *can* do is up to the database. Sqlite for example has a row-oriented storage format, so there is necessarily going to be some work involved (even if that work potentially happens internally with sqlite at the end --- I'm not sure if it supports arrow natively at this point) when going from polars to sqlite and vice versa. But you really never convert *to* a dataframe on writing: dataframes are polars-side abstraction on top of arrow, so if you come from polars then you'll already have a dataframe.
(When using polars' lazy API you can sometimes have operations such as filters etc. pushed to the database side, but that's still considered to be an unstable feature)
•
u/Independent-Shoe543 27m ago
I think you're talking about the underlying database abstraction rather than the practicalities of actually writing to it. I agree that Polars doesn't inherently need to convert to pandas, but in my case, writing to MSSQL required converting the Polars DataFrame back to pandas because the SQLAlchemy/pyodbc route I was using expected a pandas DataFrame. So while Polars might use Arrow internally, that doesn't mean the database connector necessarily supports Arrow or Polars natively. Is there a way to write directly from Polars to MSSQL without that intermediate pandas conversion?
-2
u/Alternative_Act_6548 10h 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
69
u/Gigawrench 17h 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.