Skip to content

Commit 1160d5a

Browse files
ntjohnson1claude
andauthored
Add docstring examples for Scalar trigonometric functions (#1411)
* Add docstring examples for Scalar trigonometric functions Add example usage to docstrings for Scalar trigonometric functions to improve documentation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Remove weird artifact * Move conftest so it doesn't get packaged in release --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d322b7b commit 1160d5a

File tree

2 files changed

+200
-19
lines changed

2 files changed

+200
-19
lines changed

conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""Pytest configuration for doctest namespace injection."""
19+
20+
import datafusion as dfn
21+
import numpy as np
22+
import pytest
23+
24+
25+
@pytest.fixture(autouse=True)
26+
def _doctest_namespace(doctest_namespace: dict) -> None:
27+
"""Add common imports to the doctest namespace."""
28+
doctest_namespace["dfn"] = dfn
29+
doctest_namespace["np"] = np

python/datafusion/functions.py

Lines changed: 171 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -495,16 +495,28 @@ def abs(arg: Expr) -> Expr:
495495
def acos(arg: Expr) -> Expr:
496496
"""Returns the arc cosine or inverse cosine of a number.
497497
498-
Returns:
499-
--------
500-
Expr
501-
A new expression representing the arc cosine of the input expression.
498+
Examples:
499+
---------
500+
>>> ctx = dfn.SessionContext()
501+
>>> df = ctx.from_pydict({"a": [1.0]})
502+
>>> result = df.select(dfn.functions.acos(dfn.col("a")).alias("acos"))
503+
>>> result.collect_column("acos")[0].as_py()
504+
0.0
502505
"""
503506
return Expr(f.acos(arg.expr))
504507

505508

506509
def acosh(arg: Expr) -> Expr:
507-
"""Returns inverse hyperbolic cosine."""
510+
"""Returns inverse hyperbolic cosine.
511+
512+
Examples:
513+
---------
514+
>>> ctx = dfn.SessionContext()
515+
>>> df = ctx.from_pydict({"a": [1.0]})
516+
>>> result = df.select(dfn.functions.acosh(dfn.col("a")).alias("acosh"))
517+
>>> result.collect_column("acosh")[0].as_py()
518+
0.0
519+
"""
508520
return Expr(f.acosh(arg.expr))
509521

510522

@@ -514,27 +526,73 @@ def ascii(arg: Expr) -> Expr:
514526

515527

516528
def asin(arg: Expr) -> Expr:
517-
"""Returns the arc sine or inverse sine of a number."""
529+
"""Returns the arc sine or inverse sine of a number.
530+
531+
Examples:
532+
---------
533+
>>> ctx = dfn.SessionContext()
534+
>>> df = ctx.from_pydict({"a": [0.0]})
535+
>>> result = df.select(dfn.functions.asin(dfn.col("a")).alias("asin"))
536+
>>> result.collect_column("asin")[0].as_py()
537+
0.0
538+
"""
518539
return Expr(f.asin(arg.expr))
519540

520541

521542
def asinh(arg: Expr) -> Expr:
522-
"""Returns inverse hyperbolic sine."""
543+
"""Returns inverse hyperbolic sine.
544+
545+
Examples:
546+
---------
547+
>>> ctx = dfn.SessionContext()
548+
>>> df = ctx.from_pydict({"a": [0.0]})
549+
>>> result = df.select(dfn.functions.asinh(dfn.col("a")).alias("asinh"))
550+
>>> result.collect_column("asinh")[0].as_py()
551+
0.0
552+
"""
523553
return Expr(f.asinh(arg.expr))
524554

525555

526556
def atan(arg: Expr) -> Expr:
527-
"""Returns inverse tangent of a number."""
557+
"""Returns inverse tangent of a number.
558+
559+
Examples:
560+
---------
561+
>>> ctx = dfn.SessionContext()
562+
>>> df = ctx.from_pydict({"a": [0.0]})
563+
>>> result = df.select(dfn.functions.atan(dfn.col("a")).alias("atan"))
564+
>>> result.collect_column("atan")[0].as_py()
565+
0.0
566+
"""
528567
return Expr(f.atan(arg.expr))
529568

530569

531570
def atanh(arg: Expr) -> Expr:
532-
"""Returns inverse hyperbolic tangent."""
571+
"""Returns inverse hyperbolic tangent.
572+
573+
Examples:
574+
---------
575+
>>> ctx = dfn.SessionContext()
576+
>>> df = ctx.from_pydict({"a": [0.0]})
577+
>>> result = df.select(dfn.functions.atanh(dfn.col("a")).alias("atanh"))
578+
>>> result.collect_column("atanh")[0].as_py()
579+
0.0
580+
"""
533581
return Expr(f.atanh(arg.expr))
534582

535583

536584
def atan2(y: Expr, x: Expr) -> Expr:
537-
"""Returns inverse tangent of a division given in the argument."""
585+
"""Returns inverse tangent of a division given in the argument.
586+
587+
Examples:
588+
---------
589+
>>> ctx = dfn.SessionContext()
590+
>>> df = ctx.from_pydict({"y": [0.0], "x": [1.0]})
591+
>>> result = df.select(
592+
... dfn.functions.atan2(dfn.col("y"), dfn.col("x")).alias("atan2"))
593+
>>> result.collect_column("atan2")[0].as_py()
594+
0.0
595+
"""
538596
return Expr(f.atan2(y.expr, x.expr))
539597

540598

@@ -585,22 +643,65 @@ def coalesce(*args: Expr) -> Expr:
585643

586644

587645
def cos(arg: Expr) -> Expr:
588-
"""Returns the cosine of the argument."""
646+
"""Returns the cosine of the argument.
647+
648+
Examples:
649+
---------
650+
>>> ctx = dfn.SessionContext()
651+
>>> df = ctx.from_pydict({"a": [0,-1,1]})
652+
>>> cos_df = df.select(dfn.functions.cos(dfn.col("a")).alias("cos"))
653+
>>> cos_df.collect_column("cos")[0].as_py()
654+
1.0
655+
"""
589656
return Expr(f.cos(arg.expr))
590657

591658

592659
def cosh(arg: Expr) -> Expr:
593-
"""Returns the hyperbolic cosine of the argument."""
660+
"""Returns the hyperbolic cosine of the argument.
661+
662+
Examples:
663+
---------
664+
>>> ctx = dfn.SessionContext()
665+
>>> df = ctx.from_pydict({"a": [0,-1,1]})
666+
>>> cosh_df = df.select(dfn.functions.cosh(dfn.col("a")).alias("cosh"))
667+
>>> cosh_df.collect_column("cosh")[0].as_py()
668+
1.0
669+
"""
594670
return Expr(f.cosh(arg.expr))
595671

596672

597673
def cot(arg: Expr) -> Expr:
598-
"""Returns the cotangent of the argument."""
674+
"""Returns the cotangent of the argument.
675+
676+
Examples:
677+
---------
678+
>>> from math import pi
679+
>>> ctx = dfn.SessionContext()
680+
>>> df = ctx.from_pydict({"a": [pi / 4]})
681+
>>> import builtins
682+
>>> result = df.select(
683+
... dfn.functions.cot(dfn.col("a")).alias("cot")
684+
... )
685+
>>> builtins.round(
686+
... result.collect_column("cot")[0].as_py(), 1
687+
... )
688+
1.0
689+
"""
599690
return Expr(f.cot(arg.expr))
600691

601692

602693
def degrees(arg: Expr) -> Expr:
603-
"""Converts the argument from radians to degrees."""
694+
"""Converts the argument from radians to degrees.
695+
696+
Examples:
697+
---------
698+
>>> from math import pi
699+
>>> ctx = dfn.SessionContext()
700+
>>> df = ctx.from_pydict({"a": [0,pi,2*pi]})
701+
>>> deg_df = df.select(dfn.functions.degrees(dfn.col("a")).alias("deg"))
702+
>>> deg_df.collect_column("deg")[2].as_py()
703+
360.0
704+
"""
604705
return Expr(f.degrees(arg.expr))
605706

606707

@@ -778,7 +879,22 @@ def pow(base: Expr, exponent: Expr) -> Expr:
778879

779880

780881
def radians(arg: Expr) -> Expr:
781-
"""Converts the argument from degrees to radians."""
882+
"""Converts the argument from degrees to radians.
883+
884+
Examples:
885+
---------
886+
>>> from math import pi
887+
>>> ctx = dfn.SessionContext()
888+
>>> df = ctx.from_pydict({"a": [180.0]})
889+
>>> import builtins
890+
>>> result = df.select(
891+
... dfn.functions.radians(dfn.col("a")).alias("rad")
892+
... )
893+
>>> builtins.round(
894+
... result.collect_column("rad")[0].as_py(), 6
895+
... )
896+
3.141593
897+
"""
782898
return Expr(f.radians(arg.expr))
783899

784900

@@ -939,12 +1055,30 @@ def signum(arg: Expr) -> Expr:
9391055

9401056

9411057
def sin(arg: Expr) -> Expr:
942-
"""Returns the sine of the argument."""
1058+
"""Returns the sine of the argument.
1059+
1060+
Examples:
1061+
---------
1062+
>>> ctx = dfn.SessionContext()
1063+
>>> df = ctx.from_pydict({"a": [0.0]})
1064+
>>> result = df.select(dfn.functions.sin(dfn.col("a")).alias("sin"))
1065+
>>> result.collect_column("sin")[0].as_py()
1066+
0.0
1067+
"""
9431068
return Expr(f.sin(arg.expr))
9441069

9451070

9461071
def sinh(arg: Expr) -> Expr:
947-
"""Returns the hyperbolic sine of the argument."""
1072+
"""Returns the hyperbolic sine of the argument.
1073+
1074+
Examples:
1075+
---------
1076+
>>> ctx = dfn.SessionContext()
1077+
>>> df = ctx.from_pydict({"a": [0.0]})
1078+
>>> result = df.select(dfn.functions.sinh(dfn.col("a")).alias("sinh"))
1079+
>>> result.collect_column("sinh")[0].as_py()
1080+
0.0
1081+
"""
9481082
return Expr(f.sinh(arg.expr))
9491083

9501084

@@ -992,12 +1126,30 @@ def substring(string: Expr, position: Expr, length: Expr) -> Expr:
9921126

9931127

9941128
def tan(arg: Expr) -> Expr:
995-
"""Returns the tangent of the argument."""
1129+
"""Returns the tangent of the argument.
1130+
1131+
Examples:
1132+
---------
1133+
>>> ctx = dfn.SessionContext()
1134+
>>> df = ctx.from_pydict({"a": [0.0]})
1135+
>>> result = df.select(dfn.functions.tan(dfn.col("a")).alias("tan"))
1136+
>>> result.collect_column("tan")[0].as_py()
1137+
0.0
1138+
"""
9961139
return Expr(f.tan(arg.expr))
9971140

9981141

9991142
def tanh(arg: Expr) -> Expr:
1000-
"""Returns the hyperbolic tangent of the argument."""
1143+
"""Returns the hyperbolic tangent of the argument.
1144+
1145+
Examples:
1146+
---------
1147+
>>> ctx = dfn.SessionContext()
1148+
>>> df = ctx.from_pydict({"a": [0.0]})
1149+
>>> result = df.select(dfn.functions.tanh(dfn.col("a")).alias("tanh"))
1150+
>>> result.collect_column("tanh")[0].as_py()
1151+
0.0
1152+
"""
10011153
return Expr(f.tanh(arg.expr))
10021154

10031155

0 commit comments

Comments
 (0)