Skip to content

Commit

Permalink
feat(python): Added Codebook.like to create a new codebook from an ex…
Browse files Browse the repository at this point in the history
…isting one
  • Loading branch information
schmidmt committed Apr 25, 2024
1 parent 1b499de commit df005c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pylace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PyDataFrame> {
let remove: Vec<String> = rows
.into_iter()
Expand Down
13 changes: 13 additions & 0 deletions pylace/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,19 @@ impl Codebook {
Ok(())
}
}

fn like(&self, new_data: PyDataFrame) -> PyResult<Self> {
let index: Vec<String> = ["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]
Expand Down

0 comments on commit df005c7

Please sign in to comment.