From 1bcb7791e9b81e895b781d1f6b7973208146f325 Mon Sep 17 00:00:00 2001 From: Bidek56 Date: Sat, 6 Jan 2024 16:41:51 -0500 Subject: [PATCH] Adding error message to date repeat Liniting fixes --- __tests__/functions.test.ts | 8 ++- __tests__/series.test.ts | 4 +- __tests__/setup.ts | 55 +++++++++--------- polars/dataframe.ts | 49 ++++++---------- polars/datatypes/datatype.ts | 19 +++---- polars/datatypes/field.ts | 6 +- polars/groupby.ts | 22 ++++---- polars/internals/construction.ts | 19 +++---- polars/io.ts | 37 +++++-------- polars/lazy/dataframe.ts | 24 ++++---- polars/lazy/expr/index.ts | 95 ++++++++++++++------------------ polars/lazy/expr/list.ts | 6 +- polars/lazy/expr/string.ts | 10 ++-- polars/lazy/functions.ts | 56 ++++++++----------- polars/series/index.ts | 38 +++++-------- src/series.rs | 2 +- 16 files changed, 191 insertions(+), 259 deletions(-) diff --git a/__tests__/functions.test.ts b/__tests__/functions.test.ts index 6548389c5..958d420b2 100644 --- a/__tests__/functions.test.ts +++ b/__tests__/functions.test.ts @@ -28,12 +28,12 @@ describe("concat", () => { }); it("cant concat empty list", () => { const fn = () => pl.concat([]); - expect(fn).toThrowError(); + expect(fn).toThrow(); }); it("can only concat series and df", () => { const fn = () => pl.concat([[1] as any, [2] as any]); - expect(fn).toThrowError(); + expect(fn).toThrow(); }); test("horizontal concat", () => { const a = pl.DataFrame({ a: ["a", "b"], b: [1, 2] }); @@ -75,6 +75,10 @@ describe("repeat", () => { expect(actual).toSeriesEqual(expected); }); + it("fail to repeat a date value", () => { + const fn = () => pl.repeat(new Date(), 4, "foo"); + expect(fn).toThrow(); + }); }); describe("horizontal", () => { it("compute the bitwise AND horizontally across columns.", () => { diff --git a/__tests__/series.test.ts b/__tests__/series.test.ts index 41a01b3c2..ac98a8cb1 100644 --- a/__tests__/series.test.ts +++ b/__tests__/series.test.ts @@ -546,10 +546,10 @@ describe("series", () => { } }); it("describe", () => { - expect(() => pl.Series([]).describe()).toThrowError( + expect(() => pl.Series([]).describe()).toThrow( "Series must contain at least one value", ); - expect(() => pl.Series("dt", [null], pl.Date).describe()).toThrowError( + expect(() => pl.Series("dt", [null], pl.Date).describe()).toThrow( "Invalid operation: describe is not supported for DataType(Date)", ); let actual = pl.Series([true, false, true]).describe(); diff --git a/__tests__/setup.ts b/__tests__/setup.ts index 864aa00d5..8f4bb9329 100644 --- a/__tests__/setup.ts +++ b/__tests__/setup.ts @@ -9,16 +9,15 @@ expect.extend({ message: () => "series matches", pass: true, }; - } else { - return { - message: () => ` + } + return { + message: () => ` Expected: >>${expected} Received: >>${actual}`, - pass: false, - }; - } + pass: false, + }; }, toSeriesEqual(actual, expected) { const pass = actual.seriesEqual(expected); @@ -27,16 +26,15 @@ Received: message: () => "series matches", pass: true, }; - } else { - return { - message: () => ` + } + return { + message: () => ` Expected: >>${expected} Received: >>${actual}`, - pass: false, - }; - } + pass: false, + }; }, toFrameEqual(actual, expected, nullEqual?) { const pass = actual.frameEqual(expected, nullEqual); @@ -45,16 +43,15 @@ Received: message: () => "dataframes match", pass: true, }; - } else { - return { - message: () => ` + } + return { + message: () => ` Expected: >>${expected} Received: >>${actual}`, - pass: false, - }; - } + pass: false, + }; }, toFrameStrictEqual(actual, expected) { const frameEq = actual.frameEqual(expected); @@ -64,16 +61,15 @@ Received: message: () => "dataframes match", pass: true, }; - } else { - return { - message: () => ` + } + return { + message: () => ` Expected: >>${expected} Received: >>${actual}`, - pass: false, - }; - } + pass: false, + }; }, toFrameEqualIgnoringOrder(act: pl.DataFrame, exp: pl.DataFrame) { const actual = act.sort(act.columns.sort()); @@ -84,16 +80,15 @@ Received: message: () => "dataframes match", pass: true, }; - } else { - return { - message: () => ` + } + return { + message: () => ` Expected: >>${expected} Received: >>${actual}`, - pass: false, - }; - } + pass: false, + }; }, }); diff --git a/polars/dataframe.ts b/polars/dataframe.ts index 6d625bb5f..e84ef6348 100644 --- a/polars/dataframe.ts +++ b/polars/dataframe.ts @@ -1708,9 +1708,8 @@ export interface DataFrame function prepareOtherArg(anyValue: any): Series { if (Series.isSeries(anyValue)) { return anyValue; - } else { - return Series([anyValue]) as Series; } + return Series([anyValue]) as Series; } function map(df: DataFrame, fn: (...args: any[]) => any[]) { @@ -1841,9 +1840,8 @@ export const _DataFrame = (_df: any): DataFrame => { df.getColumns().map((s) => { if (s.isNumeric() || s.isBoolean()) { return s.cast(DataType.Float64); - } else { - return s; } + return s; }), ); }; @@ -1877,9 +1875,8 @@ export const _DataFrame = (_df: any): DataFrame => { dropNulls(...subset) { if (subset.length) { return wrap("dropNulls", subset.flat(2)); - } else { - return wrap("dropNulls"); } + return wrap("dropNulls"); }, distinct(opts: any = false, subset?, keep = "first") { return this.unique(opts, subset); @@ -2037,9 +2034,8 @@ export const _DataFrame = (_df: any): DataFrame => { max(axis = 0) { if (axis === 1) { return _Series(_df.hmax() as any) as any; - } else { - return wrap("max"); } + return wrap("max"); }, mean(axis = 0, nullStrategy = "ignore") { if (axis === 1) { @@ -2057,9 +2053,8 @@ export const _DataFrame = (_df: any): DataFrame => { min(axis = 0) { if (axis === 1) { return _Series(_df.hmin() as any) as any; - } else { - return wrap("min"); } + return wrap("min"); }, nChunks() { return _df.nChunks(); @@ -2168,28 +2163,25 @@ export const _DataFrame = (_df: any): DataFrame => { false, seed, ); - } else { - throw new TypeError("must specify either 'frac' or 'n'"); } + throw new TypeError("must specify either 'frac' or 'n'"); }, select(...selection) { const hasExpr = selection.flat().some((s) => Expr.isExpr(s)); if (hasExpr) { return _DataFrame(_df).lazy().select(selection).collectSync(); - } else { - return wrap("select", columnOrColumnsStrict(selection as any)); } + return wrap("select", columnOrColumnsStrict(selection as any)); }, shift: (opt) => wrap("shift", opt?.periods ?? opt), shiftAndFill(n: any, fillValue?: number | undefined) { if (typeof n === "number" && fillValue) { return _DataFrame(_df).lazy().shiftAndFill(n, fillValue).collectSync(); - } else { - return _DataFrame(_df) - .lazy() - .shiftAndFill(n.n, n.fillValue) - .collectSync(); } + return _DataFrame(_df) + .lazy() + .shiftAndFill(n.n, n.fillValue) + .collectSync(); }, shrinkToFit(inPlace: any = false): any { if (inPlace) { @@ -2408,9 +2400,8 @@ export const _DataFrame = (_df: any): DataFrame => { } if (!options?.columnNames) { return wrap("transpose", keep_names_as, undefined); - } else { - return wrap("transpose", keep_names_as, options.columnNames); } + return wrap("transpose", keep_names_as, options.columnNames); }, unnest(names) { names = Array.isArray(names) ? names : [names]; @@ -2428,9 +2419,8 @@ export const _DataFrame = (_df: any): DataFrame => { withColumn(column: Series | Expr) { if (Series.isSeries(column)) { return wrap("withColumn", column.inner()); - } else { - return this.withColumns(column); } + return this.withColumns(column); }, withColumns(...columns: (Expr | Series)[]) { if (isSeriesArray(columns)) { @@ -2438,18 +2428,16 @@ export const _DataFrame = (_df: any): DataFrame => { (acc, curr) => acc.withColumn(curr), _DataFrame(_df), ); - } else { - return this.lazy() - .withColumns(columns) - .collectSync({ noOptimization: true, stringCache: false }); } + return this.lazy() + .withColumns(columns) + .collectSync({ noOptimization: true, stringCache: false }); }, withColumnRenamed(opt, replacement?) { if (typeof opt === "string") { return this.rename({ [opt]: replacement }); - } else { - return this.rename({ [opt.existing]: opt.replacement }); } + return this.rename({ [opt.existing]: opt.replacement }); }, withRowCount(name = "row_nr") { return wrap("withRowCount", name); @@ -2477,9 +2465,8 @@ export const _DataFrame = (_df: any): DataFrame => { } if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) { return target.row(Number(prop)); - } else { - return Reflect.get(target, prop, receiver); } + return Reflect.get(target, prop, receiver); }, set(target: DataFrame, prop, receiver) { if (Series.isSeries(receiver)) { diff --git a/polars/datatypes/datatype.ts b/polars/datatypes/datatype.ts index 77663f6e3..4cc1a846c 100644 --- a/polars/datatypes/datatype.ts +++ b/polars/datatypes/datatype.ts @@ -123,9 +123,8 @@ export abstract class DataType { toString() { if (this.inner) { return `${this.identity}(${this.variant}(${this.inner}))`; - } else { - return `${this.identity}(${this.variant})`; } + return `${this.identity}(${this.variant})`; } toJSON() { const inner = (this as any).inner; @@ -136,11 +135,10 @@ export abstract class DataType { [this.variant]: inner[0], }, }; - } else { - return { - [this.identity]: this.variant, - }; } + return { + [this.identity]: this.variant, + }; } [Symbol.for("nodejs.util.inspect.custom")]() { return this.toJSON(); @@ -186,9 +184,8 @@ class _Datetime extends DataType { this.timeUnit === (other as _Datetime).timeUnit && this.timeZone === (other as _Datetime).timeZone ); - } else { - return false; } + return false; } } @@ -202,9 +199,8 @@ class _List extends DataType { override equals(other: DataType): boolean { if (other.variant === this.variant) { return this.inner[0].equals((other as _List).inner[0]); - } else { - return false; } + return false; } } @@ -237,9 +233,8 @@ class _Struct extends DataType { return otherfld.name === fld.name && otherfld.dtype.equals(fld.dtype); }) .every((value) => value); - } else { - return false; } + return false; } override toJSON() { return { diff --git a/polars/datatypes/field.ts b/polars/datatypes/field.ts index 62de549cf..4d076452b 100644 --- a/polars/datatypes/field.ts +++ b/polars/datatypes/field.ts @@ -31,10 +31,10 @@ export namespace Field { export function from(nameOrObj, dtype?: DataType): Field { if (typeof nameOrObj === "string" && dtype) { return new Field(nameOrObj, dtype); - } else if (Array.isArray(nameOrObj)) { + } + if (Array.isArray(nameOrObj)) { return new Field(nameOrObj[0], nameOrObj[1]); - } else { - return new Field(nameOrObj.name, nameOrObj.dtype); } + return new Field(nameOrObj.name, nameOrObj.dtype); } } diff --git a/polars/groupby.ts b/polars/groupby.ts index e12061d85..e89c66742 100644 --- a/polars/groupby.ts +++ b/polars/groupby.ts @@ -174,9 +174,8 @@ export function _GroupBy(df: any, by: string[], maintainOrder = false) { if (typeof opts === "string") { if (valuesCol) { return pivot({ pivotCol: opts, valuesCol }); - } else { - throw new Error("must specify both pivotCol and valuesCol"); } + throw new Error("must specify both pivotCol and valuesCol"); } return PivotOps(df, by, opts.pivotCol, opts.valuesCol); @@ -191,17 +190,16 @@ export function _GroupBy(df: any, by: string[], maintainOrder = false) { .groupBy(by, maintainOrder) .agg(...aggs) .collectSync({ noOptimization: true }); - } else { - const pairs = Object.entries(aggs[0]).flatMap(([key, values]) => { - return [values].flat(2).map((v) => col(key)[v as any]()); - }); - - return _DataFrame(df) - .lazy() - .groupBy(by, maintainOrder) - .agg(...pairs) - .collectSync({ noOptimization: true }); } + const pairs = Object.entries(aggs[0]).flatMap(([key, values]) => { + return [values].flat(2).map((v) => col(key)[v as any]()); + }); + + return _DataFrame(df) + .lazy() + .groupBy(by, maintainOrder) + .agg(...pairs) + .collectSync({ noOptimization: true }); }; return Object.seal({ diff --git a/polars/internals/construction.ts b/polars/internals/construction.ts index 976f5e195..723790764 100644 --- a/polars/internals/construction.ts +++ b/polars/internals/construction.ts @@ -214,9 +214,8 @@ export function arrayToJsDataFrame(data: any[], options?): any { if (columns) df.columns = columns; return df; - } else { - dataSeries = data.map((s, idx) => (Series(`column_${idx}`, s) as any)._s); } + dataSeries = data.map((s, idx) => (Series(`column_${idx}`, s) as any)._s); } else { dataSeries = [(Series("column_0", data) as any)._s]; } @@ -228,15 +227,15 @@ export function arrayToJsDataFrame(data: any[], options?): any { function handleColumnsArg(data: any[], columns?: string[]) { if (!columns) { return data; - } else { - if (!data) { - return columns.map((c) => (Series.from(c, []) as any)._s); - } else if (data.length === columns.length) { - for (const [idx, name] of columns.entries()) { - data[idx].rename(name); - } - return data; + } + if (!data) { + return columns.map((c) => (Series.from(c, []) as any)._s); + } + if (data.length === columns.length) { + for (const [idx, name] of columns.entries()) { + data[idx].rename(name); } + return data; } throw new TypeError("Dimensions of columns arg must match data dimensions."); } diff --git a/polars/io.ts b/polars/io.ts index 967e79fb3..1f58199e1 100644 --- a/polars/io.ts +++ b/polars/io.ts @@ -123,11 +123,10 @@ export function readRecords( ): DataFrame { if (options?.schema) { return _DataFrame(pli.fromRows(records, options.schema)); - } else { - return _DataFrame( - pli.fromRows(records, undefined, options?.inferSchemaLength), - ); } + return _DataFrame( + pli.fromRows(records, undefined, options?.inferSchemaLength), + ); } /** @@ -184,12 +183,10 @@ export function readCSV(pathOrBody, options?) { const buf = Buffer.from(pathOrBody, "utf-8"); return _DataFrame(pli.readCsv(buf, options)); - } else { - return _DataFrame(pli.readCsv(pathOrBody, options)); } - } else { - throw new Error("must supply either a path or body"); + return _DataFrame(pli.readCsv(pathOrBody, options)); } + throw new Error("must supply either a path or body"); } export interface ScanCsvOptions { @@ -314,12 +311,10 @@ export function readJSON( const inline = !isPath(pathOrBody, extensions); if (inline) { return _DataFrame(method(Buffer.from(pathOrBody, "utf-8"), options)); - } else { - return _DataFrame(method(pathOrBody, options)); } - } else { - throw new Error("must supply either a path or body"); + return _DataFrame(method(pathOrBody, options)); } + throw new Error("must supply either a path or body"); } interface ScanJsonOptions { inferSchemaLength: number | null; @@ -419,12 +414,10 @@ export function readParquet( return _DataFrame( pli.readParquet(Buffer.from(pathOrBody), pliOptions, parallel), ); - } else { - return _DataFrame(pli.readParquet(pathOrBody, pliOptions, parallel)); } - } else { - throw new Error("must supply either a path or body"); + return _DataFrame(pli.readParquet(pathOrBody, pliOptions, parallel)); } + throw new Error("must supply either a path or body"); } export interface ReadAvroOptions { @@ -455,12 +448,10 @@ export function readAvro(pathOrBody, options = {}) { const inline = !isPath(pathOrBody, [".avro"]); if (inline) { return _DataFrame(pli.readAvro(Buffer.from(pathOrBody), options)); - } else { - return _DataFrame(pli.readAvro(pathOrBody, options)); } - } else { - throw new Error("must supply either a path or body"); + return _DataFrame(pli.readAvro(pathOrBody, options)); } + throw new Error("must supply either a path or body"); } interface RowCount { @@ -525,12 +516,10 @@ export function readIPC(pathOrBody, options = {}) { const inline = !isPath(pathOrBody, [".ipc"]); if (inline) { return _DataFrame(pli.readIpc(Buffer.from(pathOrBody, "utf-8"), options)); - } else { - return _DataFrame(pli.readIpc(pathOrBody, options)); } - } else { - throw new Error("must supply either a path or body"); + return _DataFrame(pli.readIpc(pathOrBody, options)); } + throw new Error("must supply either a path or body"); } export interface ScanIPCOptions { diff --git a/polars/lazy/dataframe.ts b/polars/lazy/dataframe.ts index dd325a3c0..fcdc5a327 100644 --- a/polars/lazy/dataframe.ts +++ b/polars/lazy/dataframe.ts @@ -470,13 +470,14 @@ const prepareGroupbyInputs = (by) => { } return newBy; - } else if (typeof by === "string") { + } + if (typeof by === "string") { return [pli.col(by)]; - } else if (Expr.isExpr(by)) { + } + if (Expr.isExpr(by)) { return [by._expr]; - } else { - return []; } + return []; }; /** @ignore */ @@ -545,9 +546,8 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => { dropNulls(...subset) { if (subset.length) { return wrap("dropNulls", subset.flat(2)); - } else { - return wrap("dropNulls"); } + return wrap("dropNulls"); }, explode(...columns) { if (!columns.length) { @@ -841,9 +841,8 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => { shiftAndFill(opts: any, fillValue?: number | undefined) { if (typeof opts === "number") { return _LazyDataFrame(_ldf.shiftAndFill(opts, fillValue)); - } else { - return _LazyDataFrame(_ldf.shiftAndFill(opts?.n, opts?.fillValue)); } + return _LazyDataFrame(_ldf.shiftAndFill(opts?.n, opts?.fillValue)); }, slice(opt, len?) { if (opt?.offset !== undefined) { @@ -857,12 +856,11 @@ export const _LazyDataFrame = (_ldf: any): LazyDataFrame => { } if (typeof arg === "string") { return wrap("sort", arg, descending, maintain_order, true, false); - } else { - descending = [descending].flat(3) as any; - const by = selectionToExprList(arg, false); - - return wrap("sortByExprs", by, descending, maintain_order, true); } + descending = [descending].flat(3) as any; + const by = selectionToExprList(arg, false); + + return wrap("sortByExprs", by, descending, maintain_order, true); }, std() { return _LazyDataFrame(_ldf.std()); diff --git a/polars/lazy/expr/index.ts b/polars/lazy/expr/index.ts index 03d5718f8..e29f66a8f 100644 --- a/polars/lazy/expr/index.ts +++ b/polars/lazy/expr/index.ts @@ -690,14 +690,13 @@ export const _Expr = (_expr: any): Expr => { return _Expr( _expr.clip(exprToLitOrExpr(arg)._expr, exprToLitOrExpr(max)._expr), ); - } else { - return _Expr( - _expr.clip( - exprToLitOrExpr(arg.min)._expr, - exprToLitOrExpr(arg.max)._expr, - ), - ); } + return _Expr( + _expr.clip( + exprToLitOrExpr(arg.min)._expr, + exprToLitOrExpr(arg.max)._expr, + ), + ); }, count() { return _Expr(_expr.count()); @@ -730,9 +729,8 @@ export const _Expr = (_expr: any): Expr => { diff(n, nullBehavior = "ignore") { if (typeof n === "number") { return _Expr(_expr.diff(n, nullBehavior)); - } else { - return _Expr(_expr.diff(n.n, n.nullBehavior)); } + return _Expr(_expr.diff(n.n, n.nullBehavior)); }, dot(other) { const expr = (exprToLitOrExpr(other, false) as any).inner(); @@ -762,19 +760,17 @@ export const _Expr = (_expr: any): Expr => { bias ?? false, ignoreNulls ?? true, ); - } else { - return wrap( - "ewmMean", - opts.alpha ?? 0.5, - opts.adjust ?? true, - opts.minPeriods ?? 1, - opts.bias ?? false, - opts.ignoreNulls ?? true, - ); } - } else { - return wrap("ewmMean", 0.5, true, 1, false, true); + return wrap( + "ewmMean", + opts.alpha ?? 0.5, + opts.adjust ?? true, + opts.minPeriods ?? 1, + opts.bias ?? false, + opts.ignoreNulls ?? true, + ); } + return wrap("ewmMean", 0.5, true, 1, false, true); }, ewmStd( opts: { @@ -799,19 +795,17 @@ export const _Expr = (_expr: any): Expr => { bias ?? false, ignoreNulls ?? true, ); - } else { - return wrap( - "ewmStd", - opts.alpha ?? 0.5, - opts.adjust ?? true, - opts.minPeriods ?? 1, - opts.bias ?? false, - opts.ignoreNulls ?? true, - ); } - } else { - return wrap("ewmStd", 0.5, true, 1, false, true); + return wrap( + "ewmStd", + opts.alpha ?? 0.5, + opts.adjust ?? true, + opts.minPeriods ?? 1, + opts.bias ?? false, + opts.ignoreNulls ?? true, + ); } + return wrap("ewmStd", 0.5, true, 1, false, true); }, ewmVar( opts: { @@ -836,19 +830,17 @@ export const _Expr = (_expr: any): Expr => { bias ?? false, ignoreNulls ?? true, ); - } else { - return wrap( - "ewmVar", - opts.alpha ?? 0.5, - opts.adjust ?? true, - opts.minPeriods ?? 1, - opts.bias ?? false, - opts.ignoreNulls ?? true, - ); } - } else { - return wrap("ewmVar", 0.5, true, 1, false, true); + return wrap( + "ewmVar", + opts.alpha ?? 0.5, + opts.adjust ?? true, + opts.minPeriods ?? 1, + opts.bias ?? false, + opts.ignoreNulls ?? true, + ); } + return wrap("ewmVar", 0.5, true, 1, false, true); }, exclude(...columns) { return _Expr(_expr.exclude(columns.flat(2))); @@ -1119,9 +1111,8 @@ export const _Expr = (_expr: any): Expr => { } if (typeof frac === "number") { return wrap("sampleFrac", frac, withReplacement, false, seed); - } else { - throw new TypeError("must specify either 'frac' or 'n'"); } + throw new TypeError("must specify either 'frac' or 'n'"); }, shift(periods) { return _Expr(_expr.shift(exprToLitOrExpr(periods)._expr)); @@ -1129,13 +1120,8 @@ export const _Expr = (_expr: any): Expr => { shiftAndFill(optOrPeriods, fillValue?) { if (typeof optOrPeriods === "number") { return wrap("shiftAndFill", optOrPeriods, fillValue); - } else { - return wrap( - "shiftAndFill", - optOrPeriods.periods, - optOrPeriods.fillValue, - ); } + return wrap("shiftAndFill", optOrPeriods.periods, optOrPeriods.fillValue); }, skew(bias) { return wrap("skew", bias?.bias ?? bias ?? true); @@ -1259,11 +1245,12 @@ export const Expr: ExprConstructor = Object.assign(_Expr, { export const exprToLitOrExpr = (expr: any, stringToLit = true): Expr => { if (typeof expr === "string" && !stringToLit) { return _Expr(pli.col(expr)); - } else if (Expr.isExpr(expr)) { + } + if (Expr.isExpr(expr)) { return expr; - } else if (Series.isSeries(expr)) { + } + if (Series.isSeries(expr)) { return _Expr(pli.lit((expr as any)._s)); - } else { - return _Expr(pli.lit(expr)); } + return _Expr(pli.lit(expr)); }; diff --git a/polars/lazy/expr/list.ts b/polars/lazy/expr/list.ts index 3bc13330b..9f4936252 100644 --- a/polars/lazy/expr/list.ts +++ b/polars/lazy/expr/list.ts @@ -50,9 +50,8 @@ export const ExprListFunctions = (_expr: any): ExprList => { get(index: number | Expr) { if (Expr.isExpr(index)) { return wrap("listGet", index._expr); - } else { - return wrap("listGet", pli.lit(index)); } + return wrap("listGet", pli.lit(index)); }, head(n = 5) { return this.slice(0, n); @@ -63,9 +62,8 @@ export const ExprListFunctions = (_expr: any): ExprList => { eval(expr, parallel = true) { if (Expr.isExpr(expr)) { return wrap("listEval", expr._expr, parallel); - } else { - return wrap("listEval", expr, parallel); } + return wrap("listEval", expr, parallel); }, first() { return this.get(0); diff --git a/polars/lazy/expr/string.ts b/polars/lazy/expr/string.ts index f0ba44d85..ea799e434 100644 --- a/polars/lazy/expr/string.ts +++ b/polars/lazy/expr/string.ts @@ -387,7 +387,8 @@ export const ExprStringFunctions = (_expr: any): StringNamespace => { strptime(dtype, format?) { if (dtype.equals(DataType.Date)) { return wrap("strToDate", format, false, false, false); - } else if (dtype.equals(DataType.Datetime("ms"))) { + } + if (dtype.equals(DataType.Datetime("ms"))) { return wrap( "strToDatetime", format, @@ -397,11 +398,10 @@ export const ExprStringFunctions = (_expr: any): StringNamespace => { false, false, ); - } else { - throw new Error( - `only "DataType.Date" and "DataType.Datetime" are supported`, - ); } + throw new Error( + `only "DataType.Date" and "DataType.Datetime" are supported`, + ); }, toLowerCase() { return wrap("strToLowercase"); diff --git a/polars/lazy/functions.ts b/polars/lazy/functions.ts index 1c26c4a0a..9b6318aa4 100644 --- a/polars/lazy/functions.ts +++ b/polars/lazy/functions.ts @@ -104,11 +104,11 @@ export function col(col: string | string[] | Series | DataType): Expr { } if (Array.isArray(col)) { return _Expr(pli.cols(col)); - } else if (typeof col === "object" && Object.values(col)[0] === "DataType") { + } + if (typeof col === "object" && Object.values(col)[0] === "DataType") { return _Expr(pli.dtypeCols([col])); - } else { - return _Expr(pli.col(col)); } + return _Expr(pli.col(col)); } export function cols(col: string | string[]): Expr; @@ -170,18 +170,17 @@ export function intRange( export function intRange(opts: any, high?, step = 1, eager?): Series | Expr { if (typeof opts?.low === "number") { return intRange(opts.low, opts.high, opts.step, opts.eager); - } else { - const low = exprToLitOrExpr(opts, false); - high = exprToLitOrExpr(high, false); - if (eager) { - const df = DataFrame({ a: [1] }); - - return df - .select(intRange(low, high, step).alias("intRange") as any) - .getColumn("intRange") as any; - } - return _Expr(pli.intRange(low, high, step, eager)); } + const low = exprToLitOrExpr(opts, false); + high = exprToLitOrExpr(high, false); + if (eager) { + const df = DataFrame({ a: [1] }); + + return df + .select(intRange(low, high, step).alias("intRange") as any) + .getColumn("intRange") as any; + } + return _Expr(pli.intRange(low, high, step, eager)); } /*** @@ -312,9 +311,8 @@ export function count(column: Series): number; export function count(column) { if (Series.isSeries(column)) { return column.len(); - } else { - return col(column).count(); } + return col(column).count(); } /** Compute the covariance between two columns/ expressions. */ @@ -349,14 +347,12 @@ export function first(column?: string | Series): Expr | T { if (Series.isSeries(column)) { if (column.length) { return column.get(0); - } else { - throw new RangeError( - "The series is empty, so no first value can be returned.", - ); } - } else { - return col(column).first(); + throw new RangeError( + "The series is empty, so no first value can be returned.", + ); } + return col(column).first(); } /** @@ -426,9 +422,8 @@ export function head(column: Series, n?: number): Series; export function head(column: Series | ExprOrString, n?): Series | Expr { if (Series.isSeries(column)) { return column.head(n); - } else { - return exprToLitOrExpr(column, false).head(n); } + return exprToLitOrExpr(column, false).head(n); } /** Get the last value. */ @@ -436,14 +431,12 @@ export function last(column: ExprOrString | Series): any { if (Series.isSeries(column)) { if (column.length) { return column.get(-1); - } else { - throw new RangeError( - "The series is empty, so no last value can be returned.", - ); } - } else { - return exprToLitOrExpr(column, false).last(); + throw new RangeError( + "The series is empty, so no last value can be returned.", + ); } + return exprToLitOrExpr(column, false).last(); } /** Get the mean value. */ @@ -521,9 +514,8 @@ export function tail(column: Series, n?: number): Series; export function tail(column: Series | ExprOrString, n?: number): Series | Expr { if (Series.isSeries(column)) { return column.tail(n); - } else { - return exprToLitOrExpr(column, false).tail(n); } + return exprToLitOrExpr(column, false).tail(n); } /** Syntactic sugar for `pl.col(column).list()` */ diff --git a/polars/series/index.ts b/polars/series/index.ts index e179965ba..b999ca5cc 100644 --- a/polars/series/index.ts +++ b/polars/series/index.ts @@ -1365,9 +1365,8 @@ export function _Series(_s: any): Series { ) ) { throw new InvalidOperationError("isFinite", dtype); - } else { - return wrap("isFinite"); } + return wrap("isFinite"); }, isFirstDistinct() { return wrap("isFirstDistinct"); @@ -1393,9 +1392,8 @@ export function _Series(_s: any): Series { ) ) { throw new InvalidOperationError("isFinite", dtype); - } else { - return wrap("isInfinite"); } + return wrap("isInfinite"); }, isNotNull() { return wrap("isNotNull"); @@ -1525,9 +1523,8 @@ export function _Series(_s: any): Series { ) ) { return wrap("reinterpret", signed); - } else { - throw new InvalidOperationError("reinterpret", dtype); } + throw new InvalidOperationError("reinterpret", dtype); }, rem(field) { return dtypeWrap("Rem", field); @@ -1580,12 +1577,10 @@ export function _Series(_s: any): Series { if (this.isNumeric()) { if (typeof opt === "number") { return wrap("round", opt); - } else { - return wrap("round", opt.decimals); } - } else { - throw new InvalidOperationError("round", this.dtype); + return wrap("round", opt.decimals); } + throw new InvalidOperationError("round", this.dtype); }, clip(...args) { return expr_op("clip", ...args); @@ -1624,9 +1619,8 @@ export function _Series(_s: any): Series { } if (typeof frac === "number") { return wrap("sampleFrac", frac, withReplacement, false, seed); - } else { - throw new TypeError("must specify either 'frac' or 'n'"); } + throw new TypeError("must specify either 'frac' or 'n'"); }, seriesEqual(other, nullEqual: any = true, strict = false) { return _s.seriesEqual(other._s, nullEqual, strict); @@ -1692,9 +1686,8 @@ export function _Series(_s: any): Series { toTypedArray() { if (!this.hasValidity()) { return _s.toTypedArray(); - } else { - throw new Error("data contains nulls, unable to convert to TypedArray"); } + throw new Error("data contains nulls, unable to convert to TypedArray"); }, toFrame() { return _DataFrame(new pli.JsDataFrame([_s])); @@ -1716,9 +1709,8 @@ export function _Series(_s: any): Series { unique(maintainOrder?) { if (maintainOrder) { return wrap("uniqueStable"); - } else { - return wrap("unique"); } + return wrap("unique"); }, valueCounts() { return null as any; @@ -1732,14 +1724,13 @@ export function _Series(_s: any): Series { }; return new Proxy(series, { - get: function (target, prop, receiver) { + get: (target, prop, receiver) => { if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) { return target.get(Number(prop)); - } else { - return Reflect.get(target, prop, receiver); } + return Reflect.get(target, prop, receiver); }, - set: function (series, prop, input): any { + set: (series, prop, input): any => { if (typeof prop !== "symbol" && !Number.isNaN(Number(prop))) { series.setAtIdx([Number(prop)], input); @@ -1806,12 +1797,12 @@ export interface SeriesConstructor extends Deserialize { // fromBinary(binary: Buffer): Series } -const SeriesConstructor = function ( +const SeriesConstructor = ( arg0: any, arg1?: any, dtype?: any, strict?: any, -): Series { +): Series => { if (typeof arg0 === "string") { const _s = arrayToJsSeries(arg0, arg1, dtype, strict); @@ -1831,9 +1822,8 @@ const isSeries = (anyVal: any): anyVal is Series => { const from = (name, values?: ArrayLike): Series => { if (Array.isArray(name)) { return SeriesConstructor("", values); - } else { - return SeriesConstructor(name, values); } + return SeriesConstructor(name, values); }; const of = (...values: any[]): Series => { return Series.from(values); diff --git a/src/series.rs b/src/series.rs index 13cfb7d0c..02e48a158 100644 --- a/src/series.rs +++ b/src/series.rs @@ -244,7 +244,7 @@ impl JsSeries { )); } } - _ => todo!(), + dt => { return Err(napi::Error::from_reason(format!("data type: {:?} is not supported as range", dt))); } }; Ok(s) }