Skip to content

Commit b0cfd2c

Browse files
authored
Combine command and output code blocks (#1799)
1 parent 82e7a91 commit b0cfd2c

File tree

13 files changed

+1028
-1413
lines changed

13 files changed

+1028
-1413
lines changed

book/dataframes.md

Lines changed: 57 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ bench -n 10 --pretty {
110110
| math sum
111111
}
112112
}
113-
```
114-
115-
Output
116-
117-
```
118-
3sec 268ms +/- 50ms
113+
# => 3sec 268ms +/- 50ms
119114
```
120115

121116
So, 3.3 seconds to perform this aggregation.
@@ -137,12 +132,7 @@ And the result from the benchmark is:
137132
bench -n 10 --pretty {
138133
python load.py | complete | null
139134
}
140-
```
141-
142-
Output
143-
144-
```
145-
1sec 322ms +/- 6ms
135+
# => 1sec 322ms +/- 6ms
146136
```
147137

148138
Not bad at all. Pandas managed to get it 2.6 times faster than Nushell.
@@ -167,12 +157,7 @@ instance for each test in order of honest comparison) is:
167157
bench -n 10 --pretty {
168158
nu load.nu | complete | null
169159
}
170-
```
171-
172-
Output
173-
174-
```
175-
135ms +/- 4ms
160+
# => 135ms +/- 4ms
176161
```
177162

178163
The `polars` dataframes plugin managed to finish operation 10 times
@@ -437,25 +422,20 @@ $group
437422
(polars col float_1 | polars sum)
438423
(polars col float_2 | polars count)
439424
] | polars sort-by first
440-
```
441-
442-
Output
443-
444-
```
445-
╭────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────╮
446-
│ plan │ SORT BY [col("first")] │
447-
│ │ AGGREGATE │
448-
│ │ [col("int_1").n_unique(), col("int_2").min(), col("float_1") │
449-
│ │ .sum(), col("float_2").count()] BY [col("first")] FROM │
450-
│ │ DF ["int_1", "int_2", "float_1", "float_2 │
451-
│ │ "]; PROJECT */8 COLUMNS; SELECTION: "None" │
452-
│ optimized_plan │ SORT BY [col("first")] │
453-
│ │ AGGREGATE │
454-
│ │ [col("int_1").n_unique(), col("int_2").min(), col("float_1") │
455-
│ │ .sum(), col("float_2").count()] BY [col("first")] FROM │
456-
│ │ DF ["int_1", "int_2", "float_1", "float_2 │
457-
│ │ "]; PROJECT 5/8 COLUMNS; SELECTION: "None" │
458-
╰────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────╯
425+
# => ╭────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────╮
426+
# => │ plan │ SORT BY [col("first")] │
427+
# => │ │ AGGREGATE │
428+
# => │ │ [col("int_1").n_unique(), col("int_2").min(), col("float_1") │
429+
# => │ │ .sum(), col("float_2").count()] BY [col("first")] FROM │
430+
# => │ │ DF ["int_1", "int_2", "float_1", "float_2 │
431+
# => │ │ "]; PROJECT */8 COLUMNS; SELECTION: "None" │
432+
# => │ optimized_plan │ SORT BY [col("first")] │
433+
# => │ │ AGGREGATE │
434+
# => │ │ [col("int_1").n_unique(), col("int_2").min(), col("float_1") │
435+
# => │ │ .sum(), col("float_2").count()] BY [col("first")] FROM │
436+
# => │ │ DF ["int_1", "int_2", "float_1", "float_2 │
437+
# => │ │ "]; PROJECT 5/8 COLUMNS; SELECTION: "None" │
438+
# => ╰────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────╯
459439
```
460440

461441
As you can see, the `GroupBy` object is a very powerful variable and it is
@@ -902,40 +882,30 @@ in column `word`
902882

903883
```nu
904884
$df_1 | polars filter-with ($in.word | polars is-unique)
905-
```
906-
907-
Output
908-
909-
```
910-
╭───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬───────╮
911-
│ # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word │
912-
├───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼───────┤
913-
│ 0 │ 1 │ 11 │ 0.10 │ 1.00 │ a │ b │ c │ first │
914-
│ 1 │ 8 │ 18 │ 0.80 │ 7.00 │ c │ c │ b │ eight │
915-
╰───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴───────╯
885+
# => ╭───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬───────╮
886+
# => │ # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word │
887+
# => ├───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼───────┤
888+
# => │ 0 │ 1 │ 11 │ 0.10 │ 1.00 │ a │ b │ c │ first │
889+
# => │ 1 │ 8 │ 18 │ 0.80 │ 7.00 │ c │ c │ b │ eight │
890+
# => ╰───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴───────╯
916891
```
917892

918893
Or all the duplicated ones
919894

