Series#

A Series is a lazily evaluated expression over the model’s periods. Make one with boki.Model.series(), boki.Model.input() or boki.Model.unknown(); the operations below build new series and check units while the model is being written, and values exist only in a render.

class boki.Series#

Lazily evaluated expression over the model’s periods, with a unit.

A series is a node in an expression graph, not a list of numbers. Arithmetic, lag, where and the other operations build new nodes and check units while the model is being written; values exist only after Model.render, and you read them with r[x] or r.value(x, period). A scalar series has one value for the whole model (an input, a total such as x.sum()); every other series has one value per period. Subclasses such as Named, Input, Unknown and LineItem add how the values are found; all of them support the operations below.

Build a series with Model.series, Model.input or Model.unknown, from the statement lines of an entity, or from the shape functions such as boki.by_year. Do not construct Series directly.

A missing value is None. Arithmetic and comparison give a missing result when an operand is missing, the reductions (sum, mean, min, max) skip missing values, and cumsum counts them as 0.

A series has no truth value while the model is being written, because it holds no numbers yet. if x > 0:, and, or and not raise TypeError; branch per period with where, combine conditions with &, | and ~, and read values with x[t] inside an @m.series function. A series cannot be iterated either. Because == builds a series, test whether two names refer to the same series with is.

Attributes:
namestr or None

Name the series is registered under, or None for an unnamed expression.

unitfrozenset or None

Dimensions of the values, as a set of (base unit name, exponent) pairs; the empty set is a dimensionless number. None means the unit is unknown and unit checks are skipped.

See also

boki.Model.series

Create a named series.

boki.Model.input

Create a named assumption.

Notes

See the series guide for the forms of m.series and the conventions for how years, periods and signs are counted.

Examples

Build a series from other series and read its values from a render:

>>> import boki as b
>>> m = b.Model("Sales", start="2027-01", periods=3, step=b.month)
>>> units = m.series("Units", data={0: 10, 1: 12, 2: 9})
>>> revenue = m.series("Revenue", units * 30)
>>> r = m.render()
>>> r[revenue]
[300, 360, 270]
>>> r[revenue.sum()]
930
__abs__()#

Return the absolute value of this series, period by period.

Returns:
Series

The absolute value, in this series’ unit. A missing value stays missing.

See also

boki.Series.__neg__

Negate a series.

Examples

>>> import boki as b
>>> m = b.Model("Gap", start="2027-01", periods=2, step=b.month)
>>> gap = m.series("Gap", data={0: -5, 1: 8})
>>> r = m.render()
>>> r[abs(gap)]
[5, 8]
__add__(other)#

Add another series, quantity, or number to this series, period by period.

Parameters:
otherSeries, Quantity, or number

The addend. A plain number is a dimensionless constant, except a literal 0, which takes any unit. A Quantity such as 100 * USD is a constant with its unit.

Returns:
Series

The sum in each period. It is scalar only when both operands are scalar, and has the operands’ common unit.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD, or USD and JPY. A literal 0 and a series of unknown unit never conflict.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__radd__

Add when this series is the right operand.

boki.Series.__sub__

Subtract instead of add.

Notes

A missing value (None) in either operand makes the sum missing in that period.

When this series is a date series (m.date, m.period_end), the sum shifts each date later. A time in months, or a plain number read as months, moves it by that many months and clamps the day to the end of a shorter month, as Excel’s EDATE does. A time in days (14 * b.day) moves it by days. The result is a date series.

Examples

Add a one-off bonus to a base amount with units; a plain number would be a unit error:

>>> import boki as b
>>> from boki.units import USD
>>> m = b.Model("Add", start="2027-01", periods=2, step=b.month)
>>> base = m.series("Base", 100 * USD)
>>> bonus = m.series("Bonus", data={1: 20 * USD})
>>> r = m.render()
>>> r[base + bonus]
[100.0, 120.0]
>>> base + 5
Traceback (most recent call last):
    ...
boki.units.UnitError: cannot add Base (USD) and 5 dimensionless

Shift period-start dates by two months:

>>> r[m.date + 2 * b.month]
[datetime.date(2027, 3, 1), datetime.date(2027, 4, 1)]
__and__(other)#

Combine two conditions with logical and, period by period.

Parameters:
otherSeries or bool

The other condition: a series, usually a comparison, or a bool.

Returns:
Series

A dimensionless series that is true where both conditions are true. A missing value in either operand makes the result missing.

Raises:
TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__or__

Logical or.

boki.Series.__invert__

Logical not.

Notes

Python’s and cannot be used on a series; it raises TypeError. & binds tighter than a comparison, so write each comparison in parentheses: (a > 0) & (b > 0).

Examples

>>> import boki as b
>>> m = b.Model("And", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[(sales > 95) & (sales < 110)]
[True, False, False]
__eq__(other)#

Build the condition that this series equals another value, period by period.

Parameters:
otherSeries, Quantity, or number

The value to compare with: a series, a number, a Quantity such as 100 * USD, or a date. Its unit must match this series’ unit; a literal 0 and an unknown unit never conflict.

Returns:
Series

A dimensionless series that is true in the periods where the values are equal. It is scalar only when both operands are scalar. A missing value in either operand makes the result missing.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__ne__

Not equal.

Notes

== builds a series; it does not tell whether two names refer to the same series. Use is for that. A series is hashed by identity, so it works as a dictionary key. The equation form Model.solve(u, u == expression) also uses it.

Examples

>>> import boki as b
>>> m = b.Model("Equal", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 100})
>>> r = m.render()
>>> r[sales == 100]
[True, False, True]
__floordiv__(other)#

Divide this series by another and round down to a whole number, period by period.

Parameters:
otherSeries, Quantity, or number

The divisor. A plain number is dimensionless, and a Quantity is a constant with its unit.

Returns:
Series

The quotient rounded toward negative infinity. It is scalar only when both operands are scalar. Its unit is the quotient of the operands’ units, and unknown if either unit is unknown.

Raises:
UnitError

At construction, if the units combine months and days, which do not convert at a fixed rate.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ZeroDivisionError

At evaluation, if the divisor is 0 in a period.

See also

boki.Series.__mod__

The remainder.

boki.floor

Round a series down to a whole number.

Notes

Excel writes it as INT(x/n).

Examples

The model year (0 for the first twelve months) of a monthly period index:

