mirror of
https://github.com/nushell/nushell.git
synced 2025-04-24 05:08:29 +02:00
Very often we need to work with tables (say extracted from unstructured data or some kind of final report, timeseries, and the like). It's inevitable we will be having columns that we can't know beforehand what their names will be, or how many. Also, we may end up with certain cells having values we may want to remove as we explore. Here, `update cells` fundamentally goes over every cell in the table coming in and updates the cell's contents with the output of the block passed. Basic example here: ``` > [ [ ty1, t2, ty]; [ 1, a, $nothing] [(wrap), (0..<10), 1Mb] [ 1s, ({}), 1000000] [ $true, $false, ([[]])] ] | update cells { describe } ───┬───────────────────────┬───────────────────────────┬────────── # │ ty1 │ t2 │ ty ───┼───────────────────────┼───────────────────────────┼────────── 0 │ integer │ string │ nothing 1 │ row Column(table of ) │ range[[integer, integer)] │ filesize 2 │ string │ nothing │ integer 3 │ boolean │ boolean │ table of ───┴───────────────────────┴───────────────────────────┴────────── ``` and another one (in the examples) for cases, say we have a timeseries table generated and we want to remove the zeros and have empty strings and save it out to something like CSV. ``` > [ [2021-04-16, 2021-06-10, 2021-09-18, 2021-10-15, 2021-11-16, 2021-11-17, 2021-11-18]; [ 37, 0, 0, 0, 37, 0, 0] ] | update cells {|value| i if ($value | into int) == 0 { "" } { $value } } ───┬────────────┬────────────┬────────────┬────────────┬────────────┬────────────┬──────────── # │ 2021-04-16 │ 2021-06-10 │ 2021-09-18 │ 2021-10-15 │ 2021-11-16 │ 2021-11-17 │ 2021-11-18 ───┼────────────┼────────────┼────────────┼────────────┼────────────┼────────────┼──────────── 0 │ 37 │ │ │ │ 37 │ │ ───┴────────────┴────────────┴────────────┴────────────┴────────────┴────────────┴──────────── ```
142 lines
4.7 KiB
Rust
142 lines
4.7 KiB
Rust
mod charting;
|
|
mod config;
|
|
mod conversions;
|
|
mod core_commands;
|
|
#[cfg(feature = "dataframe")]
|
|
mod dataframe;
|
|
mod env;
|
|
mod filesystem;
|
|
mod filters;
|
|
mod formats;
|
|
mod generators;
|
|
mod math;
|
|
mod network;
|
|
mod path;
|
|
mod pathvar;
|
|
mod platform;
|
|
mod random;
|
|
mod shells;
|
|
mod strings;
|
|
mod system;
|
|
mod viewers;
|
|
|
|
pub use charting::*;
|
|
pub use config::*;
|
|
pub use conversions::*;
|
|
pub use core_commands::*;
|
|
#[cfg(feature = "dataframe")]
|
|
pub use dataframe::{
|
|
DataFrame, DataFrameAggregate, DataFrameAllFalse, DataFrameAllTrue, DataFrameAppend,
|
|
DataFrameArgMax, DataFrameArgMin, DataFrameArgSort, DataFrameArgTrue, DataFrameArgUnique,
|
|
DataFrameColumn, DataFrameConcatenate, DataFrameContains, DataFrameCumulative, DataFrameDTypes,
|
|
DataFrameDescribe, DataFrameDrop, DataFrameDropDuplicates, DataFrameDropNulls,
|
|
DataFrameDummies, DataFrameFilter, DataFrameFirst, DataFrameGet, DataFrameGetDay,
|
|
DataFrameGetHour, DataFrameGetMinute, DataFrameGetMonth, DataFrameGetNanoSecond,
|
|
DataFrameGetOrdinal, DataFrameGetSecond, DataFrameGetWeek, DataFrameGetWeekDay,
|
|
DataFrameGetYear, DataFrameGroupBy, DataFrameIsDuplicated, DataFrameIsIn, DataFrameIsNotNull,
|
|
DataFrameIsNull, DataFrameIsUnique, DataFrameJoin, DataFrameLast, DataFrameList, DataFrameMelt,
|
|
DataFrameNNull, DataFrameNUnique, DataFrameNot, DataFrameOpen, DataFramePivot, DataFrameRename,
|
|
DataFrameReplace, DataFrameReplaceAll, DataFrameRolling, DataFrameSample, DataFrameSelect,
|
|
DataFrameSeriesRename, DataFrameSet, DataFrameSetWithIdx, DataFrameShape, DataFrameShift,
|
|
DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameStrFTime, DataFrameStringLengths,
|
|
DataFrameStringSlice, DataFrameTake, DataFrameToCsv, DataFrameToDF, DataFrameToLowercase,
|
|
DataFrameToParquet, DataFrameToUppercase, DataFrameUnique, DataFrameValueCounts,
|
|
DataFrameWhere, DataFrameWithColumn,
|
|
};
|
|
pub use env::*;
|
|
pub use filesystem::*;
|
|
pub use filters::*;
|
|
pub use formats::*;
|
|
pub use generators::*;
|
|
pub use math::*;
|
|
pub use network::*;
|
|
pub use path::*;
|
|
pub use pathvar::*;
|
|
pub use platform::*;
|
|
pub use random::*;
|
|
pub use shells::*;
|
|
pub use strings::*;
|
|
pub use system::*;
|
|
pub use viewers::*;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::examples::{test_anchors, test_examples};
|
|
use nu_engine::{whole_stream_command, Command};
|
|
use nu_errors::ShellError;
|
|
|
|
fn full_tests() -> Vec<Command> {
|
|
vec![
|
|
whole_stream_command(ErrorMake),
|
|
whole_stream_command(Drop),
|
|
whole_stream_command(DropNth),
|
|
whole_stream_command(DropColumn),
|
|
whole_stream_command(Append),
|
|
whole_stream_command(GroupBy),
|
|
whole_stream_command(Insert),
|
|
whole_stream_command(MoveColumn),
|
|
whole_stream_command(Update),
|
|
whole_stream_command(Empty),
|
|
whole_stream_command(Nth),
|
|
// whole_stream_command(Select),
|
|
// whole_stream_command(Get),
|
|
// Str Command Suite
|
|
whole_stream_command(Str),
|
|
whole_stream_command(StrToDecimal),
|
|
whole_stream_command(StrToInteger),
|
|
whole_stream_command(StrDowncase),
|
|
whole_stream_command(StrUpcase),
|
|
whole_stream_command(StrCapitalize),
|
|
whole_stream_command(StrFindReplace),
|
|
whole_stream_command(StrSubstring),
|
|
whole_stream_command(StrToDatetime),
|
|
whole_stream_command(StrContains),
|
|
whole_stream_command(StrIndexOf),
|
|
whole_stream_command(StrTrim),
|
|
whole_stream_command(StrStartsWith),
|
|
whole_stream_command(StrEndsWith),
|
|
//whole_stream_command(StrCollect),
|
|
whole_stream_command(StrLength),
|
|
whole_stream_command(StrLPad),
|
|
whole_stream_command(StrReverse),
|
|
whole_stream_command(StrRPad),
|
|
whole_stream_command(StrCamelCase),
|
|
whole_stream_command(StrPascalCase),
|
|
whole_stream_command(StrKebabCase),
|
|
whole_stream_command(StrSnakeCase),
|
|
whole_stream_command(StrScreamingSnakeCase),
|
|
whole_stream_command(ToMarkdown),
|
|
]
|
|
}
|
|
|
|
fn only_examples() -> Vec<Command> {
|
|
let mut commands = full_tests();
|
|
commands.extend([
|
|
whole_stream_command(UpdateCells),
|
|
whole_stream_command(Zip),
|
|
whole_stream_command(Flatten),
|
|
]);
|
|
commands
|
|
}
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
for cmd in only_examples() {
|
|
println!("cmd: {}", cmd.name());
|
|
test_examples(cmd)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn tracks_metadata() -> Result<(), ShellError> {
|
|
for cmd in full_tests() {
|
|
test_anchors(cmd)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
}
|