Broadcasting
Vectorized operations are more fun when you mix them with broadcasting. DTable supports broadcasting only in the following scenarios:
DTableagainst a single value. In this case, the single value will be broadcasted to match the shape ofDTable.DTableagainst a single column. In this case, the number of observations in the column must be equal to the observations inDTable. The column will be broadcasted to remaining columns inDTable.DTableagainst a single row. In this case, the row should have same number of variables as the number of variables in theDTable. This row would be broadcasted to rest of the rows.
Let's work through examples. We'll use the following dataset to work through examples:
>> san_francisco_dt = dtables.load_tuples([
>> ('max-temp', 'min-temp'),
>> (90, 40),
>> (85, 35),
>> (95, 40)
>> )]
>>
>> san_francisco_dt
+----------+----------+
| max-temp | min-temp |
+----------+----------+
| 90 | 40 |
| 85 | 35 |
| 95 | 40 |
+----------+----------+
Case 1: Scalar value - number of temperature readings above a certain value
>> san_francisco_dt > 35
+----------+----------+
| max-temp | min-temp |
+----------+----------+
| True | True |
| True | False |
| True | True |
+----------+----------+
Case 2: Scalar value - convert temperature to celsius
>> (san_francisco_dt - 32) * 5/9
+----------+----------+
| max-temp | min-temp |
+----------+----------+
| 32.22 | 4.44 |
| 29.44 | 1.66 |
| 35 | 4.44 |
+----------+----------+
Case 3: Single column
>> san_francisco_dt - san_francisco_dt.columns['min-temp']
+----------+----------+
| max-temp | min-temp |
+----------+----------+
| 50 | 0 |
| 50 | 0 |
| 55 | 0 |
+----------+----------+
Case 4: Single row
>> san_francisco_dt - san_francisco_dt.rows[0]
+----------+----------+
| max-temp | min-temp |
+----------+----------+
| 0 | 0 |
| -5 | -5 |
| 5 | 0 |
+----------+----------+