Skip to content

Commit 3a1bb36

Browse files
IndexSeekcpcloud
authored andcommitted
docs(examples): add mean, bucket, histogram examples
1 parent 4492ad2 commit 3a1bb36

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

ibis/expr/types/numeric.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,45 @@ def mean(
887887
-------
888888
NumericScalar
889889
The mean of the input expression
890+
891+
Examples
892+
--------
893+
>>> import ibis
894+
>>> ibis.options.interactive = True
895+
>>> t = ibis.memtable(
896+
... {
897+
... "id": [1, 2, 3, 4, 5, 6],
898+
... "grouper": ["a", "a", "a", "b", "b", "c"],
899+
... "values": [3, 2, 1, 2, 3, 2],
900+
... }
901+
... )
902+
>>> t.mutate(mean_col=t.values.mean())
903+
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┓
904+
┃ id ┃ grouper ┃ values ┃ mean_col ┃
905+
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━┩
906+
│ int64 │ string │ int64 │ float64 │
907+
├───────┼─────────┼────────┼──────────┤
908+
│ 1 │ a │ 3 │ 2.166667 │
909+
│ 2 │ a │ 2 │ 2.166667 │
910+
│ 3 │ a │ 1 │ 2.166667 │
911+
│ 4 │ b │ 2 │ 2.166667 │
912+
│ 5 │ b │ 3 │ 2.166667 │
913+
│ 6 │ c │ 2 │ 2.166667 │
914+
└───────┴─────────┴────────┴──────────┘
915+
916+
>>> t.mutate(mean_col=t.values.mean(where=t.grouper != "c"))
917+
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┓
918+
┃ id ┃ grouper ┃ values ┃ mean_col ┃
919+
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━┩
920+
│ int64 │ string │ int64 │ float64 │
921+
├───────┼─────────┼────────┼──────────┤
922+
│ 1 │ a │ 3 │ 2.2 │
923+
│ 2 │ a │ 2 │ 2.2 │
924+
│ 3 │ a │ 1 │ 2.2 │
925+
│ 4 │ b │ 2 │ 2.2 │
926+
│ 5 │ b │ 3 │ 2.2 │
927+
│ 6 │ c │ 2 │ 2.2 │
928+
└───────┴─────────┴────────┴──────────┘
890929
"""
891930
# TODO(kszucs): remove the alias from the reduction method in favor
892931
# of default name generated by ops.Value operations
@@ -1076,6 +1115,35 @@ def bucket(
10761115
-------
10771116
IntegerColumn
10781117
A categorical column expression
1118+
1119+
Examples
1120+
--------
1121+
>>> import ibis
1122+
>>> ibis.options.interactive = True
1123+
>>> t = ibis.memtable(
1124+
... {
1125+
... "values": [-1, 3, 5, 6, 8, 10, 11],
1126+
... }
1127+
... )
1128+
>>> buckets = [0, 5, 10]
1129+
>>> t.mutate(
1130+
... bucket_closed_left=t.values.bucket(buckets),
1131+
... bucket_closed_right=t.values.bucket(buckets, closed="right"),
1132+
... bucket_over_under=t.values.bucket(buckets, include_over=True, include_under=True),
1133+
... )
1134+
┏━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
1135+
┃ values ┃ bucket_closed_left ┃ bucket_closed_right ┃ bucket_over_under ┃
1136+
┡━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
1137+
│ int64 │ int8 │ int8 │ int8 │
1138+
├────────┼────────────────────┼─────────────────────┼───────────────────┤
1139+
│ -1 │ NULL │ NULL │ 0 │
1140+
│ 3 │ 0 │ 0 │ 1 │
1141+
│ 5 │ 1 │ 0 │ 2 │
1142+
│ 6 │ 1 │ 1 │ 2 │
1143+
│ 8 │ 1 │ 1 │ 2 │
1144+
│ 10 │ 1 │ 1 │ 2 │
1145+
│ 11 │ NULL │ NULL │ 3 │
1146+
└────────┴────────────────────┴─────────────────────┴───────────────────┘
10791147
"""
10801148
return ops.Bucket(
10811149
self,
@@ -1111,6 +1179,53 @@ def histogram(
11111179
-------
11121180
Column
11131181
Bucketed column
1182+
1183+
Examples
1184+
--------
1185+
>>> import ibis
1186+
>>> ibis.options.interactive = True
1187+
>>> t = ibis.memtable(
1188+
... {
1189+
... "values": [-1, 3, 5, 6, 8, 10, 11, 23, 25],
1190+
... }
1191+
... )
1192+
1193+
Compute a histogram with 5 bins.
1194+
1195+
>>> t.mutate(histogram=t.values.histogram(nbins=5))
1196+
┏━━━━━━━━┳━━━━━━━━━━━┓
1197+
┃ values ┃ histogram ┃
1198+
┡━━━━━━━━╇━━━━━━━━━━━┩
1199+
│ int64 │ int64 │
1200+
├────────┼───────────┤
1201+
│ -1 │ 0 │
1202+
│ 3 │ 0 │
1203+
│ 5 │ 1 │
1204+
│ 6 │ 1 │
1205+
│ 8 │ 1 │
1206+
│ 10 │ 2 │
1207+
│ 11 │ 2 │
1208+
│ 23 │ 4 │
1209+
│ 25 │ 4 │
1210+
└────────┴───────────┘
1211+
1212+
Compute a histogram with a fixed bin width of 10.
1213+
>>> t.mutate(histogram=t.values.histogram(binwidth=10))
1214+
┏━━━━━━━━┳━━━━━━━━━━━┓
1215+
┃ values ┃ histogram ┃
1216+
┡━━━━━━━━╇━━━━━━━━━━━┩
1217+
│ int64 │ int64 │
1218+
├────────┼───────────┤
1219+
│ -1 │ 0 │
1220+
│ 3 │ 0 │
1221+
│ 5 │ 0 │
1222+
│ 6 │ 0 │
1223+
│ 8 │ 0 │
1224+
│ 10 │ 1 │
1225+
│ 11 │ 1 │
1226+
│ 23 │ 2 │
1227+
│ 25 │ 2 │
1228+
└────────┴───────────┘
11141229
"""
11151230

11161231
if nbins is not None and binwidth is not None:

0 commit comments

Comments
 (0)