Frame.aggregateRowsBy is used to perform the aggregation.
In this case, the third argument, aggFunc, will be an aggregate function such as Stats.max.
However, most of the functions defined in the Stats module seem to support only float.
How can I aggregate the maximum and minimum values as DateTime or decimal?
The following is sample code that results in a runtime error.
Frame([ "id"; "date"; "quantity"], [Series.ofValues [1; 1; 2]; Series.ofValues [ DateTime(2023, 4, 13); DateTime(2023, 4, 10); DateTime(2023, 4, 16) ]; Series.ofValues [ 2.7M; 1.2M; 1.5M; ]])
|> Frame.aggregateRowsBy ["id"] ["date"; "quantity"] Stats.max
Error: System.InvalidCastException: Invalid cast from 'DateTime' to 'Double'.
There are two things to accomplish:
- Aggregate non-
floatvalues (e.g.DateTime,IComparable) - The post-aggregation
Frameshould respect the value type of the pre-aggregation. (The result of the aggregation must not be afloat.)
One limitation of the
aggregateRowsByfunction is that you can only use this to aggregate columns where the values can be converted to one shared type. You can always make thatobj, but then it will be very hard to do anything useful with the values. In your case, you can actually useIComparable, which will let you pass the values of the series toSeq.max:In more complex cases, this probably won't work. Then it probably will be easier to run
aggregateRowsBymultiple times (on different types of columns) - something like:For reference, the
obj-based version would be: