Time aggregation · time_agg

🟢 8 of 8 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · 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
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    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 aggregation · time_agg<br/>🟢8"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds["time_agg(p, DS)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_group_all["sum(DS group all time_agg(p))"]:::ok
        v_comp["calc time_agg(p, comp)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_time_period["time_agg(p, sc) on time_period"]:::ok
        v_sc_date["time_agg(p, sc) on date"]:::ok
        v_period_from["time_agg(p, from, sc)"]:::ok
        v_first["time_agg(p, sc, first) on date"]:::ok
        v_last["time_agg(p, sc, last) on date"]:::ok
    end
    subgraph G_use["Used with"]
        direction LR
        used__aggregate__aggregate_invocation["Aggregate invocation ↗<br/><small>used in group all</small>"]:::ext
        used__clause__aggr["aggr ↗<br/><small>used in group all</small>"]:::ext
    end
    O --- G_ds
    O --- G_comp
    O --- G_sc
    O -.- G_use
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
    style G_use fill:#ffffff,stroke:#868e96,stroke-width:1px,stroke-dasharray: 1 4
    click v_group_all href "#form-group-all"
    click v_ds href "#form-ds"
    click v_comp href "#form-comp"
    click v_sc_time_period href "#form-sc-time-period"
    click v_sc_date href "#form-sc-date"
    click v_period_from href "#form-period-from"
    click v_first href "#form-first"
    click v_last href "#form-last"
    click used__aggregate__aggregate_invocation href "../aggregate/aggregate_invocation.html" "Aggregate invocation · group by"
    click used__clause__aggr href "../clause/aggr.html" "Aggregation · aggr"
    

Types

Takes time, date, time_period, duration · Gives time, date, time_period

Comes from · time, date, time_period, duration — 10 operators in 2 blocks 🟢10
General purpose operators (1) 🟢1
Time operators (9) 🟢9
Comes from · operators that return any type (their input's) — 36 operators in 8 blocks 🟢36
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
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

time_agg(p, DS) 🟢 · time, duration → time

Monthly measure to quarters 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("Q", DS_1) on a dataset with a monthly time_period measure Me_1 and a quarterly time_period identifier Id_1: the measure is converted, not the identifier.

Manual:

Time aggregation: “The Data Set version acts on a single Measure Data Set of type time, date or time_period and returns a Data Set having the same structure”. Its Additional Constraint asks for “exactly one Identifier” of those types, where VTL 2.1 said “exactly one measure”: DS_1 meets both, and the example follows the Behaviour.

Expected:

2019M02, 2019M11 and 2019M07 go to 2019Q1, 2019Q4 and 2019Q3, the input duration being “inferred from the internal representation of the value”; converting the quarterly Id_1 instead would leave the months in Me_1.

script.vtl
DS_r := time_agg("Q", DS_1);
Input DS_1
Id_1Me_1
2020Q12019M02
2020Q22019M11
2020Q32019M07
Expected DS_r
Id_1Me_1
2020Q12019Q1
2020Q22019Q4
2020Q32019Q3

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

sum(DS group all time_agg(p)) 🟢 · time, duration → time

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

sum(DS_1 group all time_agg("A")): the quarters of 2010 summed by year, time_agg used with an aggregation and no operand.

Manual:

Time aggregation, Example 1, its data points verbatim: “VTL also provides a component version, for use in combination with an aggregation operator, because the change of frequency requires an aggregation.”

Expected:

2010 with 60, 100 and 20 for A, B and C. Me_1 may be Integer or Number: Sum declares a number, but “implementations may preserve the integer type for the resulting measure or component when all operand values are integer”; the manual’s output writes Integer, one of the two.

script.vtl
DS_r := sum ( DS_1  group all time_agg ( "A" ));
Input DS_1
Id_1Id_2Me_1
2010Q1A20
2010Q2A20
2010Q3A20
2010Q1B50
2010Q2B50
2010Q1C10
2010Q2C10
Expected DS_r
Id_1Id_2Me_1
2010A60
2010B100
2010C20

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

calc time_agg(p, comp) 🟢 · time, duration → time

Mixed periods to years 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("A", Me_1) inside calc, as the page’s ds_2 := ds_1[calc Me1 := time_agg(“M”,Me1)], on a quarter, a month and a semester.

Manual:

Time aggregation: “On time_period type, the operator maps the input value into the comprising larger time period specified by the periodIndTo parameter”, and the input duration “can be inferred from the internal representation of the value”.

Expected:

Each value goes to its year, whatever its own indicator: 2010Q2, 2011M07 and 2012S2 give 2010, 2011 and 2012, a Time_Period Me_2, as the operator “returns a scalar of the corresponding type”.

script.vtl
DS_r := DS_1[calc Me_2 := time_agg("A", Me_1)];
Input DS_1
Id_1Me_1
12010Q2
22011M07
32012S2
Expected DS_r
Id_1Me_1Me_2
12010Q22010
22011M072011
32012S22012

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

time_agg(p, sc) on time_period 🟢 · time_period, duration → time_period

Month to quarter 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("Q", SC_1) with SC_1 = 2012M01: January 2012 to its quarter, the page’s Example 2 without the cast mask, the scalar being an input because VTL has no time_period literal.

Manual:

Time aggregation, Example 2: DS_r := time_agg ( "Q", cast ( "2012M01", time_period, "YYYY\\MMM" ) ); “returns “2012Q1””.

Expected:

DS_r = 2012Q1, a Time_Period: without the mask, the manual’s default conversion from string to time_period (Type conversion) reads 2012M01 as January 2012.

script.vtl
DS_r := time_agg("Q", SC_1);
Input scalars
SC_1
2012M01
Expected scalars

Name

Type

Value

DS_r

Time_Period

"2012Q1"

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

time_agg(p, sc) on date 🟢 · date, duration → date

Without first or last 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("M", SC_1) with SC_1 = 2012-12-23, without first or last: the page’s valid syntax time_agg(“M”, cast (“2012-12-23”, date, “YYYY-MM-DD”) ) with the date as an input instead of the cast, whose mask the default conversion does not need.

Manual:

Time aggregation: “On date type, the operator maps the input value into the comprising larger period, whose duration is the one specified by the periodIndTo parameter, which is conventionally represented either by the start or by the end date, according to the first/last parameter”; it states no default for that parameter.

Expected:

the error 1-1-19-11, “At op time_agg: If used over a Date, first/last parameter must be declared”: the page represents the period of a date “according to the first/last parameter” and gives no default, though its examples of valid syntaxes have this call.

script.vtl
DS_r := time_agg("M", SC_1);
Input scalars
SC_1
2012-12-23

Expected error 1-1-19-11: At op time_agg: If used over a Date, first/last parameter must be declared.

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/error.json · example page ↗

time_agg(p, from, sc) 🟢 · time, duration → time

Quarter to year 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("A", "Q", SC_1) with SC_1 = 2012Q2: the optional periodIndFrom given, Q, the duration of the operand.

Manual:

Time aggregation: the syntax time_agg ( periodIndTo { , periodIndFrom } { , op } { , first | last } ), and “It is only possible to convert smaller duration values to larger duration values”, as a quarter to a year is.

Expected:

DS_r = 2012, a Time_Period: the second quarter of 2012 is in the year 2012.

script.vtl
DS_r := time_agg("A", "Q", SC_1);
Input scalars
SC_1
2012Q2
Expected scalars

Name

Type

Value

DS_r

Time_Period

"2012"

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

time_agg(p, sc, first) on date 🟢 · date, duration → date

Start of the year 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("A", SC_1, first) with SC_1 = 2012-02-13: a date to its year, represented by its start, on the date of the page’s Example 3.

Manual:

Time aggregation, Example 4: “The following example maps a date to year level, 2012 (beginning of the period)” and “produces a date value corresponding to the string “20120101””. Its script, DS_r := time_agg(cast( “A”, “2012M1”, date, “YYYYMMDD”), first );, is garbled, with the indicator inside the cast and 2012M1 not matching the mask: the example writes the call that the text describes.

Expected:

DS_r = 2012-01-01, a Date: with first, the year 2012 is “represented […] by the start”.

script.vtl
DS_r := time_agg("A", SC_1, first);
Input scalars
SC_1
2012-02-13
Expected scalars

Name

Type

Value

DS_r

Date

"2012-01-01"

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

time_agg(p, sc, last) on date 🟢 · date, duration → date

End of the quarter 🟢 pandas ✅ · DuckDB ✅
Tests:

time_agg("Q", SC_1, last) with SC_1 = 2012-02-13: a date to its quarter, represented by its end; the page’s Example 3, with the date as an input in the default YYYY-MM-DD form instead of the cast with the mask YYYYMMDD.

Manual:

Time aggregation, Example 3: “The following example maps a date to quarter level, 2012 (end of the period)”, DS_r := time_agg( “Q”, cast(“20120213”, date, “YYYYMMDD”), last );, “and produces a date value corresponding to the string “20120331””.

Expected:

DS_r = 2012-03-31, a Date: 2012-02-13 is in the first quarter of 2012, which ends on March 31.

script.vtl
DS_r := time_agg("Q", SC_1, last);
Input scalars
SC_1
2012-02-13
Expected scalars

Name

Type

Value

DS_r

Date

"2012-03-31"

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