nu-command 0.62.0

Nushell's built-in commands
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use nu_engine::CallExt;
use nu_protocol::{
    ast::Call,
    did_you_mean,
    engine::{Command, EngineState, Stack},
    Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
use polars::{
    frame::groupby::GroupBy,
    prelude::{PolarsError, QuantileInterpolOptions},
};

use crate::dataframe::values::NuGroupBy;

use super::super::values::{Column, NuDataFrame};

enum Operation {
    Mean,
    Sum,
    Min,
    Max,
    First,
    Last,
    Nunique,
    Quantile(f64),
    Median,
    Var,
    Std,
    Count,
}

impl Operation {
    fn from_tagged(
        name: &Spanned<String>,
        quantile: Option<Spanned<f64>>,
    ) -> Result<Operation, ShellError> {
        match name.item.as_ref() {
            "mean" => Ok(Operation::Mean),
            "sum" => Ok(Operation::Sum),
            "min" => Ok(Operation::Min),
            "max" => Ok(Operation::Max),
            "first" => Ok(Operation::First),
            "last" => Ok(Operation::Last),
            "nunique" => Ok(Operation::Nunique),
            "quantile" => match quantile {
                None => Err(ShellError::GenericError(
                    "Quantile value not fount".into(),
                    "Quantile operation requires quantile value".into(),
                    Some(name.span),
                    None,
                    Vec::new(),
                )),
                Some(value) => {
                    if (value.item < 0.0) | (value.item > 1.0) {
                        Err(ShellError::GenericError(
                            "Inappropriate quantile".into(),
                            "Quantile value should be between 0.0 and 1.0".into(),
                            Some(value.span),
                            None,
                            Vec::new(),
                        ))
                    } else {
                        Ok(Operation::Quantile(value.item))
                    }
                }
            },
            "median" => Ok(Operation::Median),
            "var" => Ok(Operation::Var),
            "std" => Ok(Operation::Std),
            "count" => Ok(Operation::Count),
            selection => {
                let possibilities = [
                    "mean".to_string(),
                    "sum".to_string(),
                    "min".to_string(),
                    "max".to_string(),
                    "first".to_string(),
                    "last".to_string(),
                    "nunique".to_string(),
                    "quantile".to_string(),
                    "median".to_string(),
                    "var".to_string(),
                    "std".to_string(),
                    "count".to_string(),
                ];

                match did_you_mean(&possibilities, selection) {
                    Some(suggestion) => Err(ShellError::DidYouMean(suggestion, name.span)),
                    None => Err(ShellError::GenericError(
                        "Operation not fount".into(),
                        "Operation does not exist".into(),
                        Some(name.span),
                        Some("Perhaps you want: mean, sum, min, max, first, last, nunique, quantile, median, var, std, or count".into()),
                        Vec::new(),
                    ))
                }
            }
        }
    }

    fn to_str(&self) -> &'static str {
        match self {
            Self::Mean => "mean",
            Self::Sum => "sum",
            Self::Min => "min",
            Self::Max => "max",
            Self::First => "first",
            Self::Last => "last",
            Self::Nunique => "nunique",
            Self::Quantile(_) => "quantile",
            Self::Median => "median",
            Self::Var => "var",
            Self::Std => "std",
            Self::Count => "count",
        }
    }
}

#[derive(Clone)]
pub struct Aggregate;

impl Command for Aggregate {
    fn name(&self) -> &str {
        "dfr aggregate"
    }

    fn usage(&self) -> &str {
        "Performs an aggregation operation on a dataframe and groupby object"
    }

    fn signature(&self) -> Signature {
        Signature::build(self.name())
            .required(
                "operation_name",
                SyntaxShape::String,
                "\n\tDataframes: mean, sum, min, max, quantile, median, var, std
\tGroupBy: mean, sum, min, max, first, last, nunique, quantile, median, var, std, count",
            )
            .named(
                "quantile",
                SyntaxShape::Number,
                "quantile value for quantile operation",
                Some('q'),
            )
            .switch(
                "explicit",
                "returns explicit names for groupby aggregations",
                Some('e'),
            )
            .category(Category::Custom("dataframe".into()))
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Aggregate sum by grouping by column a and summing on col b",
                example:
                    "[[a b]; [one 1] [one 2]] | dfr to-df | dfr group-by a | dfr aggregate sum",
                result: Some(
                    NuDataFrame::try_from_columns(vec![
                        Column::new("a".to_string(), vec![Value::test_string("one")]),
                        Column::new("b".to_string(), vec![Value::test_int(3)]),
                    ])
                    .expect("simple df for test should not fail")
                    .into_value(Span::test_data()),
                ),
            },
            Example {
                description: "Aggregate sum in dataframe columns",
                example: "[[a b]; [4 1] [5 2]] | dfr to-df | dfr aggregate sum",
                result: Some(
                    NuDataFrame::try_from_columns(vec![
                        Column::new("a".to_string(), vec![Value::test_int(9)]),
                        Column::new("b".to_string(), vec![Value::test_int(3)]),
                    ])
                    .expect("simple df for test should not fail")
                    .into_value(Span::test_data()),
                ),
            },
            Example {
                description: "Aggregate sum in series",
                example: "[4 1 5 6] | dfr to-df | dfr aggregate sum",
                result: Some(
                    NuDataFrame::try_from_columns(vec![Column::new(
                        "0".to_string(),
                        vec![Value::test_int(16)],
                    )])
                    .expect("simple df for test should not fail")
                    .into_value(Span::test_data()),
                ),
            },
        ]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        command(engine_state, stack, call, input)
    }
}

