Commit d8f601eb authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

get_history: have a delta mode

By passing here a timedelta (before/after) object, one gets for
each insertion date a value date range starting at insertion date
and ending at insertion date plus/minus the delta (staircase query).

Useful to exhibit historic forecasts (or any kind of future/forward
value).
parent 70b64bf6d890
......@@ -650,6 +650,37 @@ insertion_date value_date
""", tsc)
def test_history_delta(engine, tsh):
for d in range(1, 3):
idate = utcdt(2018, 1, d)
serie = genserie(idate - timedelta(hours=1), 'H', 6, initval=[d])
tsh.insert(engine, serie, 'hd', 'aurelien.campeas@pythonian.fr',
_insertion_date=idate)
assert_df("""
insertion_date value_date
2018-01-01 00:00:00+00:00 2018-01-01 00:00:00+00:00 1.0
2018-01-01 01:00:00+00:00 1.0
2018-01-01 02:00:00+00:00 1.0
2018-01-02 00:00:00+00:00 2018-01-02 00:00:00+00:00 2.0
2018-01-02 01:00:00+00:00 2.0
2018-01-02 02:00:00+00:00 2.0
""", tsh.get_history(engine, 'hd', deltaafter=timedelta(hours=2)))
assert_df("""
insertion_date value_date
2018-01-01 00:00:00+00:00 2017-12-31 23:00:00+00:00 1.0
2018-01-01 00:00:00+00:00 1.0
2018-01-01 01:00:00+00:00 1.0
2018-01-02 00:00:00+00:00 2018-01-01 23:00:00+00:00 2.0
2018-01-02 00:00:00+00:00 2.0
2018-01-02 01:00:00+00:00 2.0
""", tsh.get_history(engine, 'hd',
deltabefore=timedelta(hours=1),
deltaafter=timedelta(hours=1)))
def test_nr_gethistory(engine, tsh):
s0 = pd.Series([-1, 0, 0, -1],
index=pd.DatetimeIndex(start=datetime(2016, 12, 29),
......
......@@ -104,11 +104,18 @@ class TimeSerie(SeriesServices):
to_insertion_date=None,
from_value_date=None,
to_value_date=None,
deltabefore=None,
deltaafter=None,
diffmode=False):
table = self._get_ts_table(cn, name)
if table is None:
return
if deltabefore is not None or deltaafter is not None:
assert diffmode is False
assert from_value_date is None
assert to_value_date is None
cset = self.schema.changeset
if diffmode:
......@@ -162,6 +169,13 @@ class TimeSerie(SeriesServices):
snapshot = Snapshot(cn, self, name)
series = []
for csid, idate in revs:
if deltabefore or deltaafter:
from_value_date = idate
to_value_date = idate
if deltabefore:
from_value_date = idate - deltabefore
if deltaafter:
to_value_date = idate + deltaafter
series.append((
idate,
snapshot.find([lambda cset, _: cset.c.id == csid],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment