Set difference · setdiff
---
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["Set difference · setdiff<br/>🟢1"]:::ok
v_two["setdiff(DS, DS)"]:::ok
v_two --- O
click v_two href "#form-two"
Types
Takes any scalar · Gives any type
Comes from: any operator that returns a value
Goes to: whatever takes its input's type
Forms
setdiff(DS, DS) 🟢 · any scalar → any type
Manual example 1 🟢 pandas ✅ · DuckDB ✅
- Tests:
setdiff ( DS_1, DS_2 ): five of the six data points ofDS_1are also inDS_2, with the same measures, andDS_2has one thatDS_1lacks.- Manual:
Set difference, Example 1, output verbatim: the operator returns a Data Set “containing the Data Points that appear in the first Data Set but not in the second”.
- Expected:
the page’s one data point, (2012, B, Total, Total) with the measure 10. The (2011, B, Total, Total) of
DS_2is not inDS_1, so it adds nothing.
DS_r := setdiff ( DS_1, DS_2 );
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2012 | B | Total | Total | 10 |
| 2012 | G | Total | Total | 20 |
| 2012 | F | Total | Total | 30 |
| 2012 | M | Total | Total | 40 |
| 2012 | I | Total | Total | 50 |
| 2012 | S | Total | Total | 60 |
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2011 | B | Total | Total | 10 |
| 2012 | G | Total | Total | 20 |
| 2012 | F | Total | Total | 30 |
| 2012 | M | Total | Total | 40 |
| 2012 | I | Total | Total | 50 |
| 2012 | S | Total | Total | 60 |
| Id_1 | Id_2 | Id_3 | Id_4 | Me_1 |
|---|---|---|---|---|
| 2012 | B | Total | Total | 10 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Manual example 2 🟢 pandas ✅ · DuckDB ✅
- Tests:
setdiff ( DS_3, DS_4 )on data sets with three identifiers, whereDS_4holds two of the three data points ofDS_3.- Manual:
Set difference, Example 2, output verbatim: the resulting Dataset contains “all the data points Data Point dp1 of the operand ds1, such that there is no Data Point dp2 of ds2 having the same values for homonym Identifier Components”.
- Expected:
the page’s one data point, (R, T, 2011) with the measure 12.
DS_r := setdiff ( DS_3, DS_4 );
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| R | M | 2011 | 7 |
| R | F | 2011 | 10 |
| R | T | 2011 | 12 |
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| R | M | 2011 | 7 |
| R | F | 2011 | 10 |
| Id_1 | Id_2 | Id_3 | Me_1 |
|---|---|---|---|
| R | T | 2011 | 12 |
Files: script.vtl · input/DS_3.csv · input/DS_3.json · input/DS_4.csv · input/DS_4.json · expected/DS_r.csv · expected/DS_r.json · example page ↗
Matched by identifiers not measures 🟢 pandas ✅ · DuckDB ✅
- Tests:
setdiff(DS_1, DS_2)where ids 2 and 3 are in both operands with different measures (in the page’s examples a shared data point has the same measure in both), to show whether data points are matched by identifiers alone. The measure is 10 *Id_1+ k, with k 0 forDS_1and 1 forDS_2, so its last digit names the operand.- Manual:
Set difference: “the resulting Dataset contains all the data points Data Point dp1 of the operand ds1, such that there is no Data Point dp2 of ds2 having the same values for homonym Identifier Components”: the condition is on the identifiers alone.
- Expected:
only id 1, with the measure 10 of
DS_1. Ids 2 and 3 have a data point inDS_2with the same identifiers, whatever its measure, and id 4 is only inDS_2, the subtrahend, which adds nothing.
DS_r := setdiff(DS_1, DS_2);
| Id_1 | Me_1 |
|---|---|
| 1 | 10 |
| 2 | 20 |
| 3 | 30 |
| Id_1 | Me_1 |
|---|---|
| 2 | 21 |
| 3 | 31 |
| 4 | 41 |
| Id_1 | Me_1 |
|---|---|
| 1 | 10 |
Files: script.vtl · input/DS_1.csv · input/DS_1.json · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