DataFrame
DataFrame copied to clipboard
Can not divide number by data series
All arithmetic operations can be perform in four ways: scalar-scalar, series-scalar, scalar-series, and series-series.
Except for the division. It is implemented in a way that expects a number:
/ aNumber
"Primitive. This primitive (for /) divides the receiver by the argument
and returns the result if the division is exact. Fail if the result is not a
whole integer. Fail if the argument is 0 or is not a SmallInteger. Optional.
No Lookup. See Object documentation whatIsAPrimitive."
<primitive: 10>
aNumber isZero ifTrue: [^(ZeroDivide dividend: self) signal].
^(aNumber isMemberOf: SmallInteger)
ifTrue: [(Fraction numerator: self denominator: aNumber) reduced]
ifFalse: [super / aNumber]
As a result, these are valid:
2 * series.
3 + series.
4 - series.
But this raises an exception that series does not understand isZero:
1 / series.
I wrote a test for it:
testArithmeticsDivideScalarBySeries
| a b |
a := #(1 2 3) asDataSeries.
b := { 1 . 1/2 . 1/3 } asDataSeries.
self assert: 1 / a equals: b.
But I can't think of any way to elegantly fix it (without too many hacks), so I leave this issue for the future