fn command(
    engine_state: &EngineState,
    stack: &mut Stack,
    call: &Call,
    input: PipelineData,
) -> Result<PipelineData, ShellError> {
    let operation: Spanned<String> = call.req(engine_state, stack, 0)?;
    let quantile: Option<Spanned<f64>> = call.get_flag(engine_state, stack, "quantile")?;
    let op = Operation::from_tagged(&operation, quantile)?;

    match input {
        PipelineData::Value(Value::CustomValue { val, span }, _) => {
            let df = val.as_any().downcast_ref::<NuDataFrame>();
            let groupby = val.as_any().downcast_ref::<NuGroupBy>();

            match (df, groupby) {
                (Some(df), None) => {
                    let df = df.as_ref();
                    let res = perform_dataframe_aggregation(df, op, operation.span)?;

                    Ok(PipelineData::Value(
                        NuDataFrame::dataframe_into_value(res, span),
                        None,
                    ))
                }
                (None, Some(nu_groupby)) => {
                    let groupby = nu_groupby.to_groupby()?;

                    let res = perform_groupby_aggregation(
                        groupby,
                        op,
                        operation.span,
                        call.head,
                        call.has_flag("explicit"),
                    )?;

                    Ok(PipelineData::Value(
                        NuDataFrame::dataframe_into_value(res, span),
                        None,
                    ))
                }
                _ => Err(ShellError::GenericError(
                    "Incorrect datatype".into(),
                    "no groupby or dataframe found in input stream".into(),
                    Some(call.head),
                    None,
                    Vec::new(),
                )),
            }
        }
        _ => Err(ShellError::GenericError(
            "Incorrect datatype".into(),
            "no groupby or dataframe found in input stream".into(),
            Some(call.head),
            None,
            Vec::new(),
        )),
    }
}

fn perform_groupby_aggregation(
    groupby: GroupBy,
    operation: Operation,
    operation_span: Span,
    agg_span: Span,
    explicit: bool,
) -> Result<polars::prelude::DataFrame, ShellError> {
    let mut res = match operation {
        Operation::Mean => groupby.mean(),
        Operation::Sum => groupby.sum(),
        Operation::Min => groupby.min(),
        Operation::Max => groupby.max(),
        Operation::First => groupby.first(),
        Operation::Last => groupby.last(),
        Operation::Nunique => groupby.n_unique(),
        Operation::Quantile(quantile) => {
            groupby.quantile(quantile, QuantileInterpolOptions::default())
        }
        Operation::Median => groupby.median(),
        Operation::Var => groupby.var(),
        Operation::Std => groupby.std(),
        Operation::Count => groupby.count(),
    }
    .map_err(|e| {
        let span = match &e {
            PolarsError::NotFound(_) => agg_span,
            _ => operation_span,
        };

        ShellError::GenericError(
            "Error calculating aggregation".into(),
            e.to_string(),
            Some(span),
            None,
            Vec::new(),
        )
    })?;

    if !explicit {
        let col_names = res
            .get_column_names()
            .iter()
            .map(|name| name.to_string())
            .collect::<Vec<String>>();

        for col in col_names {
            let from = match operation {
                Operation::Mean => "_mean",
                Operation::Sum => "_sum",
                Operation::Min => "_min",
                Operation::Max => "_max",
                Operation::First => "_first",
                Operation::Last => "_last",
                Operation::Nunique => "_n_unique",
                Operation::Quantile(_) => "_quantile",
                Operation::Median => "_median",
                Operation::Var => "_agg_var",
                Operation::Std => "_agg_std",
                Operation::Count => "_count",
            };

            let new_col = match col.find(from) {
                Some(index) => &col[..index],
                None => &col[..],
            };

            res.rename(&col, new_col)
                .expect("Column is always there. Looping with known names");
        }
    }

    Ok(res)
}

fn perform_dataframe_aggregation(
    dataframe: &polars::prelude::DataFrame,
    operation: Operation,
    operation_span: Span,
) -> Result<polars::prelude::DataFrame, ShellError> {
    match operation {
        Operation::Mean => Ok(dataframe.mean()),
        Operation::Sum => Ok(dataframe.sum()),
        Operation::Min => Ok(dataframe.min()),
        Operation::Max => Ok(dataframe.max()),
        Operation::Quantile(quantile) => dataframe
            .quantile(quantile, QuantileInterpolOptions::default())
            .map_err(|e| {
                ShellError::GenericError(
                    "Error calculating quantile".into(),
                    e.to_string(),
                    Some(operation_span),
                    None,
                    Vec::new(),
                )
            }),
        Operation::Median => Ok(dataframe.median()),
        Operation::Var => Ok(dataframe.var()),
        Operation::Std => Ok(dataframe.std()),
        operation => {
            let possibilities = [
                "mean".to_string(),
                "sum".to_string(),
                "min".to_string(),
                "max".to_string(),
                "quantile".to_string(),
                "median".to_string(),
                "var".to_string(),
                "std".to_string(),
            ];

            match did_you_mean(&possibilities, operation.to_str()) {
                Some(suggestion) => Err(ShellError::DidYouMean(suggestion, operation_span)),
                None => Err(ShellError::GenericError(
                    "Operation not fount".into(),
                    "Operation does not exist".into(),
                    Some(operation_span),
                    Some(
                        "Perhaps you want: mean, sum, min, max, quantile, median, var, or std"
                            .into(),
                    ),
                    Vec::new(),
                )),
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::super::super::test_dataframe::test_dataframe;
    use super::super::CreateGroupBy;
    use super::*;

    #[test]
    fn test_examples() {
        test_dataframe(vec![Box::new(Aggregate {}), Box::new(CreateGroupBy {})])
    }
}