Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Decimal type #210

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions __tests__/dataframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,19 @@ describe("io", () => {
fs.rmSync("./test.csv");
done();
});
test("writeParquet", (done) => {
const df = pl.DataFrame([
pl.Series("doo", [1, 2, 3], pl.Decimal),
pl.Series("foo", [1, 2, 3], pl.UInt32),
pl.Series("bar", ["a", "b", "c"]),
]);
const pqFile = "./test.parquet";
df.writeParquet(pqFile);
const newDF = pl.readParquet(pqFile);
expect(newDF).toFrameEqual(df);
fs.rmSync(pqFile);
done();
});
test("JSON.stringify", () => {
const df = pl.DataFrame({
foo: [1],
Expand Down Expand Up @@ -1973,30 +1986,21 @@ describe("create", () => {
{
a: [1, 2, 3],
b: ["1", "2", "3"],
d: [1, 2, 3],
},
{
schema: {
x: pl.Int32,
y: pl.String,
z: pl.Decimal,
},
},
);
expect(df.schema).toStrictEqual({ x: pl.Int32, y: pl.String });
});
test("with schema", () => {
const df = pl.DataFrame(
{
a: [1, 2, 3],
b: ["1", "2", "3"],
},
{
schema: {
x: pl.Int32,
y: pl.String,
},
},
);
expect(df.schema).toStrictEqual({ x: pl.Int32, y: pl.String });
expect(df.schema).toStrictEqual({
x: pl.Int32,
y: pl.String,
z: pl.Decimal,
});
});
test("with schema overrides", () => {
const df = pl.DataFrame(
Expand Down
1 change: 1 addition & 0 deletions polars/dataframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ function mapPolarsTypeToJSONSchema(colType: DataType): string {
UInt64: "integer",
Float32: "number",
Float64: "number",
Decimal: "number",
Date: "string",
Datetime: "string",
Utf8: "string",
Expand Down
8 changes: 7 additions & 1 deletion polars/datatypes/datatype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export abstract class DataType {
public static get UInt64(): DataType {
return new _UInt64();
}

/** A `f32` */
public static get Float32(): DataType {
return new _Float32();
Expand All @@ -65,6 +64,10 @@ export abstract class DataType {
public static get Float64(): DataType {
return new _Float64();
}
/** A `decimal` */
public static get Decimal(): DataType {
return new _Decimal();
}
public static get Date(): DataType {
return new _Date();
}
Expand Down Expand Up @@ -163,6 +166,7 @@ class _UInt32 extends DataType {}
class _UInt64 extends DataType {}
class _Float32 extends DataType {}
class _Float64 extends DataType {}
class _Decimal extends DataType {}
class _Date extends DataType {}
class _Time extends DataType {}
class _Object extends DataType {}
Expand Down Expand Up @@ -299,6 +303,8 @@ export namespace DataType {
export type Float32 = _Float32;
/** Float64 */
export type Float64 = _Float64;
/** Decimal */
export type Decimal = _Decimal;
/** Date dtype */
export type Date = _Date;
/** Datetime */
Expand Down
4 changes: 4 additions & 0 deletions polars/datatypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const DTYPE_TO_FFINAME = {
UInt64: "U64",
Float32: "F32",
Float64: "F64",
Decimal: "Decimal",
Bool: "Bool",
Utf8: "Str",
String: "Str",
Expand All @@ -61,6 +62,9 @@ const POLARS_TYPE_TO_CONSTRUCTOR: Record<string, any> = {
Float64(name, values, strict?) {
return pli.JsSeries.newOptF64(name, values, strict);
},
Decimal(name, values, strict?) {
return pli.JsSeries.newOptDecimal(name, values, strict);
},
Int8(name, values, strict?) {
return pli.JsSeries.newOptI32(name, values, strict);
},
Expand Down
1 change: 1 addition & 0 deletions polars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export namespace pl {
export import UInt64 = DataType.UInt64;
export import Float32 = DataType.Float32;
export import Float64 = DataType.Float64;
export import Decimal = DataType.Decimal;
export import Bool = DataType.Bool;
export import Utf8 = DataType.Utf8;
// biome-ignore lint/suspicious/noShadowRestrictedNames: pl.String
Expand Down
25 changes: 25 additions & 0 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,29 @@ impl FromNapiValue for Wrap<ChunkedArray<UInt64Type>> {
}
}

impl FromNapiValue for Wrap<ChunkedArray<Int128Type>> {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> JsResult<Self> {
let arr = Array::from_napi_value(env, napi_val)?;
let len = arr.len() as usize;
let mut builder = PrimitiveChunkedBuilder::<Int128Type>::new("", len);
for i in 0..len {
match arr.get::<BigInt>(i as u32) {
Ok(val) => match val {
Some(v) => {
let (v, _b) = v.get_i128();
builder.append_value(v)
}
None => builder.append_null(),
},
Err(_) => {
builder.append_null()
}
}
}
Ok(Wrap(builder.finish()))
}
}

impl FromNapiValue for Wrap<Expr> {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> JsResult<Self> {
let obj = Object::from_napi_value(env, napi_val)?;
Expand Down Expand Up @@ -634,6 +657,7 @@ impl FromNapiValue for Wrap<DataType> {
"UInt64" => DataType::UInt64,
"Float32" => DataType::Float32,
"Float64" => DataType::Float64,
"Decimal" => DataType::Decimal(None, None),
"Bool" => DataType::Boolean,
"Utf8" => DataType::String,
"String" => DataType::String,
Expand Down Expand Up @@ -906,6 +930,7 @@ impl ToNapiValue for Wrap<DataType> {
DataType::UInt64 => String::to_napi_value(env, "UInt64".to_owned()),
DataType::Float32 => String::to_napi_value(env, "Float32".to_owned()),
DataType::Float64 => String::to_napi_value(env, "Float64".to_owned()),
DataType::Decimal(_,_) => String::to_napi_value(env, "Decimal".to_owned()),
DataType::Boolean => String::to_napi_value(env, "Bool".to_owned()),
DataType::String => String::to_napi_value(env, "String".to_owned()),
DataType::List(inner) => {
Expand Down
9 changes: 9 additions & 0 deletions src/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub enum JsDataType {
UInt64,
Float32,
Float64,
Decimal,
Bool,
Utf8,
String,
Expand All @@ -36,6 +37,7 @@ impl JsDataType {
"UInt64" => JsDataType::UInt64,
"Float32" => JsDataType::Float32,
"Float64" => JsDataType::Float64,
"Decimal" => JsDataType::Decimal,
"Bool" => JsDataType::Bool,
"Utf8" => JsDataType::Utf8,
"String" => JsDataType::String,
Expand Down Expand Up @@ -65,6 +67,7 @@ impl From<&DataType> for JsDataType {
DataType::UInt64 => UInt64,
DataType::Float32 => Float32,
DataType::Float64 => Float64,
DataType::Decimal(..) => Decimal,
DataType::Boolean => Bool,
DataType::String => Utf8,
DataType::List(_) => List,
Expand Down Expand Up @@ -115,6 +118,7 @@ pub enum JsAnyValue {
Int64(i64),
Float32(f32),
Float64(f64),
Decimal(i128, usize),
Date(i32),
Datetime(i64, TimeUnit, Option<TimeZone>),
Duration(i64, TimeUnit),
Expand Down Expand Up @@ -227,6 +231,7 @@ impl ToNapiValue for JsAnyValue {
JsAnyValue::UInt64(n) => u64::to_napi_value(env, n),
JsAnyValue::Float32(n) => f64::to_napi_value(env, n as f64),
JsAnyValue::Float64(n) => f64::to_napi_value(env, n),
JsAnyValue::Decimal(n, _u) => i128::to_napi_value(env, n),
JsAnyValue::Utf8(s) => String::to_napi_value(env, s),
JsAnyValue::String(s) => String::to_napi_value(env, s),
JsAnyValue::Date(v) => {
Expand Down Expand Up @@ -276,6 +281,7 @@ impl<'a> From<JsAnyValue> for AnyValue<'a> {
JsAnyValue::Int64(v) => AnyValue::Int64(v),
JsAnyValue::Float32(v) => AnyValue::Float32(v),
JsAnyValue::Float64(v) => AnyValue::Float64(v),
JsAnyValue::Decimal(v, u) => AnyValue::Decimal(v, u),
JsAnyValue::Date(v) => AnyValue::Date(v),
JsAnyValue::Datetime(v, w, _) => AnyValue::Datetime(v, w, &None),
JsAnyValue::Duration(v, _) => AnyValue::Duration(v, TimeUnit::Milliseconds),
Expand All @@ -302,6 +308,7 @@ impl From<AnyValue<'_>> for JsAnyValue {
AnyValue::Int64(v) => JsAnyValue::Int64(v),
AnyValue::Float32(v) => JsAnyValue::Float32(v),
AnyValue::Float64(v) => JsAnyValue::Float64(v),
AnyValue::Decimal(v, u) => JsAnyValue::Decimal(v, u),
AnyValue::Date(v) => JsAnyValue::Date(v),
AnyValue::Datetime(v, w, _) => JsAnyValue::Datetime(v, w, None),
AnyValue::Duration(v, _) => JsAnyValue::Duration(v, TimeUnit::Milliseconds),
Expand All @@ -328,6 +335,7 @@ impl From<&JsAnyValue> for DataType {
JsAnyValue::Int64(_) => DataType::Int64,
JsAnyValue::Float32(_) => DataType::Float32,
JsAnyValue::Float64(_) => DataType::Float64,
JsAnyValue::Decimal(_,_) => DataType::Decimal(None, None),
JsAnyValue::Date(_) => DataType::Date,
JsAnyValue::Datetime(_, _, _) => DataType::Datetime(TimeUnit::Milliseconds, None),
JsAnyValue::Duration(_, _) => DataType::Duration(TimeUnit::Milliseconds),
Expand Down Expand Up @@ -392,6 +400,7 @@ impl Into<DataType> for JsDataType {
JsDataType::UInt64 => UInt64,
JsDataType::Float32 => Float32,
JsDataType::Float64 => Float64,
JsDataType::Decimal => Decimal(None, None),
JsDataType::Bool => Boolean,
JsDataType::Utf8 => String,
JsDataType::String => String,
Expand Down
6 changes: 6 additions & 0 deletions src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ impl JsSeries {
s.rename(&name);
JsSeries::new(s)
}
#[napi(factory, catch_unwind)]
pub fn new_opt_decimal(name: String, val: Wrap<Int128Chunked>, _strict: bool) -> JsSeries {
let mut s = val.0.into_series();
s.rename(&name);
JsSeries::new(s)
}

#[napi(factory, catch_unwind)]
pub fn new_opt_date(
Expand Down
Loading