Maximizing productivity with selectors

blog
new feature
productivity
duckdb
Author

Phillip Cloud

Published

February 27, 2023

Before Ibis 5.0 it’s been challenging to concisely express whole-table operations with ibis. Happily this is no longer the case in ibis 5.0.

Let’s jump right in!

We’ll look at selectors examples using the palmerpenguins data set with the DuckDB backend.

Setup

from ibis.interactive import *

t = ex.penguins.fetch()
t
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64int64int64stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Torgersen39.118.71813750male  2007 │
│ Adelie Torgersen39.517.41863800female2007 │
│ Adelie Torgersen40.318.01953250female2007 │
│ Adelie TorgersennannanNULLNULLNULL2007 │
│ Adelie Torgersen36.719.31933450female2007 │
│ Adelie Torgersen39.320.61903650male  2007 │
│ Adelie Torgersen38.917.81813625female2007 │
│ Adelie Torgersen39.219.61954675male  2007 │
│ Adelie Torgersen34.118.11933475NULL2007 │
│ Adelie Torgersen42.020.21904250NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

Examples

Normalization

Let’s say you want to compute the z-score of every numeric column and replace the existing data with that normalized value. Here’s how you’d do that with selectors:

t.mutate(s.across(s.numeric(), (_ - _.mean()) / _.std()))
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year      ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━┩
│ stringstringfloat64float64float64float64stringfloat64   │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────────┤
│ Adelie Torgersen-0.8832050.784300-1.416272-0.563317male  -1.257484 │
│ Adelie Torgersen-0.8099390.126003-1.060696-0.500969female-1.257484 │
│ Adelie Torgersen-0.6634080.429833-0.420660-1.186793female-1.257484 │
│ Adelie TorgersennannannannanNULL-1.257484 │
│ Adelie Torgersen-1.3227991.088129-0.562890-0.937403female-1.257484 │
│ Adelie Torgersen-0.8465721.746426-0.776236-0.688012male  -1.257484 │
│ Adelie Torgersen-0.9198370.328556-1.416272-0.719186female-1.257484 │
│ Adelie Torgersen-0.8648881.240044-0.4206600.590115male  -1.257484 │
│ Adelie Torgersen-1.7990250.480471-0.562890-0.906229NULL-1.257484 │
│ Adelie Torgersen-0.3520291.543873-0.7762360.060160NULL-1.257484 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────────┘

What’s Up With the year Column?

Whoops, looks like we included year in our normalization because it’s an int64 column (and therefore numeric) but normalizing the year doesn’t make sense.

We can exclude year from the normalization using another selector:

t.mutate(s.across(s.numeric() & ~s.c("year"), (_ - _.mean()) / _.std()))
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64float64float64stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Torgersen-0.8832050.784300-1.416272-0.563317male  2007 │
│ Adelie Torgersen-0.8099390.126003-1.060696-0.500969female2007 │
│ Adelie Torgersen-0.6634080.429833-0.420660-1.186793female2007 │
│ Adelie TorgersennannannannanNULL2007 │
│ Adelie Torgersen-1.3227991.088129-0.562890-0.937403female2007 │
│ Adelie Torgersen-0.8465721.746426-0.776236-0.688012male  2007 │
│ Adelie Torgersen-0.9198370.328556-1.416272-0.719186female2007 │
│ Adelie Torgersen-0.8648881.240044-0.4206600.590115male  2007 │
│ Adelie Torgersen-1.7990250.480471-0.562890-0.906229NULL2007 │
│ Adelie Torgersen-0.3520291.543873-0.7762360.060160NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

c is short for “column” and the ~ means “negate”. Combining those we get “not the year column”!

Pretty neat right?

Composable Group By

The power of this approach comes in when you want the grouped version. Perhaps we think some of these columns vary by species.

With selectors, all you need to do is slap a .group_by("species") onto t:

t.group_by("species").mutate(
    s.across(s.numeric() & ~s.c("year"), (_ - _.mean()) / _.std())
)
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64float64float64stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Torgersen0.791697-1.2709970.160007-0.001444female2008 │
│ Adelie Biscoe   1.467524-0.0381030.9245960.816322male  2009 │
│ Adelie Dream    0.3786920.619441-0.9104182.070231male  2007 │
│ Adelie Dream    -0.860324-0.284681-1.216254-1.200835female2007 │
│ Adelie Torgersen-0.7852320.7838270.465843-0.546622female2007 │
│ Adelie Torgersen0.1909621.8523350.007089-0.110480male  2007 │
│ Adelie Torgersen0.040778-0.449067-1.369172-0.164997female2007 │
│ Adelie Torgersen0.1534161.0304050.7716782.124749male  2007 │
│ Adelie Torgersen-1.761426-0.2024890.465843-0.492104NULL2007 │
│ Adelie Torgersen1.2047021.5235630.0070891.197947NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

