From 05bd28f77a6e2d2423d3d89a7fdf026ede621827 Mon Sep 17 00:00:00 2001 From: Mike Schmidt Date: Wed, 31 Jan 2024 16:24:37 -0700 Subject: [PATCH] feat!: Added error variant for column in MD not in DataFrame --- CHANGELOG.md | 1 + lace/src/data/init.rs | 6 +++++- lace/src/interface/engine/error.rs | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3005e63c..3813bf54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `DataParseError::CodebookAndDataRowsMismatch` variant for when the number of rows in the codebook and the number of rows in the data do not match. +- `DataParseError::DataFrameMissingColumn` variant for when a column is in the codebook but not in the initial dataframe. ### Fixed - Initializing an engine with a codebook that has a different number of rows than the data will result in an error instead of printing a bunch on nonsense. diff --git a/lace/src/data/init.rs b/lace/src/data/init.rs index 515ca427..c8371d1d 100644 --- a/lace/src/data/init.rs +++ b/lace/src/data/init.rs @@ -224,7 +224,11 @@ pub fn df_to_col_models( .iter() .enumerate() .map(|(id, colmd)| { - let srs = srss[colmd.name.as_str()]; + let srs = srss.get(colmd.name.as_str()).ok_or_else(|| { + DataParseError::DataFrameMissingColumn { + column: colmd.name.clone(), + } + })?; let col_model = match &colmd.coltype { ColType::Continuous { hyper, prior } => continuous_col_model( id, diff --git a/lace/src/interface/engine/error.rs b/lace/src/interface/engine/error.rs index 6f291641..36bca7ba 100644 --- a/lace/src/interface/engine/error.rs +++ b/lace/src/interface/engine/error.rs @@ -42,6 +42,10 @@ pub enum DataParseError { n_codebook_rows: usize, n_data_rows: usize, }, + #[error( + "The dataframe does not contain the column `{column}` listed in the codebook" + )] + DataFrameMissingColumn { column: String }, } /// Errors that can arise when creating a new engine