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

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

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

[ad_1]

Transitioning from Pandas to Polars the straightforward approach — by taking a pit cease at SQL.

The key’s out! Polars is the most well liked factor on the block, and all people 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 without doubt one of the most typical use-cases for choosing up Polars — as a drop-in substitute for Pandas. Nonetheless, regardless that that is the commonest use-case, transitioning from Pandas to Polars could be 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 allows its customers to carry out knowledge queries in a data-oriented programming strategy, very similar to SQL. As such, regardless that 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 do exactly that: to check 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.

Be aware 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 shoppers for some restaurant:

orders

| 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 |

clients

| customer_id | is_premium_customer | identify…

[ad_2]