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

107 Upvotes

45 comments sorted by

View all comments

Show parent comment

8

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

8

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