mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
Better representation in nested dataframes (#3875)
* better dataframe representation in nested df * Error message correction
This commit is contained in:
parent
5826126284
commit
6984185e61
@ -37,7 +37,10 @@ impl WholeStreamCommand for Reverse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::needless_collect)]
|
||||||
fn reverse(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
fn reverse(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||||
|
// Clippy warning should be ignored
|
||||||
|
// This collect is needed to apply rev
|
||||||
let input = args.input.collect::<Vec<_>>();
|
let input = args.input.collect::<Vec<_>>();
|
||||||
Ok((input.into_iter().rev().map(ReturnSuccess::value)).into_action_stream())
|
Ok((input.into_iter().rev().map(ReturnSuccess::value)).into_action_stream())
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,9 @@ use std::hash::{Hash, Hasher};
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use sys_locale::get_locale;
|
use sys_locale::get_locale;
|
||||||
|
|
||||||
|
#[cfg(feature = "dataframe")]
|
||||||
|
use nu_protocol::dataframe::{FrameStruct, NuDataFrame};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
|
||||||
pub struct InlineRange {
|
pub struct InlineRange {
|
||||||
from: (InlineShape, RangeInclusion),
|
from: (InlineShape, RangeInclusion),
|
||||||
@ -45,9 +48,11 @@ pub enum InlineShape {
|
|||||||
// TODO: Error type
|
// TODO: Error type
|
||||||
Error,
|
Error,
|
||||||
|
|
||||||
// TODO: Dataframe type
|
|
||||||
#[cfg(feature = "dataframe")]
|
#[cfg(feature = "dataframe")]
|
||||||
DataFrame,
|
DataFrame(String),
|
||||||
|
|
||||||
|
#[cfg(feature = "dataframe")]
|
||||||
|
FrameStruct(String),
|
||||||
|
|
||||||
// Stream markers (used as bookend markers rather than actual values)
|
// Stream markers (used as bookend markers rather than actual values)
|
||||||
BeginningOfStream,
|
BeginningOfStream,
|
||||||
@ -123,6 +128,24 @@ impl InlineShape {
|
|||||||
InlineShape::Table(vec)
|
InlineShape::Table(vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "dataframe")]
|
||||||
|
pub fn from_df(df: &NuDataFrame) -> InlineShape {
|
||||||
|
let msg = format!("{} rows {} cols", df.as_ref().height(), df.as_ref().width());
|
||||||
|
|
||||||
|
InlineShape::DataFrame(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "dataframe")]
|
||||||
|
pub fn from_frame_struct(s: &FrameStruct) -> InlineShape {
|
||||||
|
match s {
|
||||||
|
FrameStruct::GroupBy(groupby) => {
|
||||||
|
let msg = groupby.by().join(",");
|
||||||
|
let msg = format!("groupby {}", msg);
|
||||||
|
InlineShape::DataFrame(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_value<'a>(value: impl Into<&'a UntaggedValue>) -> InlineShape {
|
pub fn from_value<'a>(value: impl Into<&'a UntaggedValue>) -> InlineShape {
|
||||||
match value.into() {
|
match value.into() {
|
||||||
UntaggedValue::Primitive(p) => InlineShape::from_primitive(p),
|
UntaggedValue::Primitive(p) => InlineShape::from_primitive(p),
|
||||||
@ -131,7 +154,9 @@ impl InlineShape {
|
|||||||
UntaggedValue::Error(_) => InlineShape::Error,
|
UntaggedValue::Error(_) => InlineShape::Error,
|
||||||
UntaggedValue::Block(_) => InlineShape::Block,
|
UntaggedValue::Block(_) => InlineShape::Block,
|
||||||
#[cfg(feature = "dataframe")]
|
#[cfg(feature = "dataframe")]
|
||||||
UntaggedValue::DataFrame(_) | UntaggedValue::FrameStruct(_) => InlineShape::DataFrame,
|
UntaggedValue::DataFrame(df) => InlineShape::from_df(df),
|
||||||
|
#[cfg(feature = "dataframe")]
|
||||||
|
UntaggedValue::FrameStruct(s) => InlineShape::from_frame_struct(s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,7 +369,16 @@ impl PrettyDebug for FormatInlineShape {
|
|||||||
InlineShape::Block => DbgDocBldr::opaque("block"),
|
InlineShape::Block => DbgDocBldr::opaque("block"),
|
||||||
InlineShape::Error => DbgDocBldr::error("error"),
|
InlineShape::Error => DbgDocBldr::error("error"),
|
||||||
#[cfg(feature = "dataframe")]
|
#[cfg(feature = "dataframe")]
|
||||||
InlineShape::DataFrame => DbgDocBldr::error("dataframe_pretty_formatter"),
|
InlineShape::DataFrame(msg) => DbgDocBldr::delimit(
|
||||||
|
"[",
|
||||||
|
DbgDocBldr::kind("dataframe") + DbgDocBldr::space() + DbgDocBldr::primitive(msg),
|
||||||
|
"]",
|
||||||
|
)
|
||||||
|
.group(),
|
||||||
|
#[cfg(feature = "dataframe")]
|
||||||
|
InlineShape::FrameStruct(msg) => {
|
||||||
|
DbgDocBldr::delimit("[", DbgDocBldr::primitive(msg), "]").group()
|
||||||
|
}
|
||||||
InlineShape::BeginningOfStream => DbgDocBldr::blank(),
|
InlineShape::BeginningOfStream => DbgDocBldr::blank(),
|
||||||
InlineShape::EndOfStream => DbgDocBldr::blank(),
|
InlineShape::EndOfStream => DbgDocBldr::blank(),
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,8 @@ impl NuDataFrame {
|
|||||||
UntaggedValue::Primitive(Primitive::Int(_))
|
UntaggedValue::Primitive(Primitive::Int(_))
|
||||||
| UntaggedValue::Primitive(Primitive::Decimal(_))
|
| UntaggedValue::Primitive(Primitive::Decimal(_))
|
||||||
| UntaggedValue::Primitive(Primitive::String(_))
|
| UntaggedValue::Primitive(Primitive::String(_))
|
||||||
| UntaggedValue::Primitive(Primitive::Boolean(_)) => {
|
| UntaggedValue::Primitive(Primitive::Boolean(_))
|
||||||
|
| UntaggedValue::DataFrame(_) => {
|
||||||
let key = format!("{}", 0);
|
let key = format!("{}", 0);
|
||||||
insert_value(value, key, &mut column_values)?
|
insert_value(value, key, &mut column_values)?
|
||||||
}
|
}
|
||||||
@ -286,6 +287,27 @@ impl NuDataFrame {
|
|||||||
Ok(series.clone())
|
Ok(series.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_value(&self, row: usize, span: Span) -> Result<Value, ShellError> {
|
||||||
|
let series = self.as_series(&Span::default())?;
|
||||||
|
let column = create_column(&series, row, row + 1)?;
|
||||||
|
|
||||||
|
if column.len() == 0 {
|
||||||
|
Err(ShellError::labeled_error_with_secondary(
|
||||||
|
"Not a valid row",
|
||||||
|
format!("No value found for index {}", row),
|
||||||
|
span,
|
||||||
|
format!("Note that the column size is {}", series.len()),
|
||||||
|
span,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
let value = column
|
||||||
|
.into_iter()
|
||||||
|
.next()
|
||||||
|
.expect("already checked there is a value");
|
||||||
|
Ok(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Print is made out a head and if the dataframe is too large, then a tail
|
// Print is made out a head and if the dataframe is too large, then a tail
|
||||||
pub fn print(&self) -> Result<Vec<Value>, ShellError> {
|
pub fn print(&self) -> Result<Vec<Value>, ShellError> {
|
||||||
let df = &self.as_ref();
|
let df = &self.as_ref();
|
||||||
|
@ -23,6 +23,10 @@ impl NuGroupBy {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn by(&self) -> &[String] {
|
||||||
|
&self.by
|
||||||
|
}
|
||||||
|
|
||||||
pub fn try_from_stream<T>(input: &mut T, span: &Span) -> Result<NuGroupBy, ShellError>
|
pub fn try_from_stream<T>(input: &mut T, span: &Span) -> Result<NuGroupBy, ShellError>
|
||||||
where
|
where
|
||||||
T: Iterator<Item = Value>,
|
T: Iterator<Item = Value>,
|
||||||
|
@ -214,11 +214,17 @@ pub fn get_data_by_member(value: &Value, name: &PathMember) -> Result<Value, She
|
|||||||
Tag::new(value.anchor(), name.span),
|
Tag::new(value.anchor(), name.span),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
_ => Err(ShellError::labeled_error(
|
UnspannedPathMember::Int(int) => {
|
||||||
"Integer as column",
|
if df.is_series() {
|
||||||
"Only string as column name",
|
df.get_value(*int as usize, name.span)
|
||||||
&name.span,
|
} else {
|
||||||
)),
|
Err(ShellError::labeled_error(
|
||||||
|
"Column not found",
|
||||||
|
"Column name not found in the dataframe",
|
||||||
|
name.span,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
other => Err(ShellError::type_error(
|
other => Err(ShellError::type_error(
|
||||||
"row or table",
|
"row or table",
|
||||||
|
Loading…
Reference in New Issue
Block a user