Skip to content

Commit

Permalink
stringify, clone, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Apr 19, 2023
1 parent 3e1b3ec commit dc191bf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "myval"
version = "0.1.15"
version = "0.1.16"
edition = "2021"
authors = ["Serhij S. <div@altertech.com>"]
license = "Apache-2.0"
Expand Down
61 changes: 60 additions & 1 deletion src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use arrow2::io::ipc::read::{StreamReader, StreamState};
use arrow2::io::ipc::write::{StreamWriter, WriteOptions};
use arrow2::types::NativeType;
use chrono::{DateTime, Local, NaiveDateTime, SecondsFormat, Utc};
use std::fmt;
use std::ops::{Add, Div, Mul, Sub};
use std::str::FromStr;

Expand Down Expand Up @@ -379,6 +380,30 @@ impl DataFrame {
df.metadata = metadata;
Ok(df)
}
/// Clone series by name
pub fn clone_series(&mut self, name: &str) -> Result<(Series, DataType), Error> {
if let Some((pos, _)) = self
.fields
.iter()
.enumerate()
.find(|(_, field)| field.name == name)
{
Ok((self.data[pos].clone(), self.fields[pos].data_type.clone()))
} else {
Err(Error::NotFound(name.to_owned()))
}
}
/// Clone series by index
pub fn clone_series_at(&mut self, index: usize) -> Result<(Series, DataType), Error> {
if index < self.fields.len() {
Ok((
self.data[index].clone(),
self.fields[index].data_type.clone(),
))
} else {
Err(Error::OutOfBounds)
}
}
/// Pop series by name
pub fn pop_series(&mut self, name: &str) -> Result<(Series, DataType), Error> {
if let Some((pos, _)) = self
Expand Down Expand Up @@ -446,6 +471,37 @@ impl DataFrame {
Err(Error::OutOfBounds)
}
}
/// Convert to string
pub fn stringify<T>(&mut self, name: &str) -> Result<(), Error>
where
T: NativeType + fmt::Display,
{
if let Some(pos) = self.get_column_index(name) {
self.stringify_at::<T>(pos)
} else {
Err(Error::NotFound(name.to_owned()))
}
}
pub fn stringify_at<T>(&mut self, index: usize) -> Result<(), Error>
where
T: NativeType + fmt::Display,
{
if let Some(series) = self.data.get(index) {
let values: &PrimitiveArray<T> =
series.as_any().downcast_ref().ok_or(Error::TypeMismatch)?;
#[allow(clippy::redundant_closure_for_method_calls)]
let dt: Vec<Option<_>> = values
.into_iter()
.map(|v| v.map(|n| n.to_string()))
.collect();
let arr = Utf8Array::<i64>::from(dt);
self.data[index] = arr.boxed();
self.fields[index].data_type = DataType::LargeUtf8;
Ok(())
} else {
Err(Error::OutOfBounds)
}
}
/// apply a custom function
pub fn apply<F, I, O>(&mut self, name: &str, func: F) -> Result<(), Error>
where
Expand All @@ -469,7 +525,10 @@ impl DataFrame {
let values: &PrimitiveArray<I> =
series.as_any().downcast_ref().ok_or(Error::TypeMismatch)?;
let dt: Vec<Option<_>> = values.into_iter().map(|v| func(v.copied())).collect();
self.data[index] = PrimitiveArray::<O>::from(dt).boxed();
let arr = PrimitiveArray::<O>::from(dt).boxed();
let dtype = arr.data_type().clone();
self.data[index] = arr;
self.fields[index].data_type = dtype;
Ok(())
} else {
Err(Error::OutOfBounds)
Expand Down

0 comments on commit dc191bf

Please sign in to comment.