mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 00:43:33 +01:00
add quantile column (#5583)
This commit is contained in:
parent
6c56829976
commit
2b1e05aad0
@ -72,7 +72,7 @@ impl Command for Histogram {
|
||||
let frequency_column_name = match frequency_name_arg {
|
||||
Some(inner) => {
|
||||
let span = inner.span;
|
||||
if ["value", "count", "percentage"].contains(&inner.item.as_str()) {
|
||||
if ["value", "count", "quantile", "percentage"].contains(&inner.item.as_str()) {
|
||||
return Err(ShellError::UnsupportedInput(
|
||||
"frequency-column-name can't be 'value', 'count' or 'percentage'"
|
||||
.to_string(),
|
||||
@ -208,27 +208,29 @@ fn histogram_impl(
|
||||
let result_cols = vec![
|
||||
value_column_name.to_string(),
|
||||
"count".to_string(),
|
||||
"quantile".to_string(),
|
||||
"percentage".to_string(),
|
||||
freq_column.to_string(),
|
||||
];
|
||||
const MAX_FREQ_COUNT: f64 = 100.0;
|
||||
for (val, count) in counter.into_iter() {
|
||||
let (percentage, freq) = {
|
||||
let percentage = match calc_method {
|
||||
PercentageCalcMethod::Normalize => (count as f64 / total_cnt as f64),
|
||||
PercentageCalcMethod::Relative => (count as f64 / max_cnt as f64),
|
||||
};
|
||||
(
|
||||
format!("{:.2}%", percentage * 100_f64),
|
||||
"*".repeat((MAX_FREQ_COUNT * percentage).floor() as usize),
|
||||
)
|
||||
let quantile = match calc_method {
|
||||
PercentageCalcMethod::Normalize => (count as f64 / total_cnt as f64),
|
||||
PercentageCalcMethod::Relative => (count as f64 / max_cnt as f64),
|
||||
};
|
||||
|
||||
let percentage = format!("{:.2}%", quantile * 100_f64);
|
||||
let freq = "*".repeat((MAX_FREQ_COUNT * quantile).floor() as usize);
|
||||
|
||||
result.push(Value::Record {
|
||||
cols: result_cols.clone(),
|
||||
vals: vec![
|
||||
val.into_value(),
|
||||
Value::Int { val: count, span },
|
||||
Value::Float {
|
||||
val: quantile,
|
||||
span,
|
||||
},
|
||||
Value::String {
|
||||
val: percentage,
|
||||
span,
|
||||
|
@ -126,7 +126,7 @@ fn count() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
echo [[bit]; [1] [0] [0] [0] [0] [0] [0] [1]]
|
||||
echo [[bit]; [1] [0] [0] [0] [0] [0] [0] [1] [1]]
|
||||
| histogram bit --percentage-type relative
|
||||
| sort-by count
|
||||
| reject frequency
|
||||
@ -134,7 +134,7 @@ fn count() {
|
||||
"#
|
||||
));
|
||||
|
||||
let bit_json = r#"[ { "bit": 1, "count": 2, "percentage": "33.33%" }, { "bit": 0, "count": 6, "percentage": "100.00%" }]"#;
|
||||
let bit_json = r#"[ { "bit": 1, "count": 3, "quantile": 0.5, "percentage": "50.00%" }, { "bit": 0, "count": 6, "quantile": 1, "percentage": "100.00%" }]"#;
|
||||
|
||||
assert_eq!(actual.out, bit_json);
|
||||
}
|
||||
@ -152,7 +152,7 @@ fn count_with_normalize_percentage() {
|
||||
"#
|
||||
));
|
||||
|
||||
let bit_json = r#"[ { "bit": 1, "count": 2, "percentage": "25.00%" }, { "bit": 0, "count": 6, "percentage": "75.00%" }]"#;
|
||||
let bit_json = r#"[ { "bit": 1, "count": 2, "quantile": 0.25, "percentage": "25.00%" }, { "bit": 0, "count": 6, "quantile": 0.75, "percentage": "75.00%" }]"#;
|
||||
|
||||
assert_eq!(actual.out, bit_json);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user