Skip to content

Commit

Permalink
chore: update datetime logic in nodejs (#2228)
Browse files Browse the repository at this point in the history
  • Loading branch information
universalmind303 committed Dec 30, 2021
1 parent 9e8dec3 commit a29f6af
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
1 change: 0 additions & 1 deletion nodejs-polars/__tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ describe("dataframe", () => {
];

const df = pl.DataFrame(rows);

expect(df.row(0)).toEqual(Object.values(rows[0]));
expect(df.row(1)).toEqual(Object.values(rows[1]));
expect(df.columns).toEqual(Object.keys(rows[0]));
Expand Down
4 changes: 2 additions & 2 deletions nodejs-polars/src/conversion/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl FromJsUnknown for AnyValue<'_> {
if val.is_date()? {
let d: JsDate = unsafe { val.cast() };
let d = d.value_of()?;
let d = d as i64 * 1000000;
Ok(AnyValue::Datetime(d))
let d = d as i64;
Ok(AnyValue::Datetime(d, TimeUnit::Milliseconds, &None))
} else {
Err(JsPolarsEr::Other("Unsupported Data type".to_owned()).into())
}
Expand Down
6 changes: 3 additions & 3 deletions nodejs-polars/src/conversion/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ impl IntoJs<JsUnknown> for Wrap<AnyValue<'_>> {
AnyValue::Float64(v) => cx.env.create_double(v).map(|v| v.into_unknown()),
AnyValue::Date(v) => cx
.env
.create_date((v / 1000000) as f64)
.create_date(v as f64)
.map(|v| v.into_unknown()),
AnyValue::Datetime(v) => cx
AnyValue::Datetime(v, _, _) => cx
.env
.create_date((v / 1000000) as f64)
.create_date(v as f64)
.map(|v| v.into_unknown()),
AnyValue::List(v) => cx.env.to_js_value(&v).map(|v| v.into_unknown()),
_ => cx.env.get_null().map(|v| v.into_unknown()),
Expand Down
10 changes: 5 additions & 5 deletions nodejs-polars/src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ impl From<&DataType> for JsDataType {
DataType::Utf8 => Utf8,
DataType::List(_) => List,
DataType::Date => Date,
DataType::Datetime => Datetime,
DataType::Datetime(_, _) => Datetime,
DataType::Time => Time,
DataType::Object(_) => Object,
DataType::Categorical => Categorical,
DataType::Null => {
panic!("null not expected here")
DataType::Null | DataType::Unknown => {
panic!("null or unknown not expected here")
}
}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Into<DataType> for JsDataType {
JsDataType::Utf8 => Utf8,
JsDataType::List => List(DataType::Null.into()),
JsDataType::Date => Date,
JsDataType::Datetime => Datetime,
JsDataType::Datetime => Datetime(TimeUnit::Milliseconds, None),
JsDataType::Time => Time,
JsDataType::Object => Object("object"),
JsDataType::Categorical => Categorical,
Expand Down Expand Up @@ -206,7 +206,7 @@ pub fn num_to_polarstype(n: u32) -> DataType {
11 => DataType::Utf8,
12 => DataType::List(DataType::Null.into()),
13 => DataType::Date,
14 => DataType::Datetime,
14 => DataType::Datetime(TimeUnit::Milliseconds, None),
15 => DataType::Time,
16 => DataType::Object("object"),
17 => DataType::Categorical,
Expand Down
10 changes: 5 additions & 5 deletions nodejs-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn lit(cx: CallContext) -> JsResult<JsExternal> {
if obj.is_date()? {
let d: JsDate = unsafe { value.cast() };
let d = d.value_of()?;
dsl::lit(d as i64 * 1000000).cast(DataType::Datetime)
dsl::lit(d as i64).cast(DataType::Datetime(TimeUnit::Milliseconds, None))
} else {
panic!(
"could not convert value {:?} as a Literal",
Expand Down Expand Up @@ -213,11 +213,11 @@ pub fn str_parse_datetime(cx: CallContext) -> JsResult<JsExternal> {
let fmt = params.get_as::<Option<String>>("fmt")?;
let function = move |s: Series| {
let ca = s.utf8()?;
ca.as_datetime(fmt.as_deref()).map(|ca| ca.into_series())
ca.as_datetime(fmt.as_deref(), TimeUnit::Milliseconds).map(|ca| ca.into_series())
};

expr.clone()
.map(function, GetOutput::from_type(DataType::Datetime))
.map(function, GetOutput::from_type(DataType::Datetime(TimeUnit::Milliseconds, None)))
.try_into_js(&cx)
}
#[js_function(1)]
Expand Down Expand Up @@ -532,9 +532,9 @@ pub fn extend(cx: CallContext) -> JsResult<JsExternal> {
if val.is_date()? {
let d: JsDate = unsafe { val.cast() };
let d = d.value_of()?;
let d = d as i64 * 1000000;
let d = d as i64;
expr.apply(
move |s| s.extend(AnyValue::Datetime(d), n),
move |s| s.extend(AnyValue::Datetime(d, TimeUnit::Milliseconds, &None), n),
GetOutput::same_type(),
)
} else {
Expand Down
4 changes: 2 additions & 2 deletions nodejs-polars/src/list_construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ pub fn js_arr_to_list(name: &str, obj: &JsObject, dtype: &DataType) -> JsResult<
}
builder.finish().into_series()
}
DataType::Datetime => {
DataType::Datetime(_,_) => {
let mut builder = ListPrimitiveChunkedBuilder::<i64>::new(
name,
len as usize,
Expand Down Expand Up @@ -384,7 +384,7 @@ pub fn js_arr_to_list(name: &str, obj: &JsObject, dtype: &DataType) -> JsResult<
let dt_series = inner_builder
.finish()
.into_series()
.cast(&DataType::Datetime)
.cast(&DataType::Datetime(TimeUnit::Milliseconds, None))
.map_err(JsPolarsEr::from)?;
builder.append_series(&dt_series);
}
Expand Down
12 changes: 7 additions & 5 deletions nodejs-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn new_opt_date(cx: CallContext) -> JsResult<JsExternal> {
if obj.is_date()? {
let d: &napi::JsDate = unsafe { &item.0.cast() };
match d.value_of() {
Ok(v) => builder.append_value(v as i64 * 1000000),
Ok(v) => builder.append_value(v as i64),
Err(e) => {
if strict.unwrap_or(false) {
return Err(e);
Expand All @@ -71,7 +71,9 @@ pub fn new_opt_date(cx: CallContext) -> JsResult<JsExternal> {
}
}
let ca: ChunkedArray<Int64Type> = builder.finish();
ca.into_date().into_series().try_into_js(&cx)
ca.into_datetime(TimeUnit::Milliseconds, None)
.into_series()
.try_into_js(&cx)
}

#[js_function(1)]
Expand Down Expand Up @@ -578,7 +580,7 @@ pub fn str_parse_datetime(cx: CallContext) -> JsResult<JsExternal> {
let series = params.get_external::<Series>(&cx, "_series")?;
let fmt = params.get_as::<Option<&str>>("fmt")?;
if let Ok(ca) = series.utf8() {
ca.as_datetime(fmt)
ca.as_datetime(fmt, TimeUnit::Milliseconds)
.map_err(JsPolarsEr::from)?
.into_series()
.try_into_js(&cx)
Expand Down Expand Up @@ -812,7 +814,7 @@ pub fn get_date(cx: CallContext) -> JsResult<JsUnknown> {
match ca.get(index) {
Some(v) => cx
.env
.create_date((v / 1000000) as f64)
.create_date(v as f64)
.map(|v| v.into_unknown()),
None => cx.env.get_null().map(|v| v.into_unknown()),
}
Expand All @@ -837,7 +839,7 @@ pub fn get_datetime(cx: CallContext) -> JsResult<JsUnknown> {
Some(v) => {
println!("value={:#?}", v);
cx.env
.create_date((v / 1000000) as f64)
.create_date(v as f64)
.map(|v| v.into_unknown())
}
None => {
Expand Down

0 comments on commit a29f6af

Please sign in to comment.