mirror of
https://github.com/nushell/nushell.git
synced 2025-05-29 06:17:54 +02:00
131 lines
4.5 KiB
Rust
131 lines
4.5 KiB
Rust
use super::super::values::{Column, NuDataFrame};
|
|
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Example, PipelineData, ShellError, Signature, Span, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct ToDataFrame;
|
|
|
|
impl Command for ToDataFrame {
|
|
fn name(&self) -> &str {
|
|
"into df"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Converts a list, table or record into a dataframe"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.input_type(Type::Any)
|
|
.output_type(Type::Custom("dataframe".into()))
|
|
.category(Category::Custom("dataframe".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Takes a dictionary and creates a dataframe",
|
|
example: "[[a b];[1 2] [3 4]] | into df",
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(vec![
|
|
Column::new(
|
|
"a".to_string(),
|
|
vec![Value::test_int(1), Value::test_int(3)],
|
|
),
|
|
Column::new(
|
|
"b".to_string(),
|
|
vec![Value::test_int(2), Value::test_int(4)],
|
|
),
|
|
])
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
Example {
|
|
description: "Takes a list of tables and creates a dataframe",
|
|
example: "[[1 2 a] [3 4 b] [5 6 c]] | into df",
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(vec![
|
|
Column::new(
|
|
"0".to_string(),
|
|
vec![Value::test_int(1), Value::test_int(3), Value::test_int(5)],
|
|
),
|
|
Column::new(
|
|
"1".to_string(),
|
|
vec![Value::test_int(2), Value::test_int(4), Value::test_int(6)],
|
|
),
|
|
Column::new(
|
|
"2".to_string(),
|
|
vec![
|
|
Value::test_string("a"),
|
|
Value::test_string("b"),
|
|
Value::test_string("c"),
|
|
],
|
|
),
|
|
])
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
Example {
|
|
description: "Takes a list and creates a dataframe",
|
|
example: "[a b c] | into df",
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(vec![Column::new(
|
|
"0".to_string(),
|
|
vec![
|
|
Value::test_string("a"),
|
|
Value::test_string("b"),
|
|
Value::test_string("c"),
|
|
],
|
|
)])
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
Example {
|
|
description: "Takes a list of booleans and creates a dataframe",
|
|
example: "[true true false] | into df",
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(vec![Column::new(
|
|
"0".to_string(),
|
|
vec![
|
|
Value::test_bool(true),
|
|
Value::test_bool(true),
|
|
Value::test_bool(false),
|
|
],
|
|
)])
|
|
.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> {
|
|
NuDataFrame::try_from_iter(input.into_iter())
|
|
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::super::super::test_dataframe::test_dataframe;
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
test_dataframe(vec![Box::new(ToDataFrame {})])
|
|
}
|
|
}
|