ext/polars/src/series.rs in polars-df-0.1.3 vs ext/polars/src/series.rs in polars-df-0.1.4
- old
+ new
@@ -3,10 +3,11 @@
use polars::prelude::*;
use polars::series::IsSorted;
use std::cell::RefCell;
use crate::conversion::*;
+use crate::list_construction::rb_seq_to_list;
use crate::set::set_at_idx;
use crate::{RbDataFrame, RbPolarsErr, RbResult, RbValueError};
#[magnus::wrap(class = "Polars::RbSeries")]
pub struct RbSeries {
@@ -121,10 +122,23 @@
let mut s = val.0.into_series();
s.rename(&name);
RbSeries::new(s)
}
+ pub fn new_object(name: String, val: RArray, _strict: bool) -> RbResult<Self> {
+ let val = val
+ .each()
+ .map(|v| v.map(ObjectValue::from))
+ .collect::<RbResult<Vec<ObjectValue>>>()?;
+ let s = ObjectChunked::<ObjectValue>::new_from_vec(&name, val).into_series();
+ Ok(s.into())
+ }
+
+ pub fn new_list(name: String, seq: Value, dtype: Wrap<DataType>) -> RbResult<Self> {
+ rb_seq_to_list(&name, seq, &dtype.0).map(|s| s.into())
+ }
+
pub fn estimated_size(&self) -> usize {
self.series.borrow().estimated_size()
}
pub fn get_fmt(&self, index: usize, str_lengths: usize) -> String {
@@ -784,7 +798,28 @@
builder.append_value(v.try_convert::<i32>()? / 86400);
}
}
let ca: ChunkedArray<Int32Type> = builder.finish();
Ok(ca.into_date().into_series().into())
+ }
+
+ pub fn new_opt_datetime(name: String, values: RArray, _strict: Option<bool>) -> RbResult<Self> {
+ let len = values.len();
+ let mut builder = PrimitiveChunkedBuilder::<Int64Type>::new(&name, len);
+ for item in values.each() {
+ let v = item?;
+ if v.is_nil() {
+ builder.append_null();
+ } else {
+ let sec: i64 = v.funcall("to_i", ())?;
+ let nsec: i64 = v.funcall("nsec", ())?;
+ // TODO use strict
+ builder.append_value(sec * 1_000_000_000 + nsec);
+ }
+ }
+ let ca: ChunkedArray<Int64Type> = builder.finish();
+ Ok(ca
+ .into_datetime(TimeUnit::Nanoseconds, None)
+ .into_series()
+ .into())
}
}