mirror of
https://github.com/nushell/nushell
synced 2025-01-28 12:55:40 +00:00
Dataframe with real index (#5892)
* remove extra print * dataframe with real index * corrected dataframe tests * clippy error * clippy error
This commit is contained in:
parent
d3e84daa49
commit
c0901ef707
4 changed files with 43 additions and 7 deletions
|
@ -34,15 +34,20 @@ impl Command for ToNu {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
let cols = vec!["a".into(), "b".into()];
|
let cols = vec!["index".into(), "a".into(), "b".into()];
|
||||||
let rec_1 = Value::Record {
|
let rec_1 = Value::Record {
|
||||||
cols: cols.clone(),
|
cols: cols.clone(),
|
||||||
vals: vec![Value::test_int(1), Value::test_int(2)],
|
vals: vec![Value::test_int(0), Value::test_int(1), Value::test_int(2)],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
};
|
};
|
||||||
let rec_2 = Value::Record {
|
let rec_2 = Value::Record {
|
||||||
|
cols: cols.clone(),
|
||||||
|
vals: vec![Value::test_int(1), Value::test_int(3), Value::test_int(4)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
};
|
||||||
|
let rec_3 = Value::Record {
|
||||||
cols,
|
cols,
|
||||||
vals: vec![Value::test_int(3), Value::test_int(4)],
|
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(4)],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,7 +56,7 @@ impl Command for ToNu {
|
||||||
description: "Shows head rows from dataframe",
|
description: "Shows head rows from dataframe",
|
||||||
example: "[[a b]; [1 2] [3 4]] | into df | into nu",
|
example: "[[a b]; [1 2] [3 4]] | into df | into nu",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![rec_1, rec_2.clone()],
|
vals: vec![rec_1, rec_2],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
@ -59,7 +64,7 @@ impl Command for ToNu {
|
||||||
description: "Shows tail rows from dataframe",
|
description: "Shows tail rows from dataframe",
|
||||||
example: "[[a b]; [1 2] [5 6] [3 4]] | into df | into nu -t -n 1",
|
example: "[[a b]; [1 2] [5 6] [3 4]] | into df | into nu -t -n 1",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![rec_2],
|
vals: vec![rec_3],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|
|
@ -555,6 +555,12 @@ pub fn add_separator(values: &mut Vec<Value>, df: &DataFrame, span: Span) {
|
||||||
let mut cols = vec![];
|
let mut cols = vec![];
|
||||||
let mut vals = vec![];
|
let mut vals = vec![];
|
||||||
|
|
||||||
|
cols.push("index".to_string());
|
||||||
|
vals.push(Value::String {
|
||||||
|
val: "...".into(),
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
|
||||||
for name in df.get_column_names() {
|
for name in df.get_column_names() {
|
||||||
cols.push(name.to_string());
|
cols.push(name.to_string());
|
||||||
vals.push(Value::String {
|
vals.push(Value::String {
|
||||||
|
|
|
@ -400,10 +400,16 @@ impl NuDataFrame {
|
||||||
|
|
||||||
let values = (0..size)
|
let values = (0..size)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|_| {
|
.map(|i| {
|
||||||
let mut cols = vec![];
|
let mut cols = vec![];
|
||||||
let mut vals = vec![];
|
let mut vals = vec![];
|
||||||
|
|
||||||
|
cols.push("index".into());
|
||||||
|
vals.push(Value::Int {
|
||||||
|
val: (i + from_row) as i64,
|
||||||
|
span,
|
||||||
|
});
|
||||||
|
|
||||||
for (name, col) in &mut iterators {
|
for (name, col) in &mut iterators {
|
||||||
cols.push(name.clone());
|
cols.push(name.clone());
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ use terminal_size::{Height, Width};
|
||||||
|
|
||||||
const STREAM_PAGE_SIZE: usize = 1000;
|
const STREAM_PAGE_SIZE: usize = 1000;
|
||||||
const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100;
|
const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100;
|
||||||
|
const INDEX_COLUMN_NAME: &str = "index";
|
||||||
|
|
||||||
fn get_width_param(width_param: Option<i64>) -> usize {
|
fn get_width_param(width_param: Option<i64>) -> usize {
|
||||||
if let Some(col) = width_param {
|
if let Some(col) = width_param {
|
||||||
|
@ -41,6 +42,10 @@ impl Command for Table {
|
||||||
"Render the table."
|
"Render the table."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
"If the table contains a column called 'index', this column is used as the table index instead of the usual continuous index"
|
||||||
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
vec!["display", "render"]
|
vec!["display", "render"]
|
||||||
}
|
}
|
||||||
|
@ -374,8 +379,22 @@ fn convert_to_table(
|
||||||
// String1 = datatype, String2 = value as string
|
// String1 = datatype, String2 = value as string
|
||||||
let mut row: Vec<(String, String)> = vec![];
|
let mut row: Vec<(String, String)> = vec![];
|
||||||
if !disable_index {
|
if !disable_index {
|
||||||
row = vec![("string".to_string(), (row_num + row_offset).to_string())];
|
let row_val = match &item {
|
||||||
|
Value::Record { .. } => item
|
||||||
|
.get_data_by_key(INDEX_COLUMN_NAME)
|
||||||
|
.map(|value| value.into_string("", config)),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
|
.unwrap_or_else(|| (row_num + row_offset).to_string());
|
||||||
|
row = vec![("string".to_string(), (row_val).to_string())];
|
||||||
|
}
|
||||||
|
|
||||||
|
// The header with the INDEX is removed from the table headers since
|
||||||
|
// it is added to the natural table index
|
||||||
|
headers = headers
|
||||||
|
.into_iter()
|
||||||
|
.filter(|header| header != INDEX_COLUMN_NAME)
|
||||||
|
.collect();
|
||||||
|
|
||||||
if headers.is_empty() {
|
if headers.is_empty() {
|
||||||
row.push((
|
row.push((
|
||||||
|
|
Loading…
Reference in a new issue