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

72 Upvotes

36 comments sorted by

View all comments

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 41m 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?