920895
```nu
921896
$df_1 | polars filter-with ($in.word | polars is-duplicated)
922-
```
923-
924-
Output
925-
926-
```
927-
╭───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────╮
928-
│ # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word │
929-
├───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────┤
930-
│ 0 │ 2 │ 12 │ 0.20 │ 1.00 │ a │ b │ c │ second │
931-
│ 1 │ 3 │ 13 │ 0.30 │ 2.00 │ a │ b │ c │ third │
932-
│ 2 │ 4 │ 14 │ 0.40 │ 3.00 │ b │ a │ c │ second │
933-
│ 3 │ 0 │ 15 │ 0.50 │ 4.00 │ b │ a │ a │ third │
934-
│ 4 │ 6 │ 16 │ 0.60 │ 5.00 │ b │ a │ a │ second │
935-
│ 5 │ 7 │ 17 │ 0.70 │ 6.00 │ b │ c │ a │ third │
936-
│ 6 │ 9 │ 19 │ 0.90 │ 8.00 │ c │ c │ b │ ninth │
937-
│ 7 │ 0 │ 10 │ 0.00 │ 9.00 │ c │ c │ b │ ninth │
938-
╰───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────╯
897+
# => ╭───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────╮
898+
# => │ # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word │
899+
# => ├───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────┤
900+
# => │ 0 │ 2 │ 12 │ 0.20 │ 1.00 │ a │ b │ c │ second │
901+
# => │ 1 │ 3 │ 13 │ 0.30 │ 2.00 │ a │ b │ c │ third │
902+
# => │ 2 │ 4 │ 14 │ 0.40 │ 3.00 │ b │ a │ c │ second │
903+
# => │ 3 │ 0 │ 15 │ 0.50 │ 4.00 │ b │ a │ a │ third │
904+
# => │ 4 │ 6 │ 16 │ 0.60 │ 5.00 │ b │ a │ a │ second │
905+
# => │ 5 │ 7 │ 17 │ 0.70 │ 6.00 │ b │ c │ a │ third │
906+
# => │ 6 │ 9 │ 19 │ 0.90 │ 8.00 │ c │ c │ b │ ninth │
907+
# => │ 7 │ 0 │ 10 │ 0.00 │ 9.00 │ c │ c │ b │ ninth │
908+
# => ╰───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────╯
939909
```
940910

941911
## Lazy Dataframes
@@ -998,19 +968,14 @@ $lf_0
998968
((polars col a) / 2 | polars as half_a)
999969
]
1000970
| polars collect
1001-
```
1002-
1003-
Output
1004-
1005-
```
1006-
╭───┬───┬───┬──────────┬────────╮
1007-
│ # │ a │ b │ double_a │ half_a │
1008-
├───┼───┼───┼──────────┼────────┤
1009-
│ 0 │ 4 │ d │ 8 │ 2 │
1010-
│ 1 │ 3 │ c │ 6 │ 1 │
1011-
│ 2 │ 2 │ b │ 4 │ 1 │
1012-
│ 3 │ 1 │ a │ 2 │ 0 │
1013-
╰───┴───┴───┴──────────┴────────╯
971+
# => ╭───┬───┬───┬──────────┬────────╮
972+
# => │ # │ a │ b │ double_a │ half_a │
973+
# => ├───┼───┼───┼──────────┼────────┤
974+
# => │ 0 │ 4 │ d │ 8 │ 2 │
975+
# => │ 1 │ 3 │ c │ 6 │ 1 │
976+
# => │ 2 │ 2 │ b │ 4 │ 1 │
977+
# => │ 3 │ 1 │ a │ 2 │ 0 │
978+
# => ╰───┴───┴───┴──────────┴────────╯
1014979
```
1015980

1016981
:::tip
@@ -1072,17 +1037,12 @@ $lf_1
10721037
(polars col value | polars mean | polars as mean)
10731038
]
10741039
| polars collect
1075-
```
1076-
1077-
Output
1078-
1079-
```
1080-
╭───┬──────┬─────┬──────╮
1081-
│ # │ name │ sum │ mean │
1082-
├───┼──────┼─────┼──────┤
1083-
│ 0 │ two │ 5 │ 2.50 │
1084-
│ 1 │ one │ 2 │ 1.00 │
1085-
╰───┴──────┴─────┴──────╯
1040+
# => ╭───┬──────┬─────┬──────╮
1041+
# => │ # │ name │ sum │ mean │
1042+
# => ├───┼──────┼─────┼──────┤
1043+
# => │ 0 │ two │ 5 │ 2.50 │
1044+
# => │ 1 │ one │ 2 │ 1.00 │
1045+
# => ╰───┴──────┴─────┴──────╯
10861046
```
10871047

