From d1ae96f6d2948c991d4324659ac1bbde31f48153 Mon Sep 17 00:00:00 2001 From: Darek Date: Mon, 22 Apr 2024 09:49:09 -0400 Subject: [PATCH] Adding arc functions (#200) --- __tests__/expr.test.ts | 18 ++++++ polars/lazy/expr/index.ts | 127 +++++++++++++++++++++++++++++++++++++- 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/__tests__/expr.test.ts b/__tests__/expr.test.ts index cd108264c..35c62c7aa 100644 --- a/__tests__/expr.test.ts +++ b/__tests__/expr.test.ts @@ -44,6 +44,24 @@ describe("expr", () => { const actual = df().filter(col("bools").eq(false).and(col("int").eq(3))); expect(actual.height).toStrictEqual(1); }); + test("arccos", () => { + const df = pl.DataFrame({ a: [1, 2] }); + const expected = pl.DataFrame({ arccos: [0.0, Number.NaN] }); + const actual = df.select(col("a").arccos().as("arccos")); + expect(actual).toFrameEqual(expected); + }); + test("arcsin", () => { + const df = pl.DataFrame({ a: [1, 2] }); + const expected = pl.DataFrame({ arcsin: [1.570796, Number.NaN] }); + const actual = df.select(col("a").arcsin().round(6).as("arcsin")); + expect(actual).toFrameEqual(expected); + }); + test("arcyan", () => { + const df = pl.DataFrame({ a: [1, 2] }); + const expected = pl.DataFrame({ arctan: [0.785398, 1.107149] }); + const actual = df.select(col("a").arctan().round(6).as("arctan")); + expect(actual).toFrameEqual(expected); + }); test("argMax", () => { const actual = df().select(col("int").argMax()).row(0)[0]; expect(actual).toStrictEqual(2); diff --git a/polars/lazy/expr/index.ts b/polars/lazy/expr/index.ts index 14b55f79c..e57e5fd14 100644 --- a/polars/lazy/expr/index.ts +++ b/polars/lazy/expr/index.ts @@ -143,6 +143,114 @@ export interface Expr */ alias(name: string): Expr; and(other: any): Expr; + /** + * Compute the element-wise value for the inverse cosine. + * @returns Expression of data type :class:`Float64`. + * @example + * ``` + >>> const df = pl.DataFrame({"a": [0.0]}) + >>> df.select(pl.col("a").acrcos()) + shape: (1, 1) + ┌──────────┐ + │ a │ + │ --- │ + │ f64 │ + ╞══════════╡ + │ 1.570796 │ + └──────────┘ + * ``` + */ + arccos(): Expr; + /** + * Compute the element-wise value for the inverse hyperbolic cosine. + * @returns Expression of data type :class:`Float64`. + * @example + * ``` + >>> const df = pl.DataFrame({"a": [1.0]}) + >>> df.select(pl.col("a").acrcosh()) + shape: (1, 1) + ┌─────┐ + │ a │ + │ --- │ + │ f64 │ + ╞═════╡ + │ 0.0 │ + └─────┘ + * ``` + */ + arccosh(): Expr; + /** + * Compute the element-wise value for the inverse sine. + * @returns Expression of data type :class:`Float64`. + * @example + * ``` + >>> const df = pl.DataFrame({"a": [1.0]}) + >>> df.select(pl.col("a").acrsin()) + shape: (1, 1) + ┌──────────┐ + │ a │ + │ --- │ + │ f64 │ + ╞══════════╡ + │ 1.570796 │ + └──────────┘ + * ``` + */ + arcsin(): Expr; + /** + * Compute the element-wise value for the inverse hyperbolic sine. + * @returns Expression of data type :class:`Float64`. + * @example + * ``` + >>> const df = pl.DataFrame({"a": [1.0]}) + >>> df.select(pl.col("a").acrsinh()) + shape: (1, 1) + ┌──────────┐ + │ a │ + │ --- │ + │ f64 │ + ╞══════════╡ + │ 0.881374 │ + └──────────┘ + * ``` + */ + arcsinh(): Expr; + /** + * Compute the element-wise value for the inverse tangent. + * @returns Expression of data type :class:`Float64`. + * @example + * ``` + >>> const df = pl.DataFrame({"a": [1.0]}) + >>> df.select(pl.col("a").arctan()) + shape: (1, 1) + ┌──────────┐ + │ a │ + │ --- │ + │ f64 │ + ╞══════════╡ + │ 0.785398 │ + └──────────┘ + * ``` + */ + arctan(): Expr; + /** + * Compute the element-wise value for the inverse hyperbolic tangent. + * @returns Expression of data type :class:`Float64`. + * @example + * ``` + >>> const df = pl.DataFrame({"a": [1.0]}) + >>> df.select(pl.col("a").arctanh()) + shape: (1, 1) + ┌─────┐ + │ a │ + │ --- │ + │ f64 │ + ╞═════╡ + │ inf │ + └─────┘ + * ``` + */ + arctanh(): Expr; /** Get the index of the maximal value. */ argMax(): Expr; /** Get the index of the minimal value. */ @@ -881,9 +989,26 @@ export const _Expr = (_expr: any): Expr => { }, and(other) { const expr = (exprToLitOrExpr(other, false) as any).inner(); - return _Expr(_expr.and(expr)); }, + arccos() { + return _Expr(_expr.arccos()); + }, + arccosh() { + return _Expr(_expr.arccosh()); + }, + arcsin() { + return _Expr(_expr.arcsin()); + }, + arcsinh() { + return _Expr(_expr.arcsinh()); + }, + arctan() { + return _Expr(_expr.arctan()); + }, + arctanh() { + return _Expr(_expr.arctanh()); + }, argMax() { return _Expr(_expr.argMax()); },