TSHISTORY FORMULA
Purpose
This tshistory component provides a formula language to build computed series.
Formulas are defined using a simple lisp-like syntax, using a pre-defined function library.
Formulas are read-only series (you can't update or replace
values).
They also have an history, which is built, time stamps wise, using the union of all constituent time stamps, and value wise, by applying the formula.
Because of this the staircase operator is available on formulae.
Some staircase operations can have a very fast implementation if the
formula obeys commutativity rules.
Formula
General Syntax
Formulas are expressed in a lisp-like syntax using operators,
positional (mandatory) parameters and keyword (optional) parameters.
The general form is:
(<operator> <param1> ... <paramN> #:<keyword1> <value1> ... #:<keywordN> <valueN>)
Here are a couple examples:
(add (series "wallonie") (series "bruxelles") (series "flandres"))
Here we see the two fundamental add and series operators at work.
This would form a new synthetic series out of three base series (which can be either raw series or formulas themselves).
Some notes:
-
operator names can contain dashes or arbitrary caracters
-
literal values can be:
3(integer),5.2(float),"hello"(string) and#tor#f(true ot false)
Pre-defined operators
*
Performs a scalar product on a series.
Example: (* -1 (series "positive-things"))
+
Add a constant quantity to a series.
Example: (+ 42 (series "i-feel-undervalued"))
/
Perform a scalar division between numbers or a series and a scalar.
Example: (/ (series "div-me") (/ 3 2))
add
Linear combination of two or more series. Takes a variable number of series as input.
Example: (add (series "wallonie") (series "bruxelles") (series "flandres"))
To specify the behaviour of the add operation in the face of missing
data, the series can be built with the fill keyword. This option is
only really applied when several series are combined. By default, if
an input series has missing values for a given time stamp, the
resulting series has no value for this timestamp (unless a fill rule
is provided).
clip
Set an upper/lower threashold for a series. Takes a series as
positional parameter and accepts two optional keywords min and max
which must be numbers (integers or floats).
Example: (clip (series "must-be-positive") #:min 0)
date
Produces an utc timestamp from its input string date in iso format.
The tz keyword allows to specify an alternate time zone.
The naive keyword forces production of a naive timestamp.
Both tz and naive keywords are mutually exlcusive.
div
Element wise division of two series.
Example: (div (series "$-to-€") (series "€-to-£"))
min
Computes the row-wise minimum of its input series.
Example: (min (series "station0") (series "station1") (series "station2"))
max
Computes the row-wise maximum of its input series.
Example: (max (series "station0") (series "station1") (series "station2"))
mul
Element wise multiplication of series. Takes a variable number of series as input.
Example: (mul (series "banana-spot-price ($)") (series "$-to-€" #:fill 'ffill'))
This might convert a series priced in dollars to a series priced in euros, using a currency exchange rate series with a forward-fill option.
naive
Allow demoting a series from a tz-aware index (strongly recommended) to a tz-naive index (unfortunately sometimes unavoidable for interop with other tz-naive series).
One must provide a country code and a target timezone.
Example: (naive (series "tz-aware-series-from-poland") "PL" "Europe/Warsaw")
priority
The priority operator combines its input series as layers. For each timestamp in the union of all series time stamps, the value comes from the first series that provides a value.
Example: (priority (series "realized") (series "nominated") (series "forecasted"))
Here realized values show up first, and any missing values come from
nominated first and then only from forecasted.
resample
Resamples its input series using freq and the aggregation method
method (as described in the pandas documentation).
Example: (resample (series "hourly") "D")
row-mean
This operator computes the row-wise mean of its input series using the
series weight option if present. The missing points are handled as
if the whole series were absent.
Example: (row-mean (series "station0") (series "station1" #:weight 2) (series "station2"))
Weights are provided as a keyword to series. No weight is
interpreted as 1.
series
The series operator accepts several keywords:
fillto specify a filling policy to avoidnanswhen the series will beadded with others; accepted values are"ffill"(forward-fill),"bfill"(backward-fill) or any floating value.
For instance in (add (series "a" #:fill 0) (series "b") will make
sure that series a, if shorter than series b will get zeroes
instead of nans where b provides values.
slice
This allows cutting a series at date points. It takes one positional
parameter (the series) and two optional keywords fromdate and
todate which must be strings in the iso8601 format.
Example: (slice (series "cut-me") #:fromdate "2018-01-01")
std
Computes the standard deviation over its input series.
Example: (std (series "station0") (series "station1") (series "station2"))
timedelta
Takes a timestamp and a number of years, months, weekds, days, hours, minutes (int) and computes a new date according to the asked delta elements.
Example: (timedelta (date "2020-1-1") #:weeks 1 #:hours 2)
today
Produces a timezone-aware timestamp as of today
The tz keyword allows to specify an alternate time zone.
The naive keyword forces production of a naive timestamp.
Both tz and naive keywords are mutually exlcusive.
Example: (today)
Series API
A few api calls are added to the tshistory base:
-
.register_formulato define a formula -
.eval_formulato evaluate on-the-fly a formula (useful to check that it computes before registering it)
register_formula
Exemple:
tsa.register_formula(
'my-sweet-formula',
'(* 3.14 (series "going-round"))'
)
eval_formula
Example:
>>> tsa.eval_formula('(* 3.14 (series "going-round"))')
...
2020-01-01 3.14
2020-01-02 6.28
2020-01-03 9.42
dtype: float64
Metadata
Release files for tshistory-formula 0.16.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tshistory_formula-0.16.2-py3-none-any.whl | Python 3 | none | any | Details |
Release files / tshistory_formula-0.16.2-py3-none-any.whl
| Download URL | tshistory_formula-0.16.2-py3-none-any.whl |
|---|---|
| Size | 65.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
671b632ab2546fb371923227d0c7768528d791b6626518e9cc59cac7c3929e3b
|
|
BLAKE2b-256 checksum How to use checksums |
7f53a8331b859382edb6ca6fa682d86504db4e7da2bf450fea840b018556764d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for tshistory-formula 0.16.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.