Since ibis translates this into a run-of-the-mill selection as if you had called select or mutate without selectors, nothing special is needed for a backend to work with these new constructs.

Let’s look at some more examples.

Min-max Normalization

Grouped min/max normalization? Easy:

t.group_by("species").mutate(
    s.across(s.numeric() & ~s.c("year"), (_ - _.min()) / (_.max() - _.min()))
)
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64float64float64stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Torgersen0.6330940.2166670.5000000.441558female2008 │
│ Adelie Biscoe   0.7625900.4666670.6315790.636364male  2009 │
│ Adelie Dream    0.5539570.6000000.3157890.935065male  2007 │
│ Adelie Dream    0.3165470.4166670.2631580.155844female2007 │
│ Adelie Torgersen0.3309350.6333330.5526320.311688female2007 │
│ Adelie Torgersen0.5179860.8500000.4736840.415584male  2007 │
│ Adelie Torgersen0.4892090.3833330.2368420.402597female2007 │
│ Adelie Torgersen0.5107910.6833330.6052630.948052male  2007 │
│ Adelie Torgersen0.1438850.4333330.5526320.324675NULL2007 │
│ Adelie Torgersen0.7122300.7833330.4736840.727273NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

Casting and Munging

How about casting every column whose name ends with any of the strings "mm" or "g" to a float32? No problem!

t.mutate(s.across(s.endswith(("mm", "g")), _.cast("float32")))
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat32float32float32float32stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ Adelie Torgersen39.09999818.700001181.03750.0male  2007 │
│ Adelie Torgersen39.50000017.400000186.03800.0female2007 │
│ Adelie Torgersen40.29999918.000000195.03250.0female2007 │
│ Adelie TorgersennannannannanNULL2007 │
│ Adelie Torgersen36.70000119.299999193.03450.0female2007 │
│ Adelie Torgersen39.29999920.600000190.03650.0male  2007 │
│ Adelie Torgersen38.90000217.799999181.03625.0female2007 │
│ Adelie Torgersen39.20000119.600000195.04675.0male  2007 │
│ Adelie Torgersen34.09999818.100000193.03475.0NULL2007 │
│ Adelie Torgersen42.00000020.200001190.04250.0NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

We can make all string columns have the same case too!

t.mutate(s.across(s.of_type("string"), _.lower()))
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     year  ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ stringstringfloat64float64int64int64stringint64 │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ adelie torgersen39.118.71813750male  2007 │
│ adelie torgersen39.517.41863800female2007 │
│ adelie torgersen40.318.01953250female2007 │
│ adelie torgersennannanNULLNULLNULL2007 │
│ adelie torgersen36.719.31933450female2007 │
│ adelie torgersen39.320.61903650male  2007 │
│ adelie torgersen38.917.81813625female2007 │
│ adelie torgersen39.219.61954675male  2007 │
│ adelie torgersen34.118.11933475NULL2007 │
│ adelie torgersen42.020.21904250NULL2007 │
│  │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘

Multiple Computations per Column

What if I want to compute multiple things? Heck yeah!

t.group_by("sex").mutate(
    s.across(
        s.numeric() & ~s.c("year"),
        dict(centered=_ - _.mean(), zscore=(_ - _.mean()) / _.std()),
    )
).select("sex", s.endswith(("_centered", "_zscore")))
┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃ sex     bill_length_mm_centered  bill_depth_mm_centered  flipper_length_mm_centered  body_mass_g_centered  bill_length_mm_zscore  bill_depth_mm_zscore  flipper_length_mm_zscore  body_mass_g_zscore ┃
┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ stringfloat64float64float64float64float64float64float64float64            │
├────────┼─────────────────────────┼────────────────────────┼────────────────────────────┼──────────────────────┼───────────────────────┼──────────────────────┼──────────────────────────┼────────────────────┤
│ female4.10303-1.92545511.636364937.7272730.836760-1.0722700.9308511.407635 │
│ female1.20303-2.42545510.636364712.7272730.245342-1.3507160.8508561.069885 │
│ female-8.096970.674545-12.363636-462.272727-1.6512710.375649-0.989030-0.693924 │
│ female-5.896970.874545-10.363636-562.272727-1.2026100.487027-0.829039-0.844035 │
│ female-0.996971.174545-15.363636-662.272727-0.2033190.654095-1.229015-0.994147 │
│ female-5.496971.374545-12.363636-162.272727-1.1210350.765473-0.989030-0.243590 │
│ female-3.396972.574545-2.363636-412.272727-0.6927681.433743-0.189079-0.618868 │
│ female-7.696971.974545-13.363636-537.272727-1.5696971.099608-1.069025-0.806507 │
│ female-4.296971.874545-23.363636-462.272727-0.8763111.043919-1.868975-0.693924 │
│ female-6.196972.774545-8.363636-62.272727-1.2637911.545122-0.669049-0.093478 │
│  │
└────────┴─────────────────────────┴────────────────────────┴────────────────────────────┴──────────────────────┴───────────────────────┴──────────────────────┴──────────────────────────┴────────────────────┘

