Anatomy of a Polars Question: A Syntax Comparability of Polars vs SQL | by Ben Feifke | Mar, 2024


Transitioning from Pandas to Polars the simple manner — by taking a pit cease at SQL.

The key’s out! Polars is the most well liked factor on the block, and everyone desires a slice 😎

I lately wrote a put up, “The three Causes I Completely Switched From Pandas to Polars”, as a result of, nicely, this is among the most typical use-cases for selecting up Polars — as a drop-in substitute for Pandas. Nonetheless, although that is the commonest use-case, transitioning from Pandas to Polars generally is a bit unusual given the heavy variations in syntax between the 2.

In my earlier weblog put up, I mentioned how Pandas forces its customers to carry out knowledge queries in an object-oriented programming strategy, whereas Polars permits its customers to carry out knowledge queries in a data-oriented programming strategy, very similar to SQL. As such, although Polars most frequently serves as a drop-in substitute for Pandas, should you’re making an attempt to be taught Polars, evaluating it to SQL is probably going a a lot simpler start line than evaluating it to Pandas. The target of this put up is to just do that: to match Polars syntax to SQL syntax as a primer for getting up and operating with Polars.

On this put up, I present a syntax comparability of Polars vs SQL, by first establishing a toy dataset, after which demonstrating a Polars-to-SQL syntax comparability of three more and more advanced queries on that dataset.

Notice that this weblog put up makes use of Google BigQuery as its SQL dialect.

The toy dataset used all through this put up is a desk of orders and a desk of consumers for some restaurant:


| order_date_utc | order_value_usd | customer_id |
| 2024-01-02 | 50 | 001 |
| 2024-01-05 | 30 | 002 |
| 2024-01-20 | 44 | 001 |
| 2024-01-22 | 33 | 003 |
| 2024-01-29 | 25 | 002 |


| customer_id | is_premium_customer | title…


Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *