Skip to content

Commit

Permalink
add polars_err!(col_not_found= shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie committed Jul 31, 2024
1 parent 2e77d31 commit d7ceee5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
10 changes: 5 additions & 5 deletions crates/polars-arrow/src/legacy/kernels/string.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::array::{Array, ArrayRef, UInt32Array, Utf8ViewArray};
use crate::buffer::Buffer;
use crate::datatypes::ArrowDataType;
use crate::legacy::trusted_len::TrustedLenPush;

pub fn utf8view_len_bytes(array: &Utf8ViewArray) -> ArrayRef {
let values = array.len_iter().collect::<Vec<_>>();
let values: Buffer<_> = values.into();
let values: Buffer<_> = array.len_iter().collect();
let array = UInt32Array::new(ArrowDataType::UInt32, values, array.validity().cloned());
Box::new(array)
}

pub fn string_len_chars(array: &Utf8ViewArray) -> ArrayRef {
let values = array.values_iter().map(|x| x.chars().count() as u32);
let values: Buffer<_> = Vec::from_trusted_len_iter(values).into();
let values: Buffer<_> = array
.values_iter()
.map(|x| x.chars().count() as u32)
.collect();
let array = UInt32Array::new(ArrowDataType::UInt32, values, array.validity().cloned());
Box::new(array)
}
8 changes: 3 additions & 5 deletions crates/polars-core/src/frame/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,9 @@ impl DataFrame {

// values will all be placed in single column, so we must find their supertype
let schema = self.schema();
let mut iter = on.iter().map(|v| {
schema
.get(v)
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", v))
});
let mut iter = on
.iter()
.map(|v| schema.get(v).ok_or_else(|| polars_err!(col_not_found = v)));
let mut st = iter.next().unwrap()?.clone();
for dt in iter {
st = try_get_supertype(&st, dt?)?;
Expand Down
12 changes: 6 additions & 6 deletions crates/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl DataFrame {
/// Get the index of the column.
fn check_name_to_idx(&self, name: &str) -> PolarsResult<usize> {
self.get_column_index(name)
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", name))
.ok_or_else(|| polars_err!(col_not_found = name))
}

fn check_already_present(&self, name: &str) -> PolarsResult<()> {
Expand Down Expand Up @@ -1361,7 +1361,7 @@ impl DataFrame {
/// Get column index of a [`Series`] by name.
pub fn try_get_column_index(&self, name: &str) -> PolarsResult<usize> {
self.get_column_index(name)
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", name))
.ok_or_else(|| polars_err!(col_not_found = name))
}

/// Select a single column by name.
Expand Down Expand Up @@ -1560,7 +1560,7 @@ impl DataFrame {
.map(|name| {
let idx = *name_to_idx
.get(name.as_str())
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", name))?;
.ok_or_else(|| polars_err!(col_not_found = name))?;
Ok(self
.select_at_idx(idx)
.unwrap()
Expand Down Expand Up @@ -1588,7 +1588,7 @@ impl DataFrame {
.map(|name| {
let idx = *name_to_idx
.get(name.as_str())
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", name))?;
.ok_or_else(|| polars_err!(col_not_found = name))?;
Ok(self.select_at_idx(idx).unwrap().clone())
})
.collect::<PolarsResult<Vec<_>>>()?
Expand Down Expand Up @@ -1696,7 +1696,7 @@ impl DataFrame {
/// ```
pub fn rename(&mut self, column: &str, name: &str) -> PolarsResult<&mut Self> {
self.select_mut(column)
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", column))
.ok_or_else(|| polars_err!(col_not_found = column))
.map(|s| s.rename(name))?;
let unique_names: AHashSet<&str, ahash::RandomState> =
AHashSet::from_iter(self.columns.iter().map(|s| s.name()));
Expand Down Expand Up @@ -2968,7 +2968,7 @@ impl DataFrame {
for col in cols {
let _ = schema
.get(&col)
.ok_or_else(|| polars_err!(ColumnNotFound: "{:?}", col))?;
.ok_or_else(|| polars_err!(col_not_found = col))?;
}
}
DataFrame::new(new_cols)
Expand Down
3 changes: 3 additions & 0 deletions crates/polars-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ on startup."#.trim_start())
(duplicate = $name:expr) => {
polars_err!(Duplicate: "column with name '{}' has more than one occurrences", $name)
};
(col_not_found = $name:expr) => {
polars_err!(ColumnNotFound: "{:?} not found", $name)
};
(oob = $idx:expr, $len:expr) => {
polars_err!(OutOfBounds: "index {} is out of bounds for sequence of length {}", $idx, $len)
};
Expand Down

0 comments on commit d7ceee5

Please sign in to comment.