Don’t like the naming convention?

Pass a function to make your own name!

t.select(s.startswith("bill")).mutate(
    s.across(
        s.all(),
        dict(x=_ - _.mean(), y=_.max()),
        names=lambda col, fn: f"{col}_{fn}_improved",
    )
)
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ bill_length_mm  bill_depth_mm  bill_length_mm_x_improved  bill_depth_mm_x_improved  bill_length_mm_y_improved  bill_depth_mm_y_improved ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ float64float64float64float64float64float64                  │
├────────────────┼───────────────┼───────────────────────────┼──────────────────────────┼───────────────────────────┼──────────────────────────┤
│           39.118.7-4.821931.5488359.621.5 │
│           39.517.4-4.421930.2488359.621.5 │
│           40.318.0-3.621930.8488359.621.5 │
│            nannannannan59.621.5 │
│           36.719.3-7.221932.1488359.621.5 │
│           39.320.6-4.621933.4488359.621.5 │
│           38.917.8-5.021930.6488359.621.5 │
│           39.219.6-4.721932.4488359.621.5 │
│           34.118.1-9.821930.9488359.621.5 │
│           42.020.2-1.921933.0488359.621.5 │
│               │
└────────────────┴───────────────┴───────────────────────────┴──────────────────────────┴───────────────────────────┴──────────────────────────┘

Don’t like lambda functions? We support a format string too!

t.select(s.startswith("bill")).mutate(
    s.across(
        s.all(),
        func=dict(x=_ - _.mean(), y=_.max()),
        names="{col}_{fn}_improved",
    )
).head(2)
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ bill_length_mm  bill_depth_mm  bill_length_mm_x_improved  bill_depth_mm_x_improved  bill_length_mm_y_improved  bill_depth_mm_y_improved ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ float64float64float64float64float64float64                  │
├────────────────┼───────────────┼───────────────────────────┼──────────────────────────┼───────────────────────────┼──────────────────────────┤
│           39.118.7-4.821931.5488359.621.5 │
│           39.517.4-4.421930.2488359.621.5 │
└────────────────┴───────────────┴───────────────────────────┴──────────────────────────┴───────────────────────────┴──────────────────────────┘

Working with other Ibis APIs

We’ve seen lots of mutate use, but selectors also work with .agg:

t.group_by("year").agg(s.across(s.numeric() & ~s.c("year"), _.mean())).order_by("year")
┏━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ year   bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g ┃
┡━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩
│ int64float64float64float64float64     │
├───────┼────────────────┼───────────────┼───────────────────┼─────────────┤
│  200743.74036717.427523196.8807344124.541284 │
│  200843.54122816.914035202.7982464266.666667 │
│  200944.45294117.125210202.8067234210.294118 │
└───────┴────────────────┴───────────────┴───────────────────┴─────────────┘

Naturally, selectors work in grouping keys too, for even more convenience:

t.group_by(~s.numeric() | s.c("year")).mutate(
    s.across(s.numeric() & ~s.c("year"), dict(centered=_ - _.mean(), std=_.std()))
).select("species", s.endswith(("_centered", "_std")))
┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ species  bill_length_mm_centered  bill_depth_mm_centered  flipper_length_mm_centered  body_mass_g_centered  bill_length_mm_std  bill_depth_mm_std  flipper_length_mm_std  body_mass_g_std ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ stringfloat64float64float64float64float64float64float64float64         │
├─────────┼─────────────────────────┼────────────────────────┼────────────────────────────┼──────────────────────┼────────────────────┼───────────────────┼───────────────────────┼─────────────────┤
│ Adelie -1.460.400000-1.600000-170.0000001.3277800.6819092.302173189.076704 │
│ Adelie -0.96-0.2000003.400000180.0000001.3277800.6819092.302173189.076704 │
│ Adelie -0.36-1.100000-1.60000030.0000001.3277800.6819092.302173189.076704 │
│ Adelie 1.440.3000001.400000-220.0000001.3277800.6819092.302173189.076704 │
│ Adelie 1.340.600000-1.600000180.0000001.3277800.6819092.302173189.076704 │
│ Gentoo 1.000.93529411.117647147.0588243.0567550.6707664.973459349.763576 │
│ Gentoo 1.00-0.164706-0.882353147.0588243.0567550.6707664.973459349.763576 │
│ Gentoo -1.40-0.864706-3.882353-152.9411763.0567550.6707664.973459349.763576 │
│ Gentoo -2.30-0.0647060.117647-352.9411763.0567550.6707664.973459349.763576 │
│ Gentoo -2.200.035294-3.882353-402.9411763.0567550.6707664.973459349.763576 │
│  │
└─────────┴─────────────────────────┴────────────────────────┴────────────────────────────┴──────────────────────┴────────────────────┴───────────────────┴───────────────────────┴─────────────────┘

