r/Python • • Mar 08 '26

Discussion Polars vs pandas

I am trying to come from database development into python ecosystem.

Wondering if going into polars framework, instead of pandas will be any beneficial?

125 Upvotes

88 comments sorted by

View all comments

Show parent comment

1

u/Black_Magic100 Mar 09 '26

I'll have to look more into this today when I get a chance. I'm guessing it defaults to eager OOTB?

3

u/commandlineluser Mar 09 '26

When you use the DataFrame API:

(df.with_columns()
   .group_by()
   .agg())

Polars basically executes:

(df.lazy()
   .with_columns().collect(optimizations=pl.QueryOpts.none())
   .lazy()
   .group_by().agg().collect(optimizations=pl.QueryOpts.none())
 )

One idea being you should be able to easily convert your "eager" code by manually calling lazy / collect to run the "entire pipeline" as a single "query" instead:

df.lazy().with_columns().group_by().agg().collect()

(Or in the case of read_* use the lazy scan_* equivalent which will return a LazyFrame directly))

With manually calling collect(), all optimizations are also enabled by default.

This is one reason why writing "pandas style" (e.g. df["foo"]) is discouraged in Polars, as it works on the in-memory Series objects and cannot be lazy.

The User Guide explains things in detail:

2

u/SV-97 Mar 09 '26

It's not really "defaulting" to it I'd say; it's just two parallel APIs. For example read_csv gives you an eager dataframe, while scan_csv gives you a lazy one.