>>> import boki as b
>>> m = b.Model("Years", start="2027-01", periods=24, step=b.month)
>>> r = m.render()
>>> [r.value(m.t // 12, period) for period in ("2027-12", "2028-01")]
[0, 1]
__ge__(other)#

Compare this series with another value: greater than or equal, period by period.

Parameters:
otherSeries, Quantity, or number

The value to compare with: a series, a number, a Quantity such as 100 * USD, or a date. Its unit must match this series’ unit; a literal 0 and an unknown unit never conflict.

Returns:
Series

A dimensionless series that is true in the periods where this series is at least other. It is scalar only when both operands are scalar. A missing value in either operand makes the result missing.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__le__

Less than or equal.

boki.Model.check

State a condition that must hold in every period.

Notes

The result is a series, not a Python bool; see Series.__lt__.

Examples

A covenant that cash stays at or above a floor, read period by period:

>>> import boki as b
>>> from boki.units import USD
>>> m = b.Model("Floor", start="2027-01", periods=3, step=b.month)
>>> cash = m.series("Cash", data={0: 50 * USD, 1: 10 * USD, 2: 30 * USD})
>>> r = m.render()
>>> r[cash >= 20 * USD]
[True, False, True]
__getitem__(t)#

Return the value at one period inside a render, or a scalar series for that period outside one.

Parameters:
tint, str, date, or Series

The period. An integer is a period index from 0, and a negative integer counts from the last period. A string is a period label ("2028", "2027-04") or a date ("2027-04-15", "2027/04") inside the period. A datetime.date is read the same way. A scalar series, such as an input, supplies the key when the model is rendered.

Returns:
Series or float

Outside a render (while the model is being written), a scalar series with this series’ unit that holds the value of the period. Inside a render (in an @m.series function), the value itself, a plain number in base units. Reading a scalar series gives its one value.

Raises:
IndexError

At evaluation, if the period is outside the model’s timeline.

TypeError

At evaluation, if the key is a bool.

See also

boki.Series.last

Value in the last period, or in the last period of a model year.

boki.Result.value

Value of any series in one period, read from a render.

Examples

Pick periods by index, label, or negative index while writing the model:

>>> import boki as b
>>> m = b.Model("Pick", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> peak = sales[1]
>>> r = m.render()
>>> r[peak], r[sales["2027-03"]], r[sales[-1]]
(120, 90, 90)

Inside an @m.series function the same call returns numbers, so ordinary Python if works:

>>> @m.series
... def bonus(t):
...     return 10 if sales[t] > 100 else 0
>>> m.render()[bonus]
[0, 10, 0]
__gt__(other)#

Compare this series with another value: greater than, period by period.

Parameters:
otherSeries, Quantity, or number

The value to compare with: a series, a number, a Quantity such as 100 * USD, or a date. Its unit must match this series’ unit; a literal 0 and an unknown unit never conflict.

Returns:
Series

A dimensionless series that is true in the periods where this series is greater than other. It is scalar only when both operands are scalar. A missing value in either operand makes the result missing.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__lt__

Less than.

Notes

The result is a series, not a Python bool; see Series.__lt__. Dates compare with dates: m.date > date(2027, 6, 1) is true from the first period that starts after that day.

Examples

Switch a cost on once sales pass a threshold:

>>> import boki as b
>>> m = b.Model("Compare", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[sales > 100]
[False, True, False]
__invert__()#

Negate a condition with logical not, period by period.

Returns:
Series

A dimensionless series that is true where this series is false. A missing value stays missing.

See also

boki.Series.__and__

Logical and.

boki.Series.__or__

Logical or.

Notes

Python’s not cannot be used on a series; it raises TypeError. Write ~(a > 0).

Examples

>>> import boki as b
>>> m = b.Model("Not", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[~(sales > 95)]
[False, False, True]
__le__(other)#

Compare this series with another value: less than or equal, period by period.

Parameters:
otherSeries, Quantity, or number

The value to compare with: a series, a number, a Quantity such as 100 * USD, or a date. Its unit must match this series’ unit; a literal 0 and an unknown unit never conflict.

Returns:
Series

A dimensionless series that is true in the periods where this series is at most other. It is scalar only when both operands are scalar. A missing value in either operand makes the result missing.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__ge__

Greater than or equal.

Notes

The result is a series, not a Python bool; see Series.__lt__.

Examples

>>> import boki as b
>>> m = b.Model("Compare", start="2027-01", periods=3, step=b.month)
>>> cash = m.series("Cash", data={0: 50, 1: 0, 2: 30})
>>> r = m.render()
>>> r[cash <= 0]
[False, True, False]
__lt__(other)#

Compare this series with another value: less than, period by period.

Parameters:
otherSeries, Quantity, or number

The value to compare with: a series, a number, a Quantity such as 100 * USD, or a date. Its unit must match this series’ unit; a literal 0 and an unknown unit never conflict.

Returns:
Series

A dimensionless series that is true in the periods where this series is less than other. It is scalar only when both operands are scalar. A missing value in either operand makes the result missing.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.where

Choose values with a condition.

boki.Model.check

State a condition that must hold in every period.

Notes

The result is a series, not a Python bool. Use it in Series.where, combine it with &, | and ~, or pass it to Model.check. Writing 100 < x calls x > 100.

Examples

>>> import boki as b
>>> m = b.Model("Compare", start="2027-01", periods=3, step=b.month)
>>> cash = m.series("Cash", data={0: 50, 1: -20, 2: 30})
>>> r = m.render()
>>> r[cash < 0]
[False, True, False]
__mod__(other)#

Return the remainder of dividing this series by another, period by period.

Parameters:
otherSeries, Quantity, or number

The divisor. Its unit must match this series’ unit (a literal 0 and an unknown unit never conflict). The remainder has the sign of the divisor.

Returns:
Series

The remainder in each period, in the operands’ common unit. It is scalar only when both operands are scalar.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ZeroDivisionError

At evaluation, if the divisor is 0 in a period.

See also

boki.mod

The same operation as a function.

boki.Series.__floordiv__

The whole number of times the divisor fits.

Notes

Python’s % and Excel’s MOD agree: the result takes the sign of the divisor.

Examples

A cycle of three positions from the period index:

>>> import boki as b
>>> m = b.Model("Cycle", start="2027-01", periods=4, step=b.month)
>>> r = m.render()
>>> r[m.t % 3]
[0, 1, 2, 0]
__mul__(other)#

Multiply this series by another series, quantity, or number, period by period.

Parameters:
otherSeries, Quantity, or number

The factor. A plain number is dimensionless, and a Quantity such as 3000 * JPY is a constant with its unit.

Returns:
Series

The product in each period. It is scalar only when both operands are scalar. Its unit is the product of the operands’ units (JPY times a plain number is JPY; JPY per month times months is JPY); it is unknown if either unit is unknown.

Raises:
UnitError

At construction, if the units combine months and days, which do not convert at a fixed rate.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__truediv__

Divide instead of multiply.

Notes

A missing value (None) in either operand makes the product missing in that period.

A series in a rate per time is not integrated by the period length automatically. Multiply it by m.period_length (a series of months in each period) to get the amount in each period.

Examples

Price times volume in a monthly model; the unit of the revenue is the price’s currency:

>>> import boki as b
>>> from boki.units import JPY
>>> m = b.Model("Revenue", start="2027-01", periods=2, step=b.month)
>>> price = m.series("Price", 3000 * JPY)
>>> volume = m.series("Volume", data={0: 10, 1: 12})
>>> revenue = price * volume
>>> r = m.render()
>>> r[revenue]
[30000.0, 36000.0]
>>> revenue.unit == JPY.dims
True

A fee per month becomes an amount per period with m.period_length, so a quarterly model bills three months at once:

>>> m = b.Model("Fees", start="2027-01", periods=2, step=3 * b.month)
>>> fee = m.series("Fee", 100 * JPY / b.month)
>>> r = m.render()
>>> r[fee * m.period_length]
[300.0, 300.0]
__ne__(other)#

Build the condition that this series differs from another value, period by period.

Parameters:
otherSeries, Quantity, or number

The value to compare with: a series, a number, a Quantity such as 100 * USD, or a date. Its unit must match this series’ unit; a literal 0 and an unknown unit never conflict.

Returns:
Series

A dimensionless series that is true in the periods where the values differ. It is scalar only when both operands are scalar. A missing value in either operand makes the result missing.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__eq__

Equal.

Notes

The result is a series, not a Python bool; see Series.__eq__.

Examples

>>> import boki as b
>>> m = b.Model("Differ", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 100})
>>> r = m.render()
>>> r[sales != 100]
[False, True, False]
__neg__()#

Negate this series, period by period.

Returns:
Series

The negated series, in this series’ unit. A missing value stays missing.

See also

boki.Series.__abs__

Absolute value.

Examples

>>> import boki as b
>>> m = b.Model("Negate", start="2027-01", periods=2, step=b.month)
>>> cost = m.series("Cost", data={0: 60, 1: 70})
>>> r = m.render()
>>> r[-cost]
[-60, -70]
__or__(other)#

Combine two conditions with logical or, period by period.

Parameters:
otherSeries or bool

The other condition: a series, usually a comparison, or a bool.

Returns:
Series

A dimensionless series that is true where either condition is true. A missing value in either operand makes the result missing.

Raises:
TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__and__

Logical and.

boki.Series.__invert__

Logical not.

Notes

Python’s or cannot be used on a series; it raises TypeError. | binds tighter than a comparison, so write each comparison in parentheses: (a > 0) | (b > 0).

Examples

>>> import boki as b
>>> m = b.Model("Or", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[(sales > 110) | (sales < 95)]
[False, True, True]
__pow__(other)#

Raise this series to a power, period by period.

Parameters:
otherSeries, Quantity, or number

The exponent. A literal whole number keeps a series with a unit valid (x ** 2 is square USD); any other exponent needs a dimensionless base.

Returns:
Series

The power in each period. It is scalar only when both operands are scalar. With a literal whole-number exponent, the unit is the base’s unit raised to it; otherwise the result is dimensionless.

Raises:
UnitError

At construction, if a series with a unit is raised to anything but a literal whole number.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ValueError

At evaluation, if a negative base is raised to a non-integer power, which has no real value.

ZeroDivisionError

At evaluation, if 0 is raised to a negative power.

See also

boki.Series.__rpow__

Raise a number to this series.

Notes

A missing value (None) in either operand makes the result missing in that period.

Examples

Compound a monthly growth rate of 1% over the periods; the exponent is a per-period count:

>>> import boki as b
>>> m = b.Model("Growth", start="2027-01", periods=3, step=b.month)
>>> growth = m.series("Growth", 1 + 0.01)
>>> r = m.render()
>>> r[growth ** m.t]
[1.0, 1.01, 1.0201]
__radd__(other)#

Add this series to a number or quantity on the left, period by period.

Parameters:
otherSeries, Quantity, or number

The left operand: a number or a Quantity such as 100 * USD. A plain number is a dimensionless constant, except a literal 0, which takes any unit.

Returns:
Series

The sum in each period, with the operands’ common unit.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD, or USD and JPY. A literal 0 and a series of unknown unit never conflict.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__add__

Add when this series is the left operand.

Examples

Python calls this for number + series:

>>> import boki as b
>>> m = b.Model("Add", start="2027-01", periods=2, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120})
>>> r = m.render()
>>> r[10 + sales]
[110, 130]
__rand__(other)#

Combine a condition on the left with this series using logical and.

Parameters:
otherSeries or bool

The left condition: a bool or a series.

Returns:
Series

A dimensionless series that is true where both conditions are true.

Raises:
TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__and__

Logical and when this series is the left operand.

Examples

Python calls this for bool & series:

>>> import boki as b
>>> m = b.Model("And", start="2027-01", periods=2, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120})
>>> r = m.render()
>>> r[True & (sales > 110)]
[False, True]
__rfloordiv__(other)#

Divide a number or quantity on the left by this series and round down to a whole number.

Parameters:
otherSeries, Quantity, or number

The dividend: a number or a Quantity.

Returns:
Series

The quotient rounded toward negative infinity. Its unit is the quotient of the operands’ units.

Raises:
UnitError

At construction, if the units combine months and days, which do not convert at a fixed rate.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ZeroDivisionError

At evaluation, if this series is 0 in a period.

See also

boki.Series.__floordiv__

Round down when this series is the dividend.

Examples

Python calls this for number // series; how many whole lots of each size fit in 100:

>>> import boki as b
>>> m = b.Model("Lots", start="2027-01", periods=2, step=b.month)
>>> size = m.series("Size", data={0: 30, 1: 40})
>>> r = m.render()
>>> r[100 // size]
[3, 2]
__rmod__(other)#

Return the remainder of dividing a number or quantity on the left by this series.

Parameters:
otherSeries, Quantity, or number

The dividend: a number or a Quantity. Its unit must match this series’ unit.

Returns:
Series

The remainder in each period, with the sign of this series.

Raises:
UnitError

At construction, if the operands have different units.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ZeroDivisionError

At evaluation, if this series is 0 in a period.

See also

boki.Series.__mod__

Remainder when this series is the dividend.

Examples

Python calls this for number % series:

>>> import boki as b
>>> m = b.Model("Cycle", start="2027-01", periods=3, step=b.month)
>>> size = m.series("Size", data={0: 4, 1: 5, 2: 6})
>>> r = m.render()
>>> r[17 % size]
[1, 2, 5]
__rmul__(other)#

Multiply a number or quantity on the left by this series, period by period.

Parameters:
otherSeries, Quantity, or number

The left factor: a number or a Quantity such as 3000 * JPY.

Returns:
Series

The product in each period. Its unit is the product of the operands’ units, and unknown if either unit is unknown.

Raises:
UnitError

At construction, if the units combine months and days, which do not convert at a fixed rate.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__mul__

Multiply when this series is the left operand.

Examples

Python calls this for number * series:

>>> import boki as b
>>> m = b.Model("Scale", start="2027-01", periods=2, step=b.month)
>>> units = m.series("Units", data={0: 10, 1: 12})
>>> r = m.render()
>>> r[3 * units]
[30, 36]
__ror__(other)#

Combine a condition on the left with this series using logical or.

Parameters:
otherSeries or bool

The left condition: a bool or a series.

Returns:
Series

A dimensionless series that is true where either condition is true.

Raises:
TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__or__

Logical or when this series is the left operand.

Examples

Python calls this for bool | series:

>>> import boki as b
>>> m = b.Model("Or", start="2027-01", periods=2, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120})
>>> r = m.render()
>>> r[False | (sales > 110)]
[False, True]
__rpow__(other)#

Raise a number on the left to the power of this series, period by period.

Parameters:
othernumber

The base: a plain number.

Returns:
Series

The power in each period; a dimensionless series.

Raises:
TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ValueError

At evaluation, if a negative base is raised to a non-integer power.

ZeroDivisionError

At evaluation, if 0 is raised to a negative power.

See also

boki.Series.__pow__

Raise this series to a power.

Examples

Python calls this for number ** series; a doubling every period:

>>> import boki as b
>>> m = b.Model("Doubling", start="2027-01", periods=3, step=b.month)
>>> r = m.render()
>>> r[2 ** m.t]
[1, 2, 4]
__rsub__(other)#

Subtract this series from a number or quantity on the left, period by period.

Parameters:
otherSeries, Quantity, or number

The minuend: a number or a Quantity such as 100 * USD. A plain number is a dimensionless constant, except a literal 0, which takes any unit.

Returns:
Series

The difference in each period, with the operands’ common unit.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD, or USD and JPY. A literal 0 and a series of unknown unit never conflict.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__sub__

Subtract when this series is the left operand.

Examples

Python calls this for number - series:

>>> import boki as b
>>> m = b.Model("Left", start="2027-01", periods=2, step=b.month)
>>> used = m.series("Used", data={0: 30, 1: 45})
>>> r = m.render()
>>> r[100 - used]
[70, 55]
__rtruediv__(other)#

Divide a number or quantity on the left by this series, period by period.

Parameters:
otherSeries, Quantity, or number

The dividend: a number or a Quantity such as 100 * USD.

Returns:
Series

The quotient in each period. Its unit is the quotient of the operands’ units, and unknown if either unit is unknown.

Raises:
UnitError

At construction, if the units combine months and days, which do not convert at a fixed rate.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ZeroDivisionError

At evaluation, if this series is 0 in a period.

See also

boki.Series.__truediv__

Divide when this series is the left operand.

Examples

Python calls this for number / series; units per customer, for example:

>>> import boki as b
>>> m = b.Model("Per unit", start="2027-01", periods=2, step=b.month)
>>> units = m.series("Units", data={0: 10, 1: 20})
>>> r = m.render()
>>> r[100 / units]
[10.0, 5.0]
__sub__(other)#

Subtract another series, quantity, or number from this series, period by period.

Parameters:
otherSeries, Quantity, or number

The subtrahend. A plain number is a dimensionless constant, except a literal 0, which takes any unit. A Quantity such as 100 * USD is a constant with its unit.

Returns:
Series

The difference in each period. It is scalar only when both operands are scalar, and has the operands’ common unit, except that a date minus a date is a number of days.

Raises:
UnitError

At construction, if the operands have different units, such as a plain number and a series in USD, or USD and JPY. A literal 0 and a series of unknown unit never conflict.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

See also

boki.Series.__rsub__

Subtract when this series is the right operand.

boki.Series.__add__

Add instead of subtract.

Notes

A missing value (None) in either operand makes the difference missing in that period.

When this series is a date series, the difference moves each date earlier: by a time in months (or a plain number read as months, clamping the day as Excel’s EDATE does), or by a time in days. A date series minus another date series is the number of days between them, in the unit b.day.

Examples

Subtract a cost from revenue:

>>> import boki as b
>>> m = b.Model("Margin", start="2027-01", periods=2, step=b.month)
>>> revenue = m.series("Revenue", data={0: 100, 1: 120})
>>> cost = m.series("Cost", data={0: 60, 1: 70})
>>> r = m.render()
>>> r[revenue - cost]
[40, 50]

The days in each period are the period end minus its start; the last day is the end minus one day:

>>> r[m.period_end - m.date]
[31, 28]
>>> r[m.period_end - 1 * b.day]
[datetime.date(2027, 1, 31), datetime.date(2027, 2, 28)]
__truediv__(other)#

Divide this series by another series, quantity, or number, period by period.

Parameters:
otherSeries, Quantity, or number

The divisor. A plain number is dimensionless, and a Quantity such as 100 * USD is a constant with its unit.

Returns:
Series

The quotient in each period. It is scalar only when both operands are scalar. Its unit is the quotient of the operands’ units, so two series in the same unit give a dimensionless ratio; it is unknown if either unit is unknown.

Raises:
UnitError

At construction, if the units combine months and days, which do not convert at a fixed rate.

TypeError

At construction, if other has a type a series expression cannot use, such as a list or a dict.

ZeroDivisionError

At evaluation, if the divisor is 0 in a period.

See also

boki.Series.__floordiv__

Divide and round down.

boki.Series.__mul__

Multiply instead of divide.

Notes

A missing value (None) in either operand makes the quotient missing in that period. Dividing by a series that is 0 raises; guard it with Series.where on the divisor.

Examples

A margin is profit divided by revenue; both are in the same currency, so the ratio is dimensionless:

>>> import boki as b
>>> from boki.units import USD
>>> m = b.Model("Ratio", start="2027-01", periods=2, step=b.month)
>>> revenue = m.series("Revenue", data={0: 100 * USD, 1: 200 * USD})
>>> profit = m.series("Profit", data={0: 20 * USD, 1: 50 * USD})
>>> margin = profit / revenue
>>> r = m.render()
>>> r[margin]
[0.2, 0.25]
property change#

Change from the previous period.

Returns:
Series

This period’s value minus the previous period’s, with 0 as the value before the first period. For a ledger balance this is the movement of the period.

See also

boki.Series.opening

The previous period’s value.

Examples

>>> import boki as b
>>> m = b.Model("Change", start="2027-01", periods=3, step=b.month)
>>> headcount = m.series("Headcount", data={0: 10, 1: 12, 2: 11})
>>> r = m.render()
>>> r[headcount.change]
[10, 2, -1]
cumsum()#

Return the running total up to and including each period.

Returns:
Series

A series in this series’ unit whose value in period t is the sum of this series from the first period to t. A missing value counts as 0. A scalar series adds its value in every period.

See also

boki.Series.reverse_cumsum

Total from each period to the last.

boki.Series.sum

One total over all periods.

boki.Series.lag

The value in an earlier period.

Examples

Accumulate monthly sales:

>>> import boki as b
>>> m = b.Model("Running", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[sales.cumsum()]
[100, 220, 310]
lag(k=1, fill=None)#

Return the value k periods earlier.

The result in period t is this series’ value in period t - k. Periods whose source falls outside the model take fill.

Parameters:
kint or Series, default=1

Number of periods to look back; a negative value looks forward. A series gives an offset for each period (a fraction is truncated toward zero). It is a count of periods, not of months, so a quarterly model looks back one quarter with k=1.

fillscalar, Quantity, or Series, optional

Value for a period whose source is outside the model. A number or Quantity is used as given (a Quantity contributes its base-unit value, and its unit is not checked); a series is read in the same period. The default None leaves such periods missing.

Returns:
Series

A series with this series’ unit and a value in every period, also when this series is scalar.

Raises:
TypeError

At construction, if fill is a list, tuple, or dict. For history before the first period, give a series such as b.by_year({...}).

See also

boki.Series.opening

The previous period’s value, 0 before the first period.

boki.Series.cumsum

Running total up to and including this period.

Examples

Look back one month, with and without a fill value, and forward with a negative k:

>>> import boki as b
>>> m = b.Model("Lag", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[sales.lag(1)]
[None, 100, 120]
>>> r[sales.lag(1, fill=0)]
[0, 100, 120]
>>> r[sales.lag(-1, fill=0)]
[120, 90, 0]
last(year=None)#

Return the value in the last period, or in the last period of one model year.

Use it for a year-end balance or a headcount at the end of the model.

Parameters:
yearint, optional

Model year, counted from 0 for the first twelve months of the model. The default None reads the last period of the model.

Returns:
Series

A scalar series in this series’ unit.

Raises:
TypeError

At construction, if year is not an int.

ValueError

At evaluation, if year is outside the model’s years.

See also

boki.Series.__getitem__

Value in any one period.

Examples

Read a year-end value from a monthly model with two model years:

>>> import boki as b
>>> m = b.Model("Ends", start="2027-01", periods=24, step=b.month)
>>> r = m.render()
>>> r[m.t.last()], r[m.t.last(year=0)]
(23, 11)
max(year=None)#

Return the largest value over all periods, or over one model year.

Parameters:
yearint, optional

Model year to search, counted from 0 for the first twelve months of the model. The default None searches every period.

Returns:
Series

A scalar series in this series’ unit. Missing values are skipped.

Raises:
TypeError

At construction, if year is not an int.

ValueError

At evaluation, if year is outside the model’s years, or if year is not given and no period has a value. With year, a model year without values gives a missing result.

See also

boki.max

Larger of several series in each period.

boki.Series.min

Smallest value over the periods.

Examples

>>> import boki as b
>>> m = b.Model("High", start="2027-01", periods=3, step=b.month)
>>> cash = m.series("Cash", data={0: 50, 1: -20, 2: 30})
>>> r = m.render()
>>> r[cash.max()], r[cash.max(year=0)]
(50, 50)
mean(year=None)#

Return the average over the periods that have a value, or over one model year.

Parameters:
yearint, optional

Model year to average, counted from 0 for the first twelve months of the model. The default None averages every period.

Returns:
Series

A scalar series in this series’ unit. Missing values are skipped, so x.where(cond).mean() averages the periods where cond holds.

Raises:
TypeError

At construction, if year is not an int.

ValueError

At evaluation, if year is outside the model’s years.

ZeroDivisionError

At evaluation, if year is not given and no period has a value. With year, a model year without values gives a missing result.

See also

boki.Series.sum

Total instead of average.

Examples

>>> import boki as b
>>> m = b.Model("Average", start="2027-01", periods=24, step=b.month)
>>> r = m.render()
>>> r[m.t.mean()], r[m.t.mean(year=1)]
(11.5, 17.5)
min(year=None)#

Return the smallest value over all periods, or over one model year.

Parameters:
yearint, optional

Model year to search, counted from 0 for the first twelve months of the model. The default None searches every period.

Returns:
Series

A scalar series in this series’ unit. Missing values are skipped.

Raises:
TypeError

At construction, if year is not an int.

ValueError

At evaluation, if year is outside the model’s years, or if year is not given and no period has a value. With year, a model year without values gives a missing result.

See also

boki.min

Smaller of several series in each period.

boki.Series.max

Largest value over the periods.

Examples

>>> import boki as b
>>> m = b.Model("Low", start="2027-01", periods=3, step=b.month)
>>> cash = m.series("Cash", data={0: 50, 1: -20, 2: 30})
>>> r = m.render()
>>> r[cash.min()], r[cash.min(year=0)]
(-20, -20)
property month#

Calendar month, 1 to 12, of each date in a date series.

Returns:
Series

A dimensionless series containing the calendar month of each date. Excel writes it as MONTH.

Raises:
TypeError

At construction, if this series does not have date units.

See also

boki.Series.year

Calendar year of each date.

boki.Model.date

The model’s date at the start of each period.

Examples

Read the calendar month of each monthly period, for example to apply a seasonal factor:

>>> import boki as b
>>> m = b.Model("Season", start="2027-11", periods=4, step=b.month)
>>> month = m.date.month
>>> r = m.render()
>>> r[month]
[11, 12, 1, 2]
property opening#

The previous period’s value, 0 before the first period.

Returns:
Series

For a flow this is the value one period back; read on a balance it is the opening balance. For a LineItem that holds a balance, the first period shows the opening balance declared for it.

See also

boki.Series.lag

The value k periods back, with a chosen fill.

boki.Series.change

This period’s value minus the opening.

Examples

>>> import boki as b
>>> m = b.Model("Opening", start="2027-01", periods=3, step=b.month)
>>> headcount = m.series("Headcount", data={0: 10, 1: 12, 2: 11})
>>> r = m.render()
>>> r[headcount.opening]
[0, 10, 12]
reverse_cumsum()#

Return the running total from each period to the last period.

Returns:
Series

A series in this series’ unit whose value in period t is the sum of this series from t to the last period, that is, what is still ahead. A missing value counts as 0.

See also

boki.Series.cumsum

Total from the first period to each period.

Examples

Remaining sales after each month:

>>> import boki as b
>>> m = b.Model("Ahead", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[sales.reverse_cumsum()]
[310, 210, 90]
spread(kernel)#

Spread each period’s value over later periods with a kernel.

The result in period t is the sum over earlier or equal periods s of kernel.weights[t - s] times this series’ value in s. Use it for payment delays, prepayments recognized over time, and ramps. The period is the unit of the weights, whatever its length in months.

Parameters:
kernelKernel

Weights by periods elapsed, from boki.delay, boki.even, boki.curve or boki.mix. A period beyond the weights receives nothing, except that a boki.curve keeps applying its last weight.

Returns:
Series

A series in this series’ unit. A missing value counts as 0.

See also

boki.Series.lag

Shift the whole series instead of spreading it.

Examples

Collect sales one month after they are made, or spread each month’s sales evenly over two months:

>>> import boki as b
>>> m = b.Model("Collections", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[sales.spread(b.delay(1))]
[0.0, 100.0, 120.0]
>>> r[sales.spread(b.even(2))]
[50.0, 110.0, 105.0]
sum(year=None)#

Return the total over all periods, or over one model year.

Parameters:
yearint, optional

Model year to total, counted from 0 for the first twelve months of the model, as boki.by_year counts. The default None totals every period. A model year spans twelve months whatever the period length.

Returns:
Series

A scalar series in this series’ unit. Missing values count as 0. The total of a scalar series over a model year is the value times the number of periods in that year.

Raises:
TypeError

At construction, if year is not an int.

ValueError

At evaluation, if year is outside the model’s years.

See also

boki.Series.cumsum

Running total in every period instead of one number.

boki.Series.mean

Average instead of total.

Examples

Total a monthly fee over the model and over each model year:

>>> import boki as b
>>> m = b.Model("Fees", start="2027-01", periods=24, step=b.month)
>>> fee = m.series("Fee", 10)
>>> r = m.render()
>>> r[fee.sum()], r[fee.sum(year=0)], r[fee.sum(year=1)]
(240, 120, 120)
where(cond, else_=None)#

Return this series in the periods where a condition holds, and another value elsewhere.

Python’s if cannot branch per period while the model is being written; this is the branch. Both branches are built, and each period takes one of them.

Parameters:
condSeries

Condition, usually a comparison such as x > 0 or a combination with &, | and ~. A period where it is true takes this series; a false or missing condition takes else_.

else_scalar, Quantity, or Series, optional

Value in the other periods. The default None leaves them missing (None), which the reductions skip, so x.where(c).mean() averages only where c holds.

Returns:
Series

A series that is scalar only when cond, this series and else_ are all scalar. Its unit is the common unit of this series and else_.

Raises:
UnitError

At construction, if this series and else_ have different units. A literal 0, None and a series of unknown unit never conflict.

See also

boki.Series.__and__

Combine two conditions.

boki.max

Larger of several series in each period.

Examples

Keep sales above a threshold, leaving the other months missing or setting them to 0:

>>> import boki as b
>>> m = b.Model("Where", start="2027-01", periods=3, step=b.month)
>>> sales = m.series("Sales", data={0: 100, 1: 120, 2: 90})
>>> r = m.render()
>>> r[sales.where(sales > 100)]
[None, 120, None]
>>> r[sales.where(sales > 100, 0)]
[0, 120, 0]
>>> r[sales.where(sales > 100).mean()]
120.0
property year#

Calendar year of each date in a date series.

This property extracts the calendar year from each date. It does not group periods into twelve-month model years; use Series.sum, Series.mean, Series.min, Series.max, or Series.last with year= for those reductions.

Returns:
Series

A dimensionless series containing the calendar year for each date.

Raises:
TypeError

At construction, if this series does not have date units.

See also

boki.Model.date

The model’s date at the start of each period.

Examples

Read calendar years from monthly period-start dates:

>>> import boki as b
>>> m = b.Model("Calendar", start="2027-01", periods=24, step=b.month)
>>> year = m.date.year
>>> r = m.render()
>>> [r.value(year, period) for period in ("2027-01", "2028-01")]
[2027, 2028]

Named series#

class boki.series.Named(name, expr=None, unit=None)#

Named series that can be declared first and defined later.

Model.series returns a Named for m.series("x", expr), and for m.series("x") followed by Named.define. Declare first and define later when two series refer to each other: Python runs top to bottom, so the name must exist before the expression that uses it. A Named supports everything a Series does, and tables and the Excel output label its values with its name. Create one with Model.series, not directly.

Parameters:
namestr | None

Name the series is registered under, or None for an unnamed series.

exprSeries | None

The expression, a series or anything that becomes one (a number, a Quantity). Leave it out, the default, to declare the series and call Named.define later.

unitDims | None

Dimensions the series must have, as a frozenset of (base unit name, exponent) pairs such as JPY.dims; Model.series takes the unit itself (unit=JPY). When given, the expression must have these dimensions. The default takes the expression’s.

See also

boki.Model.series

Create a named series, with an expression or declared for later.

boki.model.Input

A named assumption with a default, scenarios and choices.

Notes

A series that was declared but never defined raises ValueError when it is evaluated. See the series guide for forward declarations.

Examples

Declare a funding need, use it, then define it once its parts exist:

>>> import boki as b
>>> m = b.Model("Funding", start="2027-01", periods=2, step=b.month)
>>> need = m.series("Need")
>>> share = m.series("Share", need * 0.5)
>>> _ = need.define(200)
>>> m.render()[share]
[100.0, 100.0]
define(expr)#

Set the expression of a series that was declared without one.

Parameters:
exprSeries, Quantity, or number

The expression, in the series’ unit. If the series was declared with a unit (unit= in Model.series), the expression must have that unit; a literal 0 fits any unit. Otherwise the series takes the expression’s unit.

Returns:
Named

This series, so a definition can be chained.

Raises:
ValueError

At construction, if the series is already defined. At evaluation, if it is read while still undefined.

UnitError

At construction, if the declared unit and the expression’s unit differ.

TypeError

At construction, if expr has a type a series expression cannot use, such as a list.

See also

boki.Model.series

Create a series, declared or with its expression.

Examples

Declare two series that refer to each other, then close the loop with a lag:

>>> import boki as b
>>> m = b.Model("Loop", start="2027-01", periods=3, step=b.month)
>>> cash = m.series("Cash")
>>> interest = m.series("Interest", cash.lag(1, fill=1000) * 0.01)
>>> _ = cash.define(cash.lag(1, fill=1000) + interest)
>>> r = m.render()
>>> [round(v, 2) for v in r[cash]]
[1010.0, 1020.1, 1030.3]

Inputs and unknowns are series too: see boki.model.Input and boki.model.Unknown. The line items of an entity are described on the Line item page.

Functions of series#

These take series, numbers or quantities and return series. They work period by period; for a reduction over the periods use boki.Series.sum(), boki.Series.min(), boki.Series.max() and boki.Series.mean().

boki.max(*xs)#

Return the largest of several series, numbers or quantities in each period.

This compares the arguments period by period. To find the largest value of one series across the periods, use Series.max.

Parameters:
*xsSeries, Quantity, or number

The values to compare. A plain number is a dimensionless constant, except a literal 0, which takes any unit. All arguments must have the same unit.

Returns:
Series

In each period, the largest argument. A missing value in any argument makes the result missing. Its unit is the arguments’ common unit.

Raises:
UnitError

At construction, if the arguments have different units.

TypeError

At construction, if an argument has a type a series expression cannot use, such as a list.

See also

boki.min

Smallest of several series in each period.

boki.Series.max

Largest value of one series across the periods.

Examples

Floor a taxable profit at zero in each month:

>>> import boki as b
>>> from boki.units import USD
>>> m = b.Model("Floor", start="2027-01", periods=3, step=b.month)
>>> profit = m.series("Profit", data={0: 50 * USD, 1: -20 * USD, 2: 30 * USD})
>>> r = m.render()
>>> r[b.max(profit, 0 * USD)]
[50.0, 0.0, 30.0]
boki.min(*xs)#

Return the smallest of several series, numbers or quantities in each period.

This compares the arguments period by period. To find the smallest value of one series across the periods, use Series.min.

Parameters:
*xsSeries, Quantity, or number

The values to compare. A plain number is a dimensionless constant, except a literal 0, which takes any unit. All arguments must have the same unit.

Returns:
Series

In each period, the smallest argument. A missing value in any argument makes the result missing. Its unit is the arguments’ common unit.

Raises:
UnitError

At construction, if the arguments have different units.

TypeError

At construction, if an argument has a type a series expression cannot use, such as a list.

See also

boki.max

Largest of several series in each period.

boki.Series.min

Smallest value of one series across the periods.

Examples

Cap a repayment at the balance outstanding in each month:

>>> import boki as b
>>> m = b.Model("Cap", start="2027-01", periods=3, step=b.month)
>>> due = m.series("Due", data={0: 100, 1: 40, 2: 70})
>>> balance = m.series("Balance", data={0: 60, 1: 90, 2: 70})
>>> r = m.render()
>>> r[b.min(due, balance)]
[60, 40, 70]
boki.round(x, digits=0)#

Round half away from zero, as Excel’s ROUND does.

Python’s built-in round rounds halves to the nearest even number and works on the binary value, so round(2.675, 2) is 2.67. This function rounds the decimal the value prints as: 2.675 becomes 2.68.

Parameters:
xSeries, Quantity, or number

The value to round.

digitsint or Series, default=0

Number of decimal places. A negative number rounds to tens, hundreds, and so on (-2 rounds to the nearest hundred). A series gives a number of places for each period.

Returns:
Series

The rounded value in each period, in the unit of x. A missing value stays missing.

Raises:
TypeError

At construction, if x has a type a series expression cannot use, such as a list.

See also

boki.rounddown

Round toward zero.

boki.roundup

Round away from zero.

boki.floor

Round down to a whole number.

Examples

Halves round away from zero, and decimals round as printed:

>>> import boki as b
>>> m = b.Model("Round", start="2027-01", periods=3, step=b.month)
>>> price = m.series("Price", data={0: 2.5, 1: -2.5, 2: 2.675})
>>> r = m.render()
>>> r[b.round(price)]
[3.0, -3.0, 3.0]
>>> r[b.round(price, 2)]
[2.5, -2.5, 2.68]
boki.rounddown(x, digits=0)#

Round toward zero, as Excel’s ROUNDDOWN does.

Parameters:
xSeries, Quantity, or number

The value to round.

digitsint or Series, default=0

Number of decimal places. A negative number rounds to tens, hundreds, and so on. A series gives a number of places for each period.

Returns:
Series

The value with the digits beyond digits dropped, in the unit of x. A missing value stays missing.

Raises:
TypeError

At construction, if x has a type a series expression cannot use, such as a list.

See also

boki.round

Round half away from zero.

boki.roundup

Round away from zero.

boki.floor

Round down toward negative infinity.

Examples

Drop the digits beyond one decimal and beyond the hundreds:

>>> import boki as b
>>> m = b.Model("Down", start="2027-01", periods=2, step=b.month)
>>> price = m.series("Price", data={0: 2.57, 1: -2.57})
>>> r = m.render()
>>> r[b.rounddown(price, 1)]
[2.5, -2.5]
>>> r[b.rounddown(price * 1000, -2)]
[2500.0, -2500.0]
boki.roundup(x, digits=0)#

Round away from zero, as Excel’s ROUNDUP does.

Parameters:
xSeries, Quantity, or number

The value to round.

digitsint or Series, default=0

Number of decimal places. A negative number rounds to tens, hundreds, and so on. A series gives a number of places for each period.

Returns:
Series

The value rounded up in magnitude to digits places, in the unit of x. A missing value stays missing.

Raises:
TypeError

At construction, if x has a type a series expression cannot use, such as a list.

See also

boki.round

Round half away from zero.

boki.rounddown

Round toward zero.

boki.ceil

Round up toward positive infinity.

Examples

Round a price up to the next tenth and a quantity up to the next hundred:

>>> import boki as b
>>> m = b.Model("Up", start="2027-01", periods=2, step=b.month)
>>> price = m.series("Price", data={0: 2.51, 1: -2.51})
>>> r = m.render()
>>> r[b.roundup(price, 1)]
[2.6, -2.6]
>>> r[b.roundup(price * 1000, -2)]
[2600.0, -2600.0]
boki.ceil(x)#

Round up to the smallest whole number at or above x.

Parameters:
xSeries, Quantity, or number

The value to round.

Returns:
Series

The smallest whole number not below x, in the unit of x. A missing value stays missing. Excel writes it as -INT(-x).

Raises:
TypeError

At construction, if x has a type a series expression cannot use, such as a list.

See also

boki.floor

Round down to a whole number.

boki.roundup

Round away from zero to a number of decimals.

Examples

Whole buses needed for a passenger count:

>>> import boki as b
>>> m = b.Model("Buses", start="2027-01", periods=2, step=b.month)
>>> passengers = m.series("Passengers", data={0: 130, 1: 100})
>>> r = m.render()
>>> r[b.ceil(passengers / 50)]
[3, 2]
boki.floor(x)#

Round down to the largest whole number at or below x, as Excel’s INT does.

Parameters:
xSeries, Quantity, or number

The value to round.

Returns:
Series

The largest whole number not above x, in the unit of x. A negative number rounds away from zero (-2.1 becomes -3). A missing value stays missing.

Raises:
TypeError

At construction, if x has a type a series expression cannot use, such as a list.

See also

boki.ceil

Round up to a whole number.

boki.rounddown

Round toward zero to a number of decimals.

Examples

Whole units a budget buys:

>>> import boki as b
>>> m = b.Model("Units", start="2027-01", periods=2, step=b.month)
>>> budget = m.series("Budget", data={0: 130, 1: 100})
>>> r = m.render()
>>> r[b.floor(budget / 50)]
[2, 2]
boki.mod(x, n)#

Return the remainder of x divided by n, with the sign of n, as Excel’s MOD does.

Parameters:
xSeries, Quantity, or number

The dividend.

nSeries, Quantity, or number

The divisor. Its unit must match the unit of x; a literal 0 and an unknown unit never conflict.

Returns:
Series

The remainder in each period, in the common unit. A missing value in either argument makes the result missing.

Raises:
UnitError

At construction, if x and n have different units.

TypeError

At construction, if an argument has a type a series expression cannot use, such as a list.

ZeroDivisionError

At evaluation, if n is 0 in a period.

See also

boki.Series.__mod__

The % operator, which does the same.

boki.Series.__floordiv__

The whole number of times the divisor fits.

Examples

A remainder takes the sign of the divisor, as Python’s % does:

>>> import boki as b
>>> m = b.Model("Mod", start="2027-01", periods=2, step=b.month)
>>> x = m.series("X", data={0: 7, 1: -7})
>>> r = m.render()
>>> r[b.mod(x, 3)]
[1, 2]
boki.exp(x)#

Return e raised to the power of x, as Excel’s EXP does.

Parameters:
xSeries or number

A plain number: a series that is dimensionless or of unknown unit.

Returns:
Series

The exponential in each period; a dimensionless series. A missing value stays missing.

Raises:
UnitError

At construction, if x has a unit.

TypeError

At construction, if x has a type a series expression cannot use, such as a list.

OverflowError

At evaluation, if the result is too large to represent (x above about 709).

See also

boki.log

Natural logarithm.

boki.Series.__pow__

Raise a series to a power.

Examples

Continuous compounding at 5% over the period index, in a monthly model:

>>> import boki as b
>>> m = b.Model("Exp", start="2027-01", periods=3, step=b.month)
>>> r = m.render()
>>> [round(v, 4) for v in r[b.exp(0.05 * m.t)]]
[1.0, 1.0513, 1.1052]
boki.log(x)#

Return the natural logarithm of x, as Excel’s LN does.

Parameters:
xSeries or number

A plain number: a series that is dimensionless or of unknown unit.

Returns:
Series

The natural logarithm in each period; a dimensionless series. A missing value stays missing.

Raises:
UnitError

At construction, if x has a unit.

TypeError

At construction, if x has a type a series expression cannot use, such as a list.

ValueError

At evaluation, if x is zero or negative in a period.

See also

boki.exp

The exponential.

Examples

The continuous growth rate between two index values:

>>> import boki as b
>>> m = b.Model("Log", start="2027-01", periods=2, step=b.month)
>>> index = m.series("Index", data={0: 1, 1: 2})
>>> r = m.render()
>>> [round(v, 4) for v in r[b.log(index)]]
[0.0, 0.6931]
boki.sqrt(x)#

Return the square root of x, as Excel’s SQRT does.

Parameters:
xSeries or number

A plain number: a series that is dimensionless or of unknown unit.

Returns:
Series

The square root in each period; a dimensionless series. A missing value stays missing.

Raises:
UnitError

At construction, if x has a unit.

TypeError

At construction, if x has a type a series expression cannot use, such as a list.

ValueError

At evaluation, if x is negative in a period.

See also

boki.Series.__pow__

Raise a series to a power, such as x ** 0.5.

Examples

>>> import boki as b
>>> m = b.Model("Root", start="2027-01", periods=2, step=b.month)
>>> variance = m.series("Variance", data={0: 4, 1: 9})
>>> r = m.render()
>>> r[b.sqrt(variance)]
[2.0, 3.0]