Conditional

All functions · nseries package

If

func (s Series) If(cond any, yes any, no any) Series

If takes a function that acts on each element of a series and returns a boolean that is used to determine whether the “yes” or “no” values passed will be used in the corresponding positions in the returned series. The condition function can take either a single float64 (the series value at each position in the series) or a float64 and an int, which will be the series value and its position in the series. The “yes” and “no” values can be either a scalar, another series, or a function. If a function is passed as the “yes” or “no” value, it should accept either a single float64 (the series value) or a float64 and an int (the series value and its position in the series). A series passed as the “yes” or “no” value is right-aligned with the receiver at the most recent bar, each branch on its own: its last value is used at the receiver’s last position, its second-last at the second-last, and so on. A branch shorter than the receiver gives zero at the earlier positions it does not reach (where it is selected), and a longer branch contributes only its trailing len(s) values. For example, with a condition that is always true, Series{1, 2, 3}.If(cond, Series{8}, 0) gives [0, 0, 8]. Series are paired by position only; see “Time alignment” in the manual. An unsupported cond returns a zero-filled series of the same length; unsupported yes/no values yield zero at the positions where they are selected. Defined function types with these exact underlying signatures are accepted. Scalar branches accept built-in and defined real numeric types, including uintptr. Each operand is normalised once before iteration. A typed-nil function value of any of these forms is treated as unsupported and is not invoked. Only the selected branch (yes or no) is evaluated at each position.