← if_then_else operator page · conditional/if_then_else/ds/manual_ex_1

Manual example 1 🟢 all ok

pandas ✅ · DuckDB ✅

Tests:

a data set condition, if ( DS_1#Id_4 = "F" ) then DS_2 else DS_3, that sends each data point to one of two data sets.

Manual:

if-then-else, Example 1, output verbatim: "returns the Data Point from thenOperand when the Data Point of condition having the same Identifiers’ values evaluates to true, and returns the Data Point from elseOperand otherwise". The page's introduction names the operands DS_cond, DS_1 and DS_2 where the script uses DS_1, DS_2 and DS_3, a slip of the prose and not of the example, and the page's TimePeriod type is written Time_Period, as vtlengine does.

Expected:

the page's six data points: those of DS_2 where Id_4 is F, for Id_2 S, F and I (25.8, null and 20.9), and those of DS_3 where it is M, for Id_2 B, G and S (0.12, 22.5 and 23.7). A data point that is missing from the branch it is sent to, such as B with Id_4 F in DS_2, is left out.

script.vtl ⇩

DS_r := if ( DS_1#Id_4 = "F" ) then DS_2 else DS_3;

input/DS_1.csv ⇩

Id_1Id_2Id_3Id_4Me_1
2012BTotalM5451780
2012BTotalF5643070
2012GTotalM5449803
2012GTotalF5673231
2012STotalM23099012
2012STotalF23719207
2012FTotalM31616281
2012FTotalF33671580
2012ITotalM28726599
2012ITotalF30667608
2012ATotalM
2012ATotalF

input/DS_1.json ⇩

{
  "datasets": [
    {
      "name": "DS_1",
      "DataStructure": [
        {"name": "Id_1", "type": "Time_Period", "role": "Identifier", "nullable": false},
        {"name": "Id_2", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Id_3", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Id_4", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Me_1", "type": "Number", "role": "Measure", "nullable": true}
      ]
    }
  ]
}

input/DS_2.csv ⇩

Id_1Id_2Id_3Id_4Me_1
2012STotalF25.8
2012FTotalF
2012ITotalF20.9
2012ATotalM6.3

input/DS_2.json ⇩

{
  "datasets": [
    {
      "name": "DS_2",
      "DataStructure": [
        {"name": "Id_1", "type": "Time_Period", "role": "Identifier", "nullable": false},
        {"name": "Id_2", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Id_3", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Id_4", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Me_1", "type": "Number", "role": "Measure", "nullable": true}
      ]
    }
  ]
}

input/DS_3.csv ⇩

Id_1Id_2Id_3Id_4Me_1
2012BTotalM0.12
2012GTotalM22.5
2012STotalM23.7
2012ATotalF

input/DS_3.json ⇩

{
  "datasets": [
    {
      "name": "DS_3",
      "DataStructure": [
        {"name": "Id_1", "type": "Time_Period", "role": "Identifier", "nullable": false},
        {"name": "Id_2", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Id_3", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Id_4", "type": "String", "role": "Identifier", "nullable": false},
        {"name": "Me_1", "type": "Number", "role": "Measure", "nullable": true}
      ]
    }
  ]
}

expected/DS_r.csv ⇩

Id_1Id_2Id_3Id_4Me_1
2012STotalF25.8
2012FTotalF
2012ITotalF20.9
2012BTotalM0.12
2012GTotalM22.5
2012STotalM23.7

expected/DS_r.json ⇩

{
  "datasets": [
    {
      "name": "DS_r",
      "DataStructure": [
        {"name": "Id_1", "type": "Time_Period", "role": "Identifier"},
        {"name": "Id_2", "type": "String", "role": "Identifier"},
        {"name": "Id_3", "type": "String", "role": "Identifier"},
        {"name": "Id_4", "type": "String", "role": "Identifier"},
        {"name": "Me_1", "type": "Number", "role": "Measure"}
      ]
    }
  ]
}