Skip to content

Commit

Permalink
Nodejs updates (#3547)
Browse files Browse the repository at this point in the history
* fix node compile errors

* wip: nodejs updates

* update rolling

* revert jsonpath
  • Loading branch information
universalmind303 committed Jun 1, 2022
1 parent 4e21538 commit ed825ab
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 176 deletions.
1 change: 1 addition & 0 deletions nodejs-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_json = { version = "1" }

[dependencies.polars]
features = [
"rolling_window",
"json",
"dynamic_groupby",
"zip_with",
Expand Down
27 changes: 11 additions & 16 deletions nodejs-polars/__tests__/expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1927,16 +1927,16 @@ describe("expr.dt", () => {
pl.Series("date_col", [dt], pl.Datetime)
]);
const expected = pl.DataFrame({
millisecond: [0],
second: [30],
minute: [10],
hour: [1],
day: [8],
ordinalDay: [8],
weekday: [6],
week: [1],
month: [1],
year: [1984]
millisecond: pl.Series("", [0.0], pl.UInt32),
second: pl.Series("", [30], pl.UInt32),
minute: pl.Series("", [10], pl.UInt32),
hour: pl.Series("", [1], pl.UInt32),
day: pl.Series("", [8], pl.UInt32),
ordinalDay: pl.Series("", [8], pl.UInt32),
weekday: pl.Series("", [7], pl.UInt32),
week: pl.Series("", [1], pl.UInt32),
month: pl.Series("", [1], pl.UInt32),
year: pl.Series("", [1984], pl.Int32),
});
const dtCol = col("date_col").date;
const dtSeries = df.getColumn("date_col").date;
Expand Down Expand Up @@ -1980,7 +1980,7 @@ describe("expr metadata", () => {
});
});

