66from pandas import DataFrame , Series
77from pipda import register_func , register_verb
88
9- from ..core .contexts import Context
10- from ..core .types import NumericOrIter , NumericType , is_not_null , is_scalar
11- from ..core .utils import Array , register_numpy_func_x , recycle_value , length_of
129from ..core .collections import Collection
10+ from ..core .contexts import Context
11+ from ..core .types import (
12+ FloatOrIter ,
13+ NumericOrIter ,
14+ NumericType ,
15+ is_not_null ,
16+ is_scalar
17+ )
18+ from ..core .utils import Array , length_of , recycle_value , register_numpy_func_x
1319
1420# cor?, range, summary, iqr
1521
@@ -50,6 +56,20 @@ def _arithmetric(x: Iterable, na_rm: bool = False) -> Iterable:
5056 """ ,
5157)
5258
59+ prod = _register_arithmetic_agg (
60+ "prod" ,
61+ "prod" ,
62+ doc = """Product of the input.
63+
64+ Args:
65+ x: The input
66+ na_rm: Exclude the NAs
67+
68+ Returns:
69+ The product of the input
70+ """ ,
71+ )
72+
5373mean = _register_arithmetic_agg (
5474 "mean" ,
5575 "mean" ,
@@ -161,6 +181,7 @@ def pmax(*x: Iterable, na_rm: bool = False) -> Iterable[float]:
161181@register_func (None , context = Context .EVAL )
162182def round (x : NumericOrIter , ndigits : int = 0 ) -> NumericOrIter :
163183 """Rounding a number"""
184+ # recycle ndigits?
164185 return numpy .round (x , ndigits )
165186
166187
@@ -190,6 +211,33 @@ def round(x: NumericOrIter, ndigits: int = 0) -> NumericOrIter:
190211 """ ,
191212)
192213
214+ sign = register_numpy_func_x (
215+ "sign" ,
216+ "sign" ,
217+ doc = """Get the signs of the corresponding elements of x
218+
219+ Args:
220+ x: The input
221+
222+ Returns:
223+ The signs of the corresponding elements of x
224+ """ ,
225+ )
226+
227+ trunc = register_numpy_func_x (
228+ "trunc" ,
229+ "trunc" ,
230+ doc = """Get the integers truncated for each element in x
231+
232+ Args:
233+ x: The input
234+
235+ Returns:
236+ The ingeters of elements in x being truncated
237+ Note the dtype is still float.
238+ """ ,
239+ )
240+
193241ceiling = register_numpy_func_x (
194242 "ceiling" ,
195243 "ceil" ,
@@ -216,6 +264,27 @@ def round(x: NumericOrIter, ndigits: int = 0) -> NumericOrIter:
216264 """ ,
217265)
218266
267+ @register_func (None , context = Context .EVAL )
268+ def signif (
269+ x : NumericOrIter ,
270+ digits : int = 6
271+ ) -> NumericOrIter :
272+ """Rounds the values in its first argument to the specified number of
273+ significant digits
274+
275+ Args:
276+ x: A numeric vector or scalar
277+ digits: integer indicating the number of significant digits to be used
278+
279+ Returns:
280+ The rounded values for each element in x
281+ """
282+ # todo complex?
283+ return numpy .fromiter (
284+ (round (elem , digits - int (ceiling (log10 (abs (elem ))))) for elem in x ),
285+ dtype = float
286+ )
287+
219288# pylint: disable=unused-argument
220289@register_verb (DataFrame , context = Context .EVAL )
221290def cov (x : DataFrame , y : Iterable = None , ddof : int = 1 ) -> DataFrame :
@@ -501,3 +570,75 @@ def row_medians(
501570 The medians by row.
502571 """
503572 return x .agg (median , axis = 1 , na_rm = na_rm )
573+
574+ @register_func (None , context = Context .EVAL )
575+ def log (x : NumericOrIter , base : float = numpy .e ) -> FloatOrIter :
576+ """Computes logarithms, by default natural logarithm
577+
578+ Args:
579+ x: A numeric scalar or vector
580+ base: The base of the logarithm
581+
582+ Returns:
583+ The value of the logarithm if x is scalar, otherwise element-wise
584+ logarithm of elements in x
585+ """
586+ if base == numpy .e :
587+ return numpy .log (x )
588+
589+ return numpy .log (x ) / numpy .log (base )
590+
591+ exp = register_numpy_func_x (
592+ "exp" ,
593+ "exp" ,
594+ doc = """Calculates the power of natural number
595+
596+ Args:
597+ x: A numeric scalar or vector
598+
599+ Returns:
600+ Power of natural number of element-wise power of natural number for x
601+ """
602+ )
603+
604+ log2 = register_numpy_func_x (
605+ "log2" ,
606+ "log2" ,
607+ doc = """Computes logarithms with base 2
608+
609+ Args:
610+ x: A numeric scalar or vector
611+
612+ Returns:
613+ The value of log2 if x is scalar, otherwise element-wise
614+ log2 of elements in x
615+ """
616+ )
617+
618+ log10 = register_numpy_func_x (
619+ "log10" ,
620+ "log10" ,
621+ doc = """Computes logarithms with base 10
622+
623+ Args:
624+ x: A numeric scalar or vector
625+
626+ Returns:
627+ The value of log10 if x is scalar, otherwise element-wise
628+ log10 of elements in x
629+ """
630+ )
631+
632+ log1p = register_numpy_func_x (
633+ "log1p" ,
634+ "log1p" ,
635+ doc = """Computes log(1+x)
636+
637+ Args:
638+ x: A numeric scalar or vector
639+
640+ Returns:
641+ The value of log(1+x) if x is scalar, otherwise element-wise
642+ log(1+x) of elements in x
643+ """
644+ )
0 commit comments