Filtering Selectors

You can also express complex filters more concisely.

Let’s say we only want to keep rows where all the bill size z-score related columns’ absolute values are greater than 2.

t.drop("year").group_by("species").mutate(
    s.across(s.numeric(), dict(zscore=(_ - _.mean()) / _.std()))
).filter(s.if_all(s.startswith("bill") & s.endswith("_zscore"), _.abs() > 2))
┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃ species  island     bill_length_mm  bill_depth_mm  flipper_length_mm  body_mass_g  sex     bill_length_mm_zscore  bill_depth_mm_zscore  flipper_length_mm_zscore  body_mass_g_zscore ┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ stringstringfloat64float64int64int64stringfloat64float64float64float64            │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────────────────────┼──────────────────────┼──────────────────────────┼────────────────────┤
│ Adelie Torgersen46.021.51944200male  2.7065392.5920710.6187601.088911 │
│ Adelie Dream    32.115.51883050female-2.512345-2.339505-0.298747-1.418906 │
│ Gentoo Biscoe   55.917.02285600male  2.7240462.0565081.6673941.039411 │
│ Gentoo Biscoe   59.617.02306050male  3.9246212.0565081.9757991.932062 │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────────────────────┴──────────────────────┴──────────────────────────┴────────────────────┘

Bonus: Generated SQL

The SQL for that last expression is pretty gnarly:

ibis.to_sql(
    t.drop("year")
    .group_by("species")
    .mutate(s.across(s.numeric(), dict(zscore=(_ - _.mean()) / _.std())))
    .filter(s.if_all(s.startswith("bill") & s.endswith("_zscore"), _.abs() > 2))
)
WITH t0 AS (
  SELECT
    t2.species AS species,
    t2.island AS island,
    t2.bill_length_mm AS bill_length_mm,
    t2.bill_depth_mm AS bill_depth_mm,
    t2.flipper_length_mm AS flipper_length_mm,
    t2.body_mass_g AS body_mass_g,
    t2.sex AS sex
  FROM main._ibis_examples_penguins_mqqdnfaydfbevoowdsf7djbsom AS t2
), t1 AS (
  SELECT
    t0.species AS species,
    t0.island AS island,
    t0.bill_length_mm AS bill_length_mm,
    t0.bill_depth_mm AS bill_depth_mm,
    t0.flipper_length_mm AS flipper_length_mm,
    t0.body_mass_g AS body_mass_g,
    t0.sex AS sex,
    (
      t0.bill_length_mm - AVG(t0.bill_length_mm) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    ) / STDDEV_SAMP(t0.bill_length_mm) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS bill_length_mm_zscore,
    (
      t0.bill_depth_mm - AVG(t0.bill_depth_mm) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    ) / STDDEV_SAMP(t0.bill_depth_mm) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS bill_depth_mm_zscore,
    (
      t0.flipper_length_mm - AVG(t0.flipper_length_mm) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    ) / STDDEV_SAMP(t0.flipper_length_mm) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS flipper_length_mm_zscore,
    (
      t0.body_mass_g - AVG(t0.body_mass_g) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
    ) / STDDEV_SAMP(t0.body_mass_g) OVER (PARTITION BY t0.species ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS body_mass_g_zscore
  FROM t0
)
SELECT
  t1.species,
  t1.island,
  t1.bill_length_mm,
  t1.bill_depth_mm,
  t1.flipper_length_mm,
  t1.body_mass_g,
  t1.sex,
  t1.bill_length_mm_zscore,
  t1.bill_depth_mm_zscore,
  t1.flipper_length_mm_zscore,
  t1.body_mass_g_zscore
FROM t1
WHERE
  ABS(t1.bill_length_mm_zscore) > CAST(2 AS TINYINT)
  AND ABS(t1.bill_depth_mm_zscore) > CAST(2 AS TINYINT)

Good thing you didn’t have to write that by hand!

Summary

This blog post illustrates the ability to apply computations to many columns at once and the power of ibis as a composable, expressive library for analytics.

Back to top