Skip to content

Commit e7a1ea8

Browse files
committed
Merge branch 'features/dataframe' into dev
2 parents 0cf7bf1 + 3c740f8 commit e7a1ea8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/structure/dataframe.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,63 @@ impl DataFrame {
15911591
None => panic!("Can't drop header '{}'", col_header),
15921592
}
15931593
}
1594+
1595+
/// Filter DataFrame by specific column
1596+
pub fn filter_by<F>(&self, column: &str, predicate: F) -> Result<DataFrame, String>
1597+
where
1598+
F: Fn(Scalar) -> bool,
1599+
{
1600+
let series = match self.ics.iter().position(|x| x.as_str() == column) {
1601+
Some(i) => &self.data[i],
1602+
None => return Err(format!("Column '{}' not found", column)),
1603+
};
1604+
1605+
let mut indices = Vec::new();
1606+
for i in 0 .. series.len() {
1607+
let value = series.at(i);
1608+
if predicate(value) {
1609+
indices.push(i);
1610+
}
1611+
}
1612+
1613+
let mut new_df = DataFrame::new(vec![]);
1614+
for (col_idx, col_series) in self.data.iter().enumerate() {
1615+
let filtered_series = self.extract_series_by_indices(col_series, &indices);
1616+
new_df.push(&self.ics[col_idx], filtered_series);
1617+
}
1618+
1619+
Ok(new_df)
1620+
}
1621+
1622+
fn extract_series_by_indices(&self, series: &Series, indices: &[usize]) -> Series {
1623+
macro_rules! extract_by_indices {
1624+
($array:expr, $type:ty, $dtype:ident) => {{
1625+
let values: Vec<$type> = indices
1626+
.iter()
1627+
.map(|&i| $array[i].clone())
1628+
.collect();
1629+
Series::new(values)
1630+
}};
1631+
}
1632+
1633+
match &series.values {
1634+
DTypeArray::USIZE(v) => extract_by_indices!(v, usize, USIZE),
1635+
DTypeArray::U8(v) => extract_by_indices!(v, u8, U8),
1636+
DTypeArray::U16(v) => extract_by_indices!(v, u16, U16),
1637+
DTypeArray::U32(v) => extract_by_indices!(v, u32, U32),
1638+
DTypeArray::U64(v) => extract_by_indices!(v, u64, U64),
1639+
DTypeArray::ISIZE(v) => extract_by_indices!(v, isize, ISIZE),
1640+
DTypeArray::I8(v) => extract_by_indices!(v, i8, I8),
1641+
DTypeArray::I16(v) => extract_by_indices!(v, i16, I16),
1642+
DTypeArray::I32(v) => extract_by_indices!(v, i32, I32),
1643+
DTypeArray::I64(v) => extract_by_indices!(v, i64, I64),
1644+
DTypeArray::F32(v) => extract_by_indices!(v, f32, F32),
1645+
DTypeArray::F64(v) => extract_by_indices!(v, f64, F64),
1646+
DTypeArray::Bool(v) => extract_by_indices!(v, bool, Bool),
1647+
DTypeArray::Str(v) => extract_by_indices!(v, String, Str),
1648+
DTypeArray::Char(v) => extract_by_indices!(v, char, Char),
1649+
}
1650+
}
15941651
}
15951652

15961653
impl Index<&str> for DataFrame {

0 commit comments

Comments
 (0)