r/Python • • 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?

98 Upvotes

43 comments sorted by

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.

8

u/silicon_turfsurfer 1d ago edited 1d 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).

2

u/SV-97 1d 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.

1

u/silicon_turfsurfer 18h ago

I will definitely check it out. Is it easy to adapt existing polars dataframe code to lazeframe, then just collect() when you need the data?

Today I was working with 3 million rows and the polars dataframe is still working 'instantly' for my calculations.

1

u/SV-97 14h ago

It somewhat depends: mostly yes, but there are some functions that lazy frames can't support directly (in the worst case that means you have to insert another collect at that point). But polars should tell you about any such problematic parts when you make the change.

Today I was working with 3 million rows and the polars dataframe is still working 'instantly' for my calculations.

I mean it always depends on how large yours rows are etc. --- but I think 3 Million is still "basically nothing" relative to what polars is built to be able to handle :) The number of rows in my last project was more on the order of billions and that was probably still way less than what other people have to deal with (IIRC it was ultimately "just" around 100GB of data with filesystem level compression)

7

u/TheFlamingDiceAgain 1d 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

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.

6

u/JambaJuiceIsAverage 1d 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.

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/Wheaties4brkfst 15h ago

The perfect data language is R. Unfortunately R isn’t good at all the other stuff.

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.

10

u/nightcracker 1d ago

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

3

u/RedEyed__ 1d ago

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

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.

16

u/sinnayre 1d 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 1d ago

There's no geopolars yet, is there?

4

u/johnnymo1 1d 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 1d ago

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

1

u/Mcletters 1d ago

Any tips on ways to learn, tutorials, etc?

6

u/TheFlamingDiceAgain 1d 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 1d ago

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

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)

2

u/airen977 1d ago

With AI migrating anything to anything is way too easy

1

u/justanothersnek 1d ago

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

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?

1

u/SV-97 1d 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 1d 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 22h 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/Actual__Wizard 1d ago edited 1d 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 1d ago

Ok

-1

u/Actual__Wizard 1d 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.

0

u/CaptainFoyle 1d ago

Just choose your units

0

u/Actual__Wizard 16h ago

Robot...

1

u/CaptainFoyle 15h ago

That's a really weird comment

1

u/justanothersnek 1d ago

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

-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