One Function. No Rewrites. Explore Ibis for Filling Null Values.
Patrick Clarke · Sep 20, 2022
Have you ever wanted your query and transform code to work against your tabular data, no matter where it lived?
Well, it’s possible, and I’m going to demonstrate how you can actually achieve this with Ibis using a function that forward- or backward- fills null values in a dataframe or table.
That’s right: you can use the exact same directional fill code for your data in Postgres tables, BigQuery datasets, and even Pandas dataframes. One function – no rewrites.
Before I get to that, I’ll review what Ibis is for those who aren’t familiar with the project.
Ibis is an open source project that allows for engine-agnostic querying using python expressions. Or, put simply: it’s a python package that you can use to connect to and query data, and the API works the same way for multiple backends–like DuckDB or Postgres.
Backends are extensions to the framework. There are several first-party (Ibis Project) maintained backends like Pandas, DuckDB and Postgres, and some third party backends like Google’s ibis-BigQuery. There’s even a Substrait backend in development for more sophisticated users that are on the cutting-edge of interoperability in data tech stacks.
Ibis is a powerful tool. It significantly reduces tech debt when performing data migrations and allows for a unified, pythonic way for users to access and transform their data. I wrote about this in a walkthrough on engine-agnostic analytics, but the bottom line is:
- No more fstrings littering your console, IDE, or notebook
- No more extensive code refactors slowing you down from migrating to the latest and greatest hardware or software
- No more cumbersome vendor-locking portal-based APIs
It is an incredible framework for people invested in staying up-to-date with the fastest and smartest tech available: the less time your team spends migrating your code, the more time your team can spend playing with your new toys.
Let’s go through an example of this in action by implementing bfill and ffill and then executing the same function against multiple backends.
bfill and ffill with Ibis
Fill functions fill in missing data based on present data. This is typically used in time series data with holes in values between dates, where values aren’t subject to change over time (or, if they do, it’s okay to assume that they don’t).
For example, the forward fill function works like this:
- If this is the first value, then skip to (3), even if this value is null
- If this value is null, then replace it with the previous value (even if the previous value was null)
- Go to the next value
Basically, you’re taking all of the null values after a non-null value and replacing them with that non-null value. I explain this in more detail in the strategy section on the original blog post.
Taken from the Ibis Project How To guide I contributed, this can be written as follows:
I decided to put this in a function and execute it against the multiple backends using the same data I used in the blog post. Here is the function (with some small modifications):
I can use this function to pick the order column I care about, pick a strategy without thinking about the mechanics of the fill, and decide if I even want to group before filling (if
group_by is None, then treat the entire set as one event).
To test interoperability, I tested this on a few backends. I kept the DataFrame around to test against the Pandas backend, I saved it to parquet to test against the DuckDB backend, uploaded it to a .db file to test against SQLite, uploaded it to a table in Postgres to test against the Postgres backend, and uploaded it to a BigQuery Dataset to test against BigQuery.
And it worked against all of them! You can find the code rendered in a notebook on my GitHub page.
To keep up with the latest Ibis news, follow @IbisData on Twitter, subscribe to Phillip Cloud’s live streams and videos on YouTube, star the repository on GitHub, and browse the Ibis Project website.
The team at Voltron Data is dedicated to continuously improving Ibis and contributed to the recent release of version 3.2. Ibis is free and open source — install it today through pip:
pip install ibis-framework or conda:
conda install -c conda-forge ibis-framework.
Learn how a Voltron Data Enterprise Support subscription can help accelerate your success with Ibis.
Photo by Frankie Lopez