Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Polars Cookbook

You're reading from   Polars Cookbook Over 60 practical recipes to transform, manipulate, and analyze your data using Python Polars 1.x

Arrow left icon
Product type Paperback
Published in Aug 2024
Publisher Packt
ISBN-13 9781805121152
Length 394 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Yuki Kakegawa Yuki Kakegawa
Author Profile Icon Yuki Kakegawa
Yuki Kakegawa
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Chapter 1: Getting Started with Python Polars FREE CHAPTER 2. Chapter 2: Reading and Writing Files 3. Chapter 3: An Introduction to Data Analysis in Python Polars 4. Chapter 4: Data Transformation Techniques 5. Chapter 5: Handling Missing Data 6. Chapter 6: Performing String Manipulations 7. Chapter 7: Working with Nested Data Structures 8. Chapter 8: Reshaping and Tidying Data 9. Chapter 9: Time Series Analysis 10. Chapter 10: Interoperability with Other Python Libraries 11. Chapter 11: Working with Common Cloud Data Sources 12. Chapter 12: Testing and Debugging in Polars 13. Index 14. Other Books You May Enjoy

Integrating with DuckDB

DuckDB is an in-process analytical database. The speed is like that of Polars, which is really fast. Like Polars, DuckDB has built-in integrations with other tools. One example is its integration with Arrow. Just like Polars, DuckDB allows for zero-copy to and from the Arrow format. DuckDB has APIs in many languages such as Python, C, Go, R, and Swift. You can check out DuckDB’s documentation to learn more about it at https://duckdb.org/.

DuckDB has a solid SQL API with a rich set of features. It’s superior to the SQL API in Polars as of the time of writing this. You can also run a SQL query on a Polars DataFrame directly without copying data. Even if you convert a DuckDB relation to a Polars DataFrame or vice versa, it allows you to enable zero-copy operations, thanks to Apache Arrow columnar memory format used both in Polars and DuckDB.

Getting ready

You need to have PyArrow installed as Arrow is the fundamental technology through which DuckDB and Polars can interoperate. If you haven’t, execute the following code in the command line:

pip install duckdb

How to do it...

Here’s how to use DuckDB with Polars. We’ll first see how to run an SQL query on a Polars DataFrame with DuckDB. And then we’ll see how to convert a DuckDB relation to a Polars DataFrame.

  1. Run a SQL query with DuckDB directly on a Polars DataFrame:
    import duckdb
    df = pl.DataFrame({
        'a': [1,2,3]
    })
    rel = duckdb.sql('SELECT * FROM df')
    rel.show()

    The preceding code will return the following output:

Figure 10.4 – The result of a DuckDB SQL query on a Polars DataFrame

Figure 10.4 – The result of a DuckDB SQL query on a Polars DataFrame

  1. Convert a DuckDB relation to a Polars DataFrame:
    rel.pl()

    The preceding code will return the following output:

Figure 10.5 – A Polars DataFrame converted from a DuckDB relation

Figure 10.5 – A Polars DataFrame converted from a DuckDB relation

How it works...

In step 1, we ran a SQL query on a Polars DataFrame through DuckDB. In step 2, we converted a DuckDB relation to a Polars DataFrame. In both methods, we used DuckDB’s functionalities. DuckDB is already compatible to work with Polars. This is all thanks to DuckDB having the zero-copy integration with Arrow, as well as Polars using Arrow for its memory model.

Although you can run DuckDB SQL on both Polars DataFrame and Polars LazyFrame, the resulting DuckDB relation doesn’t retain the laziness from the input LazyFrame.

Tip

You might wonder which tool you should use (Polars or DuckDB) if you’re solely using SQL. My answer is DuckDB, as the SQL API is more mature and includes the whole list of standard SQL functions, as well as more advanced ones such as those that work on nested data types. It’s just a more full-featured SQL API than that of Polars as of the time of writing.

See also

To learn more about Polars integration with DuckDB or DuckDB in general, please refer to these additional resources:

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime