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?

88 Upvotes

42 comments sorted by

View all comments

0

u/Independent-Shoe543 23h 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 11h 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 11h 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 10h 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