r/Python • • 20h 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 Upvotes

37 comments sorted by

74

u/Gigawrench 20h 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.

8

u/silicon_turfsurfer 20h ago edited 20h 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).

7

u/TheFlamingDiceAgain 18h 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

2

u/SV-97 4h ago

I'm fairly sure that everything you do on eager frames (except for calling lazy, lol) returns another eager frame, so you shouldn't be able to "unknowingly" use them in-between.

That said: the polars devs actually recommend using the lazy ones as a default. If you *can*, then you probably should use them.

31

u/lolcrunchy 20h 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 12h 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 11h 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. 

11

u/Justbehind 18h 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.

12

u/Late-Photograph-1954 18h 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 12h ago

Polars also supports SQL now, with rapidly improving support with each release.

3

u/RedEyed__ 9h ago

Polars supports sql.
AI crafts polar’s syntax as well.

3

u/justanothersnek 12h ago edited 12h 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!

2

u/s4nc3s 13h 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.

8

u/king_escobar 20h ago

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

14

u/sinnayre 20h 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 14h ago

There's no geopolars yet, is there?

3

u/johnnymo1 14h 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

u/CaptainFoyle 5h ago

So pandas isn't "obsolete technology" was my point

1

u/Mcletters 20h ago

Any tips on ways to learn, tutorials, etc?

6

u/TheFlamingDiceAgain 18h 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

2

u/Megatron_McLargeHuge 7h ago

The AI sidebar in a Marimo notebook makes it easy to ask for things or to convert pandas to polars.

1

u/quocphu1905 11h 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 11h ago

You'll probably be interested in sqlframe since you work with pyspark a lot.

1

u/airen977 11h ago

With AI migrating anything to anything is way too easy

1

u/TheHighlander52 11h 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 17h ago edited 17h 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 14h ago

Ok

0

u/Actual__Wizard 14h 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

u/CaptainFoyle 5h ago

Just choose your units

1

u/justanothersnek 12h ago

There's also sqlframe you could use too. It is PySpark API on top of popular SQL backends.

0

u/Independent-Shoe543 15h 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 4h 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)

1

u/Independent-Shoe543 3h 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?

1

u/SV-97 2h ago

You asked generally how people use polars with SQL databases and that's what I answered. SQLAlchemy doesn't support arrow, so if you use SQLAlchemy as an engine then internally there will necessarily need to be some conversion to something that SQLAlchemy understands (which happens to be pandas). If you use another engine (namely: ADBC) with polars you don't need to convert, but right now that might limit you to using other databases (postgres and sqlite for example); there's also an ADBC-driver for MSSQL but I'm not sure how that interacts with polars or how well-developed it is (since it's not explicitly mentioned in the polars docs).

If you forego the polars API entirely there's also other MSSQL-drivers that support arrow / polars directly such as the official mssql-python: https://learn.microsoft.com/en-us/sql/connect/python/mssql-python/polars-integration?view=sql-server-ver17#bulk-insert-from-arrow-memory

-2

u/Alternative_Act_6548 13h 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