11// ------------------------------------------------------------------------------
2- // Copyright 2022 H2O.ai
2+ // Copyright 2022-2023 H2O.ai
33//
44// Permission is hereby granted, free of charge, to any person obtaining a
55// copy of this software and associated documentation files (the "Software"),
2323#include " column/latent.h"
2424#include " column/mean.h"
2525#include " documentation.h"
26- #include " expr/fexpr_func .h"
26+ #include " expr/fexpr_reduce_unary .h"
2727#include " expr/eval_context.h"
2828#include " expr/workframe.h"
2929#include " python/xargs.h"
@@ -32,47 +32,19 @@ namespace dt {
3232namespace expr {
3333
3434
35- class FExpr_Mean : public FExpr_Func {
36- private:
37- ptrExpr arg_;
38-
35+ class FExpr_Mean : public FExpr_ReduceUnary {
3936 public:
40- FExpr_Mean (ptrExpr &&arg)
41- : arg_(std::move(arg)) {}
42-
43- std::string repr () const override {
44- std::string out = " mean" ;
45- out += ' (' ;
46- out += arg_->repr ();
47- out += ' )' ;
48- return out;
49- }
50-
51-
52- Workframe evaluate_n (EvalContext &ctx) const override {
53- Workframe outputs (ctx);
54- Workframe wf = arg_->evaluate_n (ctx);
55- Groupby gby = ctx.get_groupby ();
37+ using FExpr_ReduceUnary::FExpr_ReduceUnary;
5638
57- if (!gby) {
58- gby = Groupby::single_group (wf.nrows ());
59- }
6039
61- for (size_t i = 0 ; i < wf.ncols (); ++i) {
62- bool is_grouped = ctx.has_group_column (
63- wf.get_frame_id (i),
64- wf.get_column_id (i)
65- );
66- Column coli = evaluate1 (wf.retrieve_column (i), gby, is_grouped);
67- outputs.add_column (std::move (coli), wf.retrieve_name (i), Grouping::GtoONE);
68- }
69-
70- return outputs;
40+ std::string name () const override {
41+ return " mean" ;
7142 }
7243
7344
74- Column evaluate1 (Column && col, const Groupby& gby, bool is_grouped) const {
45+ Column evaluate1 (Column&& col, const Groupby& gby, bool is_grouped) const override {
7546 SType stype = col.stype ();
47+ Column col_out;
7648
7749 switch (stype) {
7850 case SType::VOID: return Column (new ConstNa_ColumnImpl (
@@ -83,42 +55,34 @@ class FExpr_Mean : public FExpr_Func {
8355 case SType::INT16:
8456 case SType::INT32:
8557 case SType::INT64:
58+ case SType::DATE32:
59+ case SType::TIME64:
8660 case SType::FLOAT64:
87- return make<double >(std::move (col), SType::FLOAT64, gby, is_grouped);
61+ col_out = make<double >(std::move (col), SType::FLOAT64, gby, is_grouped);
62+ break ;
8863 case SType::FLOAT32:
89- return make<float >(std::move (col), SType::FLOAT32, gby, is_grouped);
90-
91- case SType::DATE32: {
92- Column coli = make<double >(std::move (col), SType::FLOAT64, gby, is_grouped);
93- coli.cast_inplace (SType::DATE32);
94- return coli;
95- }
96- case SType::TIME64: {
97- Column coli = make<double >(std::move (col), SType::FLOAT64, gby, is_grouped);
98- coli.cast_inplace (SType::TIME64);
99- return coli;
100- }
101-
64+ col_out = make<float >(std::move (col), SType::FLOAT32, gby, is_grouped);
65+ break ;
10266 default :
10367 throw TypeError ()
10468 << " Invalid column of type `" << stype << " ` in " << repr ();
10569 }
70+
71+ if (stype == SType::DATE32 || stype == SType::TIME64) {
72+ col_out.cast_inplace (stype);
73+ }
74+ return col_out;
10675 }
10776
10877
10978 template <typename T>
110- Column make (Column && col, SType stype, const Groupby& gby, bool is_grouped) const {
79+ Column make (Column&& col, SType stype, const Groupby& gby, bool is_grouped) const {
11180 col.cast_inplace (stype);
11281
113- if (is_grouped) {
114- return Column (new Latent_ColumnImpl (new Mean_ColumnImpl<T, true >(
115- std::move (col), gby
116- )));
117- } else {
118- return Column (new Latent_ColumnImpl (new Mean_ColumnImpl<T, false >(
119- std::move (col), gby
120- )));
121- }
82+ return is_grouped? std::move (col)
83+ : Column (new Latent_ColumnImpl (new Mean_ColumnImpl<T>(
84+ std::move (col), gby
85+ )));
12286 }
12387};
12488
0 commit comments