10881048
And we could join on a lazy dataframe that hasn't being collected. Let's join
@@ -1098,19 +1058,14 @@ let group = $lf_2
10981058
]
10991059
11001060
$lf_2 | polars join $group name name | polars collect
1101-
```
1102-
1103-
Output
1104-
1105-
```
1106-
╭───┬──────┬───────┬─────┬──────╮
1107-
│ # │ name │ value │ sum │ mean │
1108-
├───┼──────┼───────┼─────┼──────┤
1109-
│ 0 │ one │ 1 │ 2 │ 1.00 │
1110-
│ 1 │ two │ 2 │ 5 │ 2.50 │
1111-
│ 2 │ one │ 1 │ 2 │ 1.00 │
1112-
│ 3 │ two │ 3 │ 5 │ 2.50 │
1113-
╰───┴──────┴───────┴─────┴──────╯
1061+
# => ╭───┬──────┬───────┬─────┬──────╮
1062+
# => │ # │ name │ value │ sum │ mean │
1063+
# => ├───┼──────┼───────┼─────┼──────┤
1064+
# => │ 0 │ one │ 1 │ 2 │ 1.00 │
1065+
# => │ 1 │ two │ 2 │ 5 │ 2.50 │
1066+
# => │ 2 │ one │ 1 │ 2 │ 1.00 │
1067+
# => │ 3 │ two │ 3 │ 5 │ 2.50 │
1068+
# => ╰───┴──────┴───────┴─────┴──────╯
11141069
```
11151070

11161071
As you can see lazy frames are a powerful construct that will let you query

cookbook/files.md

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,15 @@ You can parse it into a table.
6161

6262
```nu
6363
open bands.txt | lines | split column ":" Band Album Year | skip 1 | sort-by Year
64-
```
65-
66-
Output
67-
68-
```
69-
━━━┯━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━
70-
# │ Band │ Album │ Year
71-
───┼────────┼────────────────────────┼──────
72-
0 │ Fugazi │ 7 Songs │ 1988
73-
1 │ Fugazi │ Repeater │ 1990
74-
2 │ Fugazi │ Steady Diet of Nothing │ 1991
75-
3 │ Fugazi │ In On The Kill Taker │ 1993
76-
4 │ Fugazi │ The Argument │ 2001
77-
━━━┷━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━
64+
# => ━━━┯━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━
65+
# => # │ Band │ Album │ Year
66+
# => ───┼────────┼────────────────────────┼──────
67+
# => 0 │ Fugazi │ 7 Songs │ 1988
68+
# => 1 │ Fugazi │ Repeater │ 1990
69+
# => 2 │ Fugazi │ Steady Diet of Nothing │ 1991
70+
# => 3 │ Fugazi │ In On The Kill Taker │ 1993
71+
# => 4 │ Fugazi │ The Argument │ 2001
72+
# => ━━━┷━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━
7873
```
7974

8075
You can alternatively do this using `parse`.
@@ -97,24 +92,19 @@ Suppose you would like to check the number of lines the string "Value" appears p
9792

9893
```nu
9994
rg -c Value | lines | split column ":" file line_count | into int line_count | sort-by line_count | reverse
100-
```
101-
102-
Output
103-
104-
```
105-
───┬──────────────────────────────────────┬────────────
106-
# │ file │ line_count
107-
───┼──────────────────────────────────────┼────────────
108-
0 │ crates/nu-source/src/meta.rs │ 27
109-
1 │ crates/nu-protocol/src/value/dict.rs │ 10
110-
2 │ src/commands/config.rs │ 10
111-
3 │ crates/nu_plugin_sys/src/sys.rs │ 10
112-
4 │ src/commands/from_bson.rs │ 9
113-
5 │ src/utils/data_processing.rs │ 9
114-
6 │ src/deserializer.rs │ 8
115-
7 │ src/commands/histogram.rs │ 7
116-
8 │ src/commands/split_column.rs │ 6
117-
9 │ src/data/dict.rs │ 6
118-
───┴──────────────────────────────────────┴────────────
119-
... example output limited due to large output
95+
# => ───┬──────────────────────────────────────┬────────────
96+
# => # │ file │ line_count
97+
# => ───┼──────────────────────────────────────┼────────────
98+
# => 0 │ crates/nu-source/src/meta.rs │ 27
99+
# => 1 │ crates/nu-protocol/src/value/dict.rs │ 10
100+
# => 2 │ src/commands/config.rs │ 10
101+
# => 3 │ crates/nu_plugin_sys/src/sys.rs │ 10
102+
# => 4 │ src/commands/from_bson.rs │ 9
103+
# => 5 │ src/utils/data_processing.rs │ 9
104+
# => 6 │ src/deserializer.rs │ 8
105+
# => 7 │ src/commands/histogram.rs │ 7
106+
# => 8 │ src/commands/split_column.rs │ 6
107+
# => 9 │ src/data/dict.rs │ 6
108+
# => ───┴──────────────────────────────────────┴────────────
109+
# => ... example output limited due to large output
120110
```

0 commit comments

Comments
 (0)