describe("rolling", () => {
describe.only("rolling", () => {
test("rollingMax", () => {
const df = pl.Series("rolling", [1, 2, 3, 2, 1]).toFrame();
const expected = pl.Series("rolling", [null, 2, 3, 3, 2], pl.Float64).toFrame();
Expand Down Expand Up @@ -2034,13 +2034,8 @@ describe("rolling", () => {
.rollingQuantile({windowSize: 2, quantile: 0.5})
.prefix("rolling_quantile_")
);
const seriesActual = df
.getColumn("a")
.rollingQuantile({windowSize: 2, quantile: 0.5})
.rename("rolling_quantile_a");

expect(actual).toFrameStrictEqual(expected);
expect(seriesActual).toSeriesStrictEqual(expected["rolling_quantile_a"]);
});
test("rollingSkew", () => {
const df = pl.DataFrame({"a": [1, 2, 3, 3, 2, 10, 8]});
Expand Down
6 changes: 3 additions & 3 deletions nodejs-polars/polars/lazy/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export const _Expr = (_expr: any): Expr => {
throw new Error("window size is required");
}
const callOpts = {
windowSize: opts?.["windowSize"] ?? (typeof opts === "number"? opts : null),
windowSize: `${windowSize}i`,
weights: opts?.["weights"] ?? weights,
minPeriods: opts?.["minPeriods"] ?? minPeriods ?? windowSize,
center : opts?.["center"] ?? center ?? false,
Expand Down Expand Up @@ -885,7 +885,7 @@ export const _Expr = (_expr: any): Expr => {
val,
interpolation ?? "nearest",
{
windowSize,
windowSize: `${windowSize}i`,
weights,
minPeriods,
center
Expand All @@ -896,7 +896,7 @@ export const _Expr = (_expr: any): Expr => {
throw new Error("window size is required");
}
const options = {
windowSize: val?.["windowSize"] ?? (typeof val === "number"? val : null),
windowSize: `${windowSize}i`,
weights: val?.["weights"] ?? weights,
minPeriods: val?.["minPeriods"] ?? minPeriods ?? windowSize,
center : val?.["center"] ?? center ?? false,
Expand Down
1 change: 0 additions & 1 deletion nodejs-polars/polars/series/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const SeriesListFunctions = (_s): ListFunctions<Series> => {

return {
argMax() {

return wrap("argMax");
},
argMin() {
Expand Down
110 changes: 31 additions & 79 deletions nodejs-polars/polars/series/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,20 +1026,13 @@ export function _Series(_s: any): Series {
return pli[internalMethod](_s, ...args);
};

const rolling = (method: string, opts, weights?, minPeriods?, center?) => {
const windowSize = opts?.["windowSize"] ?? (typeof opts === "number" ? opts : null);
if (windowSize === null) {
throw new Error("window size is required");
}
const callOpts = {
windowSize: opts?.["windowSize"] ?? (typeof opts === "number" ? opts : null),
weights: opts?.["weights"] ?? weights,
minPeriods: opts?.["minPeriods"] ?? minPeriods ?? windowSize,
center: opts?.["center"] ?? center ?? false,
};

return _Series(_s[method](callOpts));
const expr_op = (method: string, ...args) => {
return _Series(_s)
.toFrame()
.select(col(_s.name)[method](...args))
.getColumn(_s.name);
};

const series = {
_s,
[inspect]() {
Expand Down Expand Up @@ -1149,10 +1142,7 @@ export function _Series(_s: any): Series {
return _Series(s);
},
cumCount(reverse?) {
return this
.toFrame()
.select(col(this.name).cumCount(reverse))
.getColumn(this.name);
return expr_op("cumCount", reverse);
},
cumSum(reverse?) {
return _Series(_s.cumsum(reverse));
Expand Down Expand Up @@ -1473,62 +1463,34 @@ export function _Series(_s: any): Series {
return this.alias(obj?.name ?? obj);
}
},
rollingMax(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingMax", windowSize, weights, minPeriods, center);


rollingMax(...args) {
return expr_op("rollingMax", ...args);
},
rollingMean(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingMean", windowSize, weights, minPeriods, center);
rollingMean(...args) {
return expr_op("rollingMean", ...args);
},
rollingMin(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingMin", windowSize, weights, minPeriods, center);
rollingMin(...args) {
return expr_op("rollingMin", ...args);
},
rollingSum(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingSum", windowSize, weights, minPeriods, center);
rollingSum(...args) {
return expr_op("rollingSum", ...args);
},
rollingStd(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingStd", windowSize, weights, minPeriods, center);
rollingStd(...args) {
return expr_op("rollingStd", ...args);
},
rollingVar(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingVar", windowSize, weights, minPeriods, center);
rollingVar(...args) {
return expr_op("rollingVar", ...args);
},
rollingMedian(windowSize, weights?, minPeriods?, center? ) {
return rolling("rollingMedian", windowSize, weights, minPeriods, center);
rollingMedian(...args) {
return expr_op("rollingMedian", ...args);
},
rollingQuantile(val, interpolation = "nearest", windowSize?, weights?, minPeriods?, center?) {
if(typeof val === "number") {

return wrap("rollingQuantile",
val,
interpolation ?? "nearest",
{
windowSize,
weights,
minPeriods,
center
});
}
windowSize = val?.["windowSize"] ?? (typeof val === "number" ? val : null);
if(windowSize === null) {
throw new Error("window size is required");
}
const options = {
windowSize: val?.["windowSize"] ?? (typeof val === "number"? val : null),
weights: val?.["weights"] ?? weights,
minPeriods: val?.["minPeriods"] ?? minPeriods ?? windowSize,
center : val?.["center"] ?? center ?? false,
};

return wrap("rollingQuantile",
val.quantile,
val.interpolation ?? "nearest",
options
);
rollingQuantile(...args) {
return expr_op("rollingQuantile", ...args);
},
rollingSkew(windowSize, bias?) {
return this
.toFrame()
.select(col(this.name).rollingSkew(windowSize, bias))
.getColumn(this.name);
rollingSkew(...args) {
return expr_op("rollingSkew", ...args);
},
floor() {
return wrap("floor");
Expand All @@ -1548,13 +1510,8 @@ export function _Series(_s: any): Series {
throw new InvalidOperationError("round", this.dtype);
}
},
clip(arg, max?) {
return this
.toFrame()
.select(
col(this.name).clip(arg, max)
)
.getColumn(this.name);
clip(...args) {
return expr_op("clip", ...args);
},
setAtIdx(indices, value) {
indices = Series.isSeries(indices) ? indices.cast(DataType.UInt32).toArray() : indices;
Expand Down Expand Up @@ -1605,13 +1562,8 @@ export function _Series(_s: any): Series {
shift(periods=1) {
return wrap("shift", periods);
},
shiftAndFill(periods, fillValue?) {
return this
.toFrame()
.select(
col(this.name).shiftAndFill(periods, fillValue)
)
.getColumn(this.name);
shiftAndFill(...args) {
return expr_op("shiftAndFill", ...args);
},
shrinkToFit(inPlace?: boolean) {
if(inPlace) {
Expand Down
20 changes: 18 additions & 2 deletions nodejs-polars/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,19 +480,35 @@ impl<'a> FromNapiValue for Wrap<&'a str> {

#[napi(object)]
pub struct JsRollingOptions {
pub window_size: i64,
pub window_size: String,
pub weights: Option<Vec<f64>>,
pub min_periods: i64,
pub center: bool,
}

impl From<JsRollingOptions> for RollingOptionsImpl<'static> {
fn from(o: JsRollingOptions) -> Self {
RollingOptionsImpl {
window_size: Duration::parse(&o.window_size),
weights: o.weights,
min_periods: o.min_periods as usize,
center: o.center,
by: None,
tu: None,
closed_window: None,
}
}
}

impl From<JsRollingOptions> for RollingOptions {
fn from(o: JsRollingOptions) -> Self {
RollingOptions {
window_size: o.window_size as usize,
window_size: Duration::parse(&o.window_size),
weights: o.weights,
min_periods: o.min_periods as usize,
center: o.center,
by: None,
closed_window: None
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions nodejs-polars/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,8 @@ impl JsExpr {
self.inner.clone().log(base).into()
}
#[napi]
pub fn entropy(&self, base: f64) -> JsExpr {
self.inner.clone().entropy(base).into()
pub fn entropy(&self, base: f64, normalize: bool) -> JsExpr {
self.inner.clone().entropy(base, normalize).into()
}
#[napi]
pub fn add(&self, rhs: &JsExpr) -> JsExpr {
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 @@ -15,7 +15,7 @@ macro_rules! typed_to_chunked {
macro_rules! typed_option_or_null {
($name:expr, $arr:expr, $type:ty, $dtype:expr, $pl_type:ty) => {{
let len = $arr.len();
let mut builder = ListPrimitiveChunkedBuilder::<$type>::new(
let mut builder = ListPrimitiveChunkedBuilder::<$pl_type>::new(
$name,
len as usize,
(len as usize) * 5,
Expand Down Expand Up @@ -55,7 +55,7 @@ macro_rules! typed_option_or_null {
macro_rules! build_list_with_downcast {
($name:expr, $arr:expr, $type:ty, $dtype:expr, $pl_type:ty) => {{
let len = $arr.len();
let mut builder = ListPrimitiveChunkedBuilder::<$type>::new(
let mut builder = ListPrimitiveChunkedBuilder::<$pl_type>::new(
$name,
len as usize,
(len as usize) * 5,
Expand Down
71 changes: 0 additions & 71 deletions nodejs-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,77 +925,6 @@ impl JsSeries {
None
}
}

#[napi]
pub fn rolling_sum(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_sum(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_mean(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_mean(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_median(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_median(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_max(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_max(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_min(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_min(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_var(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_var(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_std(&self, options: JsRollingOptions) -> napi::Result<JsSeries> {
let s = self
.series
.rolling_std(options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn rolling_quantile(
&self,
quantile: f64,
interpolation: Wrap<QuantileInterpolOptions>,
options: JsRollingOptions,
) -> napi::Result<JsSeries> {
let interpol = interpolation.0;
let s = self
.series
.rolling_quantile(quantile, interpol, options.into())
.map_err(JsPolarsErr::from)?;
Ok(s.into())
}
#[napi]
pub fn year(&self) -> napi::Result<JsSeries> {
let s = self.series.year().map_err(JsPolarsErr::from)?;
Expand Down

0 comments on commit ed825ab

Please sign in to comment.