| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| import ibis | |
| import ibis.expr.types as ir | |
| def raw_table(raw_data_path: str) -> ir.Table: | |
| """Load CSV from `raw_data_path` into a Table expression | |
| and format column names to snakecase | |
| """ | |
| return ibis.read_csv(sources=raw_data_path, table_name="absenteism").rename("snake_case") | |
| def feature_table(raw_table: ir.Table) -> ir.Table: | |
| """Add to `raw_table` the feature columns `has_children` | |
| `has_pet`, and `is_summer_brazil` | |
| """ | |
| return raw_table.mutate( | |
| has_children=(ibis.ifelse(ibis._.son > 0, True, False)), | |
| has_pet=ibis.ifelse(ibis._.pet > 0, True, False), | |
| is_summer_brazil=ibis._.month_of_absence.isin([1, 2, 12]), | |
| ) | |
| def feature_set( | |
| feature_table: ir.Table, | |
| feature_selection: list[str], | |
| condition: ibis.common.deferred.Deferred | None = None, | |
| ) -> ir.Table: | |
| """Select feature columns and filter rows""" | |
| selection = feature_table[feature_selection] | |
| if condition is None: | |
| return selection | |
| return selection.filter(condition) |