From df005c731730a4d78cbc5c1c29caff8aab4ef225 Mon Sep 17 00:00:00 2001 From: Mike Schmidt Date: Thu, 25 Apr 2024 16:40:10 -0600 Subject: [PATCH] feat(python): Added Codebook.like to create a new codebook from an existing one --- pylace/src/lib.rs | 12 +++++++++++- pylace/src/metadata.rs | 13 +++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pylace/src/lib.rs b/pylace/src/lib.rs index c53e43dd..30930436 100644 --- a/pylace/src/lib.rs +++ b/pylace/src/lib.rs @@ -1179,7 +1179,17 @@ impl CoreEngine { Ok(()) } - /// Remove Rows at the given indices + /// Remove Rows at the given indices. + /// + /// + /// Example + /// ------- + /// + /// >>> import lace + /// >>> engine = lace.Engine('animals.rp') + /// >>> removed = engine.remove_rows(["wolf", "ox"]) + /// >>> removed["index"] + /// fn remove_rows(&mut self, rows: &PyList) -> PyResult { let remove: Vec = rows .into_iter() diff --git a/pylace/src/metadata.rs b/pylace/src/metadata.rs index 1706e77a..e137608d 100644 --- a/pylace/src/metadata.rs +++ b/pylace/src/metadata.rs @@ -668,6 +668,19 @@ impl Codebook { Ok(()) } } + + fn like(&self, new_data: PyDataFrame) -> PyResult { + let index: Vec = ["id", "ID", "index", "INDEX"].into_iter().map(|name| + new_data.0.column(name) + ).find(|x| x.is_ok()).ok_or_else(|| PyValueError::new_err("No index column: convention for index is ID or INDEX, in upper or lower case."))? + .expect("The value has already been checked") + .str().map_err(to_pyerr)?.into_iter().flatten().map(|x| x.to_string()).collect(); + + Ok(Self(lace::codebook::Codebook { + row_names: RowNameList::try_from(index).map_err(to_pyerr)?, + ..self.0.clone() + })) + } } #[pyfunction]