2021-12-06 05:09:49 +01:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
2022-06-12 21:18:00 +02:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
2021-12-06 05:09:49 +01:00
|
|
|
};
|
|
|
|
|
2021-12-21 19:32:09 +01:00
|
|
|
use crate::dataframe::values::utils::convert_columns_string;
|
|
|
|
|
|
|
|
use super::super::values::{Column, NuDataFrame};
|
2021-12-06 05:09:49 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2021-12-21 19:32:09 +01:00
|
|
|
pub struct GetDF;
|
2021-12-06 05:09:49 +01:00
|
|
|
|
2021-12-21 19:32:09 +01:00
|
|
|
impl Command for GetDF {
|
2021-12-06 05:09:49 +01:00
|
|
|
fn name(&self) -> &str {
|
2022-06-12 21:18:00 +02:00
|
|
|
"get"
|
2021-12-06 05:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2021-12-21 19:32:09 +01:00
|
|
|
"Creates dataframe with the selected columns"
|
2021-12-06 05:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name())
|
2021-12-21 19:32:09 +01:00
|
|
|
.rest("rest", SyntaxShape::Any, "column names to sort dataframe")
|
2021-12-06 05:09:49 +01:00
|
|
|
.category(Category::Custom("dataframe".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
2022-06-12 21:18:00 +02:00
|
|
|
description: "Returns the selected column",
|
|
|
|
example: "[[a b]; [1 2] [3 4]] | to-df | get a",
|
2021-12-06 05:09:49 +01:00
|
|
|
result: Some(
|
|
|
|
NuDataFrame::try_from_columns(vec![Column::new(
|
|
|
|
"a".to_string(),
|
2021-12-19 08:46:13 +01:00
|
|
|
vec![Value::test_int(1), Value::test_int(3)],
|
2021-12-06 05:09:49 +01:00
|
|
|
)])
|
|
|
|
.expect("simple df for test should not fail")
|
2021-12-19 08:46:13 +01:00
|
|
|
.into_value(Span::test_data()),
|
2021-12-06 05:09:49 +01:00
|
|
|
),
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:18:00 +02:00
|
|
|
fn input_type(&self) -> Type {
|
|
|
|
Type::Custom("dataframe".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output_type(&self) -> Type {
|
|
|
|
Type::Custom("dataframe".into())
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:09:49 +01:00
|
|
|
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> {
|
2021-12-21 19:32:09 +01:00
|
|
|
let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
|
|
|
let (col_string, col_span) = convert_columns_string(columns, call.head)?;
|
2021-12-06 05:09:49 +01:00
|
|
|
|
|
|
|
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
|
|
|
|
2021-12-21 19:32:09 +01:00
|
|
|
df.as_ref()
|
|
|
|
.select(&col_string)
|
|
|
|
.map_err(|e| {
|
2022-04-18 14:34:10 +02:00
|
|
|
ShellError::GenericError(
|
2021-12-21 19:32:09 +01:00
|
|
|
"Error selecting columns".into(),
|
|
|
|
e.to_string(),
|
2022-04-18 14:34:10 +02:00
|
|
|
Some(col_span),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2021-12-21 19:32:09 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
2021-12-06 05:09:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2021-12-21 19:32:09 +01:00
|
|
|
use super::super::super::test_dataframe::test_dataframe;
|
2021-12-06 05:09:49 +01:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
2021-12-21 19:32:09 +01:00
|
|
|
test_dataframe(vec![Box::new(GetDF {})])
|
2021-12-06 05:09:49 +01:00
|
|
|
}
|
|
|
|
}
|