Time shift · timeshift

🟢 3 of 3 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc2 (90ee9376) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart LR
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["Time shift · timeshift<br/>🟢3"]:::ok
    v_time_period["timeshift(DS, n) on time_period"]:::ok
    v_date["timeshift(DS, n) on date"]:::ok
    v_time["timeshift(DS, n) on time"]:::ok
    v_time_period --- O
    v_date --- O
    O --- v_time
    click v_time_period href "#form-time-period"
    click v_date href "#form-date"
    click v_time href "#form-time"
    

Types

Takes integer, time, date, time_period · Gives time, date, time_period, any type

Comes from · integer, time, date, time_period — 26 operators in 6 blocks 🟢26
General purpose operators (2) 🟢2
String operators (2) 🟢2
Numeric operators (4) 🟢4
Time operators (13) 🟢13
Aggregate and Analytic operators (2) 🟢2
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 34 operators in 7 blocks 🟢34
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Goes to: whatever takes its input's type

Goes to · time, date, time_period — 13 operators in 3 blocks 🟢13
General purpose operators (1) 🟢1
Time operators (11) 🟢11
Definitions (VTL-DL) (1) 🟢1
Goes to · operators that take any scalar — 49 operators in 9 blocks 🟢49
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Definitions (VTL-DL) (1) 🟢1

Forms

timeshift(DS, n) on time_period 🟢 · time_period, integer → time_period, any type

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

timeshift(DS_3, 1) on annual time_period series: one period into the future.

Manual:

Time shift, Example 3, output verbatim: the operator “shifts the reference time Identifier of a number of periods (of the time series) equal to the shiftNumber parameter. If shiftNumber is negative, the shift is in the past, otherwise it is in the future.”

Expected:

Each year moves one year on, 2010 to 2013 becoming 2011 to 2014, with the measures as they were.

script.vtl
DS_r := timeshift ( DS_3 , 1 );
Input DS_3
Id_1Id_2Me_1
A2010hello world
A2011
A2012say hello
A2013he
B2010hi,hello!
B2011hi
B2012
B2013hello!
Expected DS_r
Id_1Id_2Me_1
A2011hello world
A2012
A2013say hello
A2014he
B2011hi,hello!
B2012hi
B2013
B2014hello!

Files: script.vtl · input/DS_3.csv · input/DS_3.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

Manual example 4 🟢 pandas ✅ · DuckDB ✅
Tests:

timeshift(DS_4, -1) on an annual and a quarterly series of the same phenomenon A: one period of each series into the past.

Manual:

Time shift, Example 4, output verbatim: the operator “shifts the reference time Identifier of a number of periods (of the time series) equal to the shiftNumber parameter. If shiftNumber is negative, the shift is in the past”.

Expected:

The years move back one year, 2010 to 2013 becoming 2009 to 2012, and the quarters one quarter, 2010Q1 to 2010Q4 becoming 2009Q4 to 2010Q3.

script.vtl
DS_r := timeshift( DS_4 , -1 );
Input DS_4
Id_1Id_2Me_1
A2010hello world
A2011
A2012say hello
A2013he
A2010Q1hi, hello!
A2010Q2hi
A2010Q3
A2010Q4hello!
Expected DS_r
Id_1Id_2Me_1
A2009hello world
A2010
A2011say hello
A2012he
A2009Q4hi, hello!
A2010Q1hi
A2010Q2
A2010Q3hello!

Files: script.vtl · input/DS_4.csv · input/DS_4.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

timeshift(DS, n) on date 🟢 · date, integer → date, any type

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

timeshift(DS_2, 2) on annual date series, where “conventionally each period is identified by its last day”.

Manual:

Time shift, Example 2, output verbatim: the operator “shifts the reference time Identifier of a number of periods (of the time series) equal to the shiftNumber parameter”.

Expected:

Each date moves two years on, 2010-12-31 to 2013-12-31 becoming 2012-12-31 to 2015-12-31, with the measures as they were.

script.vtl
DS_r := timeshift ( DS_2 , 2 );
Input DS_2
Id_1Id_2Me_1
A2010-12-31hello world
A2011-12-31
A2012-12-31say hello
A2013-12-31he
B2010-12-31hi, hello!
B2011-12-31hi
B2012-12-31
B2013-12-31hello!
Expected DS_r
Id_1Id_2Me_1
A2012-12-31hello world
A2013-12-31
A2014-12-31say hello
A2015-12-31he
B2012-12-31hi, hello!
B2013-12-31hi
B2014-12-31
B2015-12-31hello!

Files: script.vtl · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

timeshift(DS, n) on time 🟢 · time, integer → time, any type

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

timeshift(DS_1, -1) on annual series whose reference time identifier is a time.

Manual:

Time shift, Example 1: “If shiftNumber is negative, the shift is in the past, otherwise it is in the future.”

Expected:

The manual’s output with its time values rewritten: each year moves one year back, 2010 to 2013 becoming 2009 to 2012, each the whole year YYYY-01-01/YYYY-12-31.

Adaptation:

The page writes its time values in “the time format YYYY-MM/YYYY-MM”, as 2010M1/2010M12, which vtlengine cannot load (DataLoadError 0-3-1-6, “Use YYYY-MM-DD/YYYY-MM-DD or YYYY or YYYY-MM”, on both engines). Each YYYYM1/YYYYM12 of the input and the output is written YYYY-01-01/YYYY-12-31, the same interval in the YYYY-MM-DD/YYYY-MM-DD form of the manual’s “Behaviour of Time operators” and of docs/data_types.rst; nothing else changes.

script.vtl
DS_r := timeshift ( DS_1 , -1 );
Input DS_1
Id_1Id_2Me_1
A2010-01-01/2010-12-31hello world
A2011-01-01/2011-12-31
A2012-01-01/2012-12-31say hello
A2013-01-01/2013-12-31he
B2010-01-01/2010-12-31hi, hello!
B2011-01-01/2011-12-31hi
B2012-01-01/2012-12-31
B2013-01-01/2013-12-31hello!
Expected DS_r
Id_1Id_2Me_1
A2009-01-01/2009-12-31hello world
A2010-01-01/2010-12-31
A2011-01-01/2011-12-31say hello
A2012-01-01/2012-12-31he
B2009-01-01/2009-12-31hi, hello!
B2010-01-01/2010-12-31hi
B2011-01-01/2011-12-31
B2012-01-01/2012-12-31hello!

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