carray 3.0.7 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1940
- data/README.md +38 -32
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +6 -9
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +27 -0
- data/ext/ca_binop_dispatch.h +22 -47
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1506
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +90 -163
- data/ext/ca_obj_bincmp.c +47 -46
- data/ext/ca_obj_binop.c +38 -38
- data/ext/ca_obj_bitarray.c +5 -17
- data/ext/ca_obj_bitfield.c +15 -30
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +32 -30
- data/ext/ca_obj_const_string.c +57 -62
- data/ext/ca_obj_face.c +99 -91
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +15 -21
- data/ext/ca_obj_farray.c +7 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +8 -19
- data/ext/ca_obj_grid.c +137 -106
- data/ext/ca_obj_meld.c +23 -21
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +24 -35
- data/ext/ca_obj_object.c +51 -37
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +8 -10
- data/ext/ca_obj_refer.c +42 -29
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +18 -24
- data/ext/ca_obj_select.c +10 -22
- data/ext/ca_obj_select_axis.c +27 -36
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +28 -24
- data/ext/ca_obj_stride.c +53 -54
- data/ext/ca_obj_string.c +6 -18
- data/ext/ca_obj_tile.c +12 -12
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +35 -39
- data/ext/ca_obj_window.c +67 -86
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +34 -12
- data/ext/ca_op_powi.h +4 -0
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +20 -26
- data/ext/ca_sweep_engine.c +2 -3
- data/ext/ca_sweep_engine.h +17 -9
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +11 -11
- data/ext/carray.h +300 -1011
- data/ext/carray_access.c +234 -180
- data/ext/carray_address_basis.c +33 -38
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +30 -26
- data/ext/carray_call_cfunc.c +169 -277
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +912 -346
- data/ext/carray_cast_func.rb +31 -3
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +43 -38
- data/ext/carray_copy.c +28 -26
- data/ext/carray_core.c +217 -126
- data/ext/carray_count.c +27 -15
- data/ext/carray_element.c +40 -25
- data/ext/carray_factorize.c +53 -52
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +8 -10
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +123 -228
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1274 -160
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +21 -36
- data/ext/carray_loop.c +19 -22
- data/ext/carray_mask.c +123 -145
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +95 -94
- data/ext/carray_memory_view.c +42 -47
- data/ext/carray_operator.c +423 -122
- data/ext/carray_order.c +121 -85
- data/ext/carray_partition.c +13 -15
- data/ext/carray_random.c +47 -41
- data/ext/carray_round.c +9 -4
- data/ext/carray_scatter.c +54 -40
- data/ext/carray_segment.c +41 -45
- data/ext/carray_slab.c +64 -47
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +32 -29
- data/ext/carray_sort_kernel.c +12 -10
- data/ext/carray_struct.c +38 -43
- data/ext/carray_test.c +47 -43
- data/ext/carray_time_text.c +24 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +258 -94
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +19 -20
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +782 -461
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +264 -180
- data/lib/carray/basics.rb +272 -48
- data/lib/carray/bincount_nd.rb +33 -16
- data/lib/carray/block_iterator.rb +123 -102
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +63 -51
- data/lib/carray/categorical_iterator.rb +89 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +49 -22
- data/lib/carray/const_string.rb +54 -62
- data/lib/carray/core_extensions.rb +1 -1
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +25 -24
- data/lib/carray/frame/column_verbs.rb +20 -8
- data/lib/carray/frame/concat.rb +3 -3
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +7 -1
- data/lib/carray/frame/frame.rb +35 -25
- data/lib/carray/frame/group.rb +183 -150
- data/lib/carray/frame/io.rb +26 -7
- data/lib/carray/frame/join.rb +37 -12
- data/lib/carray/frame/records.rb +1 -1
- data/lib/carray/frame/reshape.rb +3 -3
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +148 -118
- data/lib/carray/frame/windows.rb +232 -21
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +24 -21
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +97 -24
- data/lib/carray/key_windows.rb +83 -57
- data/lib/carray/lazy.rb +196 -94
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +5 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +72 -71
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/gather_nd.rb +106 -106
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +165 -145
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +150 -138
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +12 -12
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +18 -17
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +190 -22
- data/lib/carray/segment_iterator.rb +251 -129
- data/lib/carray/serialize.rb +618 -571
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +11 -12
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +76 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +243 -72
- data/lib/carray/time.rb +496 -342
- data/lib/carray/window_iterator.rb +191 -150
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +4 -3
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +7 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +26 -15
- data/yard-stubs/carray_access.rb +51 -4
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +118 -165
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +5 -3
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +157 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +67 -34
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +10 -2
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +62 -24
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25337
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -262
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/frame/group.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# CAFrame group_by (memo §5
|
|
1
|
+
# CAFrame group_by (memo §5 core, §11.6 surface).
|
|
2
2
|
#
|
|
3
3
|
# Grouping is always on the row axis (axis 0). The key is any length-N
|
|
4
4
|
# thing: a column name, several column names (composite key), or an
|
|
@@ -20,12 +20,12 @@ class CAFrame
|
|
|
20
20
|
# df.value_counts("station")
|
|
21
21
|
# df.value_counts("station", "weather", sort: :value)
|
|
22
22
|
#
|
|
23
|
-
# The columns are given as +select+ takes them. +group_by(...).
|
|
23
|
+
# The columns are given as +select+ takes them. +group_by(...).group_sizes+ is the
|
|
24
24
|
# same count in the order of the groups.
|
|
25
25
|
#
|
|
26
26
|
# @return [CAFrame]
|
|
27
27
|
# @raise [ArgumentError] for another +sort:+, or a +name:+ a key has.
|
|
28
|
-
def value_counts
|
|
28
|
+
def value_counts(*names, sort: :count, name: "count")
|
|
29
29
|
unless [:count, :value, false].include?(sort)
|
|
30
30
|
raise ArgumentError, "value_counts: sort: is :count, :value or false (got #{sort.inspect})"
|
|
31
31
|
end
|
|
@@ -40,7 +40,7 @@ class CAFrame
|
|
|
40
40
|
"value_counts: a counted column is named #{name.inspect}; name the count " \
|
|
41
41
|
"otherwise, as in value_counts(..., name: \"n\")"
|
|
42
42
|
end
|
|
43
|
-
out = group_by(*keys).
|
|
43
|
+
out = group_by(*keys).group_sizes(name: name)
|
|
44
44
|
case sort
|
|
45
45
|
when :count then out.sort_by_key(name, descending: true)
|
|
46
46
|
when :value
|
|
@@ -54,11 +54,11 @@ class CAFrame
|
|
|
54
54
|
else out
|
|
55
55
|
end
|
|
56
56
|
rescue ArgumentError, KeyError, TypeError, CArray::DataTypeError => e
|
|
57
|
-
raise e.class, e.message.sub(/\A(group_by|
|
|
57
|
+
raise e.class, e.message.sub(/\A(group_by|group_sizes|sort_by_key):/, "value_counts:"), e.backtrace
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
# Group rows by one or more keys. Each key is a column name (String) or an
|
|
61
|
-
# external length-N CArray. Returns a
|
|
61
|
+
# external length-N CArray. Returns a CAGroupedFrame.
|
|
62
62
|
#
|
|
63
63
|
# With one key, a summary's index is the key's value of each group. With
|
|
64
64
|
# several, each key is a column of a summary, holding its part of each
|
|
@@ -70,11 +70,11 @@ class CAFrame
|
|
|
70
70
|
cat = grouping_categorical(keys)
|
|
71
71
|
if keys.size == 1
|
|
72
72
|
axis = keys.first.is_a?(String) ? keys.first : "group"
|
|
73
|
-
return
|
|
73
|
+
return CAGroupedFrame.new(self, cat, axis, keys.grep(String), key: key_column(keys.first))
|
|
74
74
|
end
|
|
75
75
|
names = keys.each_with_index.map { |k, i| k.is_a?(String) ? k : "key#{i}" }
|
|
76
76
|
CAFrame.__send__(:refuse_repeated_names, names, "group_by")
|
|
77
|
-
|
|
77
|
+
CAGroupedFrame.new(self, cat, "group", keys.grep(String),
|
|
78
78
|
key_columns: names.zip(keys.map { |k| key_column(k) }).to_h)
|
|
79
79
|
end
|
|
80
80
|
|
|
@@ -128,10 +128,10 @@ class CAFrame
|
|
|
128
128
|
end
|
|
129
129
|
|
|
130
130
|
# Group rows into time bins of length +unit+ along the time column (or the
|
|
131
|
-
# time index) +name+, for the reductions
|
|
131
|
+
# time index) +name+, for the reductions CAGroupedFrame offers:
|
|
132
132
|
#
|
|
133
133
|
# df.resample("time", "1 hour").mean
|
|
134
|
-
# df.resample("time", "1 day",
|
|
134
|
+
# df.resample("time", "1 day", keep_empty: true).aggregate("rain" => ["rain", :sum])
|
|
135
135
|
#
|
|
136
136
|
# The result's index is the bin labels as a CATime, in time order, and its
|
|
137
137
|
# row axis is named after +name+.
|
|
@@ -143,16 +143,16 @@ class CAFrame
|
|
|
143
143
|
# describes the hour before it. +origin:+ shifts the bins as +CATime#floor+
|
|
144
144
|
# does ("1 hour" with origin 00:30 gives 00:30, 01:30, ...).
|
|
145
145
|
#
|
|
146
|
-
# Without +
|
|
147
|
-
# every bin from the first to the last is a row, and an
|
|
148
|
-
# as an empty reduction does: UNDEF for +mean+, 0 for +count+ and +sum+.
|
|
146
|
+
# Without +keep_empty:+, a bin with no rows does not appear. With
|
|
147
|
+
# +keep_empty: true+ every bin from the first to the last is a row, and an
|
|
148
|
+
# empty one reduces as an empty reduction does: UNDEF for +mean+, 0 for +count+ and +sum+.
|
|
149
149
|
# A row whose time is masked belongs to no bin.
|
|
150
150
|
#
|
|
151
151
|
# +by:+ (a column name, a CArray key or an Array of them) bins each group of rows on its
|
|
152
152
|
# own: a group is the rows sharing the +by:+ values and a bin. The result
|
|
153
153
|
# then has no index: the +by:+ columns and the bin times (named +name+)
|
|
154
154
|
# are its first columns, the groups in the order their +by:+ values first
|
|
155
|
-
# appear and the bins of each in time order. With +
|
|
155
|
+
# appear and the bins of each in time order. With +keep_empty: true+ every group
|
|
156
156
|
# has every bin from the first to the last of the whole frame. A row whose
|
|
157
157
|
# +by:+ value is masked belongs to no group.
|
|
158
158
|
#
|
|
@@ -162,14 +162,18 @@ class CAFrame
|
|
|
162
162
|
# @param unit [String, Symbol, CATime::Resolution] the bin length.
|
|
163
163
|
# @param origin [nil, Time, String, CATime::Element] where bins start.
|
|
164
164
|
# @param label [:left, :right] which end of a bin names it and is closed.
|
|
165
|
-
# @param
|
|
165
|
+
# @param keep_empty [Boolean] whether bins with no rows are rows of the
|
|
166
|
+
# result.
|
|
166
167
|
# @param by [nil, String, CArray, Array<String, CArray>] columns whose groups
|
|
167
168
|
# are binned apart; a CArray key (one value per row, as group_by takes it)
|
|
168
169
|
# is labelled "key0", "key1", ... by its place.
|
|
169
|
-
# @return [
|
|
170
|
+
# @return [CAGroupedFrame]
|
|
170
171
|
# @raise [ArgumentError] when the column is not a CATime, or +label:+ is
|
|
171
172
|
# neither :left nor :right, or +by:+ names the time column.
|
|
172
|
-
def resample(name, unit, origin: nil, label: :left,
|
|
173
|
+
def resample(name, unit, origin: nil, label: :left, keep_empty: false, by: nil)
|
|
174
|
+
keep_empty = CArray.send(:bool_arg, keep_empty, "keep_empty", "resample")
|
|
175
|
+
# A Grid is read as its (unit, origin) pair, which every step below takes.
|
|
176
|
+
unit, origin = CATime::Grid.resolve(unit, origin, "resample")
|
|
173
177
|
name = column_key(name, "resample")
|
|
174
178
|
time = key_column(name, "resample")
|
|
175
179
|
unless time.is_a?(CATime)
|
|
@@ -183,10 +187,10 @@ class CAFrame
|
|
|
183
187
|
raise ArgumentError, "resample: label: must be :left or :right (got #{label.inspect})"
|
|
184
188
|
end
|
|
185
189
|
# key-labels: the bins are the index, given the column's attributes below.
|
|
186
|
-
grid =
|
|
187
|
-
return resample_by(name, time, bins, grid, by,
|
|
190
|
+
grid = keep_empty && bins.count_not_masked > 0 ? resample_grid(bins, unit) : bins.unique(sort: true)
|
|
191
|
+
return resample_by(name, time, bins, grid, by, keep_empty) unless by.nil?
|
|
188
192
|
cat = CACategorical.from_codes(bins.locate_addr(grid), grid.to_a)
|
|
189
|
-
|
|
193
|
+
CAGroupedFrame.new(self, cat, name, @columns.key?(name) ? [name] : [],
|
|
190
194
|
index: CAFrame.__send__(:key_labels, time, grid))
|
|
191
195
|
end
|
|
192
196
|
|
|
@@ -195,7 +199,7 @@ class CAFrame
|
|
|
195
199
|
# by values first appear and through each group's bins in time order.
|
|
196
200
|
# A by: key is a column name or, as group_by takes it, a CArray of one
|
|
197
201
|
# value per row; a CArray key is labelled "key0", "key1", ... by its place.
|
|
198
|
-
private def resample_by(name, time, bins, grid, by,
|
|
202
|
+
private def resample_by(name, time, bins, grid, by, keep_empty)
|
|
199
203
|
keys = by.is_a?(Array) ? by : [by]
|
|
200
204
|
raise ArgumentError, "resample: by: names no column" if keys.empty?
|
|
201
205
|
keys.each { |k| column_key(k, "resample") unless k.is_a?(CArray) }
|
|
@@ -210,8 +214,8 @@ class CAFrame
|
|
|
210
214
|
nb = grid.elements
|
|
211
215
|
comb = gcat.codes.int64 * nb + bins.locate_addr(grid).int64
|
|
212
216
|
# key-labels: the used (group, bin) codes, not labels; those go through key_labels below
|
|
213
|
-
occupied =
|
|
214
|
-
codes =
|
|
217
|
+
occupied = keep_empty ? CArray.int64(ng * nb).seq! : comb.unique(sort: true)
|
|
218
|
+
codes = keep_empty ? comb : comb.locate_addr(occupied)
|
|
215
219
|
g_of = nb > 0 ? occupied / nb : occupied
|
|
216
220
|
b_of = nb > 0 ? occupied % nb : occupied
|
|
217
221
|
# Each group's by values, from its first row.
|
|
@@ -231,14 +235,14 @@ class CAFrame
|
|
|
231
235
|
end
|
|
232
236
|
cat = CACategorical.from_codes(codes, labels)
|
|
233
237
|
key_names = keys.grep(String) + (@columns.key?(name) ? [name] : [])
|
|
234
|
-
|
|
238
|
+
CAGroupedFrame.new(self, cat, "group", key_names, label_columns: columns)
|
|
235
239
|
end
|
|
236
240
|
|
|
237
241
|
# Every bin from the first occupied one to the last, stepped from the
|
|
238
242
|
# first bin so an origin's phase (or label: :right) is kept. A month or a
|
|
239
243
|
# year cannot be stepped on a finer grid, so it is stepped on the one-month
|
|
240
244
|
# or one-year grid and then given the bins' unit, as the labels without
|
|
241
|
-
#
|
|
245
|
+
# keep_empty: are.
|
|
242
246
|
private def resample_grid(bins, unit)
|
|
243
247
|
res = CATime::Resolution.parse(unit)
|
|
244
248
|
if %i[Y M].include?(res.base)
|
|
@@ -247,7 +251,7 @@ class CAFrame
|
|
|
247
251
|
# A key coarser than the bins holds one bin for several grid points.
|
|
248
252
|
# key-labels: the bins of a grid, labelled by the caller
|
|
249
253
|
grid.to_unit(bins.unit).unique(sort: true)
|
|
250
|
-
elsif
|
|
254
|
+
elsif CATime::UnitAlgebra.ratio(res, bins.unit).denominator != 1
|
|
251
255
|
# A step finer than the key is stepped in its own unit; the key holds
|
|
252
256
|
# one bin for several of its points.
|
|
253
257
|
grid = CArray.time_range(bins.min, bins.max, unit: res.base, step: unit)
|
|
@@ -303,7 +307,7 @@ class CAFrame
|
|
|
303
307
|
end
|
|
304
308
|
|
|
305
309
|
# Each column reduced over all the rows, as a frame of one row: the
|
|
306
|
-
# reductions of +
|
|
310
|
+
# reductions of +CAGroupedFrame+ with every row in one group. Given names, the
|
|
307
311
|
# frame of those columns; without, of every column whose type has the
|
|
308
312
|
# reduction. A column keeps its type where the reduction keeps it (the
|
|
309
313
|
# earliest time of a time column is a time), and +min_count:+ /
|
|
@@ -346,10 +350,12 @@ class CAFrame
|
|
|
346
350
|
# @return [CAFrame] one row: each column's number of distinct present values.
|
|
347
351
|
# @!method count(*names, keep_rows: false)
|
|
348
352
|
# @return [CAFrame] one row: each column's number of present values.
|
|
349
|
-
# @!method all(*names, keep_rows: false)
|
|
350
|
-
# @return [CAFrame] one row, for the boolean columns
|
|
351
|
-
#
|
|
352
|
-
#
|
|
353
|
+
# @!method all(*names, keep_rows: false, skip_masked: true)
|
|
354
|
+
# @return [CAFrame] one row, for the boolean columns; +skip_masked:+ as
|
|
355
|
+
# +CArray#all+ takes it.
|
|
356
|
+
# @!method any(*names, keep_rows: false, skip_masked: true)
|
|
357
|
+
# @return [CAFrame] one row, for the boolean columns; +skip_masked:+ as
|
|
358
|
+
# +CArray#any+ takes it.
|
|
353
359
|
CArray.send(:define_named_methods, self,
|
|
354
360
|
[:sum, :mean, :min, :max, :prod, :accumulate, :median,
|
|
355
361
|
:variance, :stddev, :variancep, :stddevp], :reduce_all_rows)
|
|
@@ -368,8 +374,9 @@ class CAFrame
|
|
|
368
374
|
# a percentile.
|
|
369
375
|
# @return [CAFrame, CArray] one row; with +keep_rows: true+ the frame's
|
|
370
376
|
# rows, one name giving the CArray.
|
|
371
|
-
def percentile
|
|
372
|
-
|
|
377
|
+
def percentile(p, *names, min_count: nil, fill_value: nil, keep_rows: false)
|
|
378
|
+
keep_rows = CArray.send(:bool_arg, keep_rows, "keep_rows", "percentile")
|
|
379
|
+
p = CAFrame.__send__(:check_percentage, p)
|
|
373
380
|
reduce_all_rows(:percentile, *names, min_count: min_count, fill_value: fill_value,
|
|
374
381
|
keep_rows: keep_rows, args: [p])
|
|
375
382
|
end
|
|
@@ -383,11 +390,19 @@ class CAFrame
|
|
|
383
390
|
#
|
|
384
391
|
# @param names [Array<String>] the columns; default every column that has
|
|
385
392
|
# a quantile.
|
|
393
|
+
# @param min_count [Integer, nil] as for {#percentile}.
|
|
394
|
+
# @param fill_value [Object, Hash, nil] as for {#percentile}.
|
|
386
395
|
# @return [CAFrame]
|
|
387
|
-
def quantile
|
|
396
|
+
def quantile(*names, min_count: nil, fill_value: nil, keep_rows: false)
|
|
397
|
+
keep_rows = CArray.send(:bool_arg, keep_rows, "keep_rows", "quantile")
|
|
398
|
+
walk = ColumnWalk.new(self)
|
|
399
|
+
walk.__send__(:refuse_unknown_fill_columns, :quantile, fill_value)
|
|
400
|
+
walk.__send__(:refuse_wrong_reduction_arguments, :quantile, [], { min_count: min_count }.compact)
|
|
388
401
|
named = {}
|
|
389
|
-
|
|
390
|
-
|
|
402
|
+
walk.over(:quantile, names) do |col, name|
|
|
403
|
+
fill = fill_value.is_a?(Hash) ? fill_value[name] : fill_value
|
|
404
|
+
kw = { min_count: min_count, fill_value: fill }.compact
|
|
405
|
+
named[name] = col.quantile(axis: 0, keep_axis: true, **kw).map do |q|
|
|
391
406
|
keep_rows ? q[CArray.int64(@nrow).fill(0), *([nil] * (q.ndim - 1))].copy : q
|
|
392
407
|
end
|
|
393
408
|
end
|
|
@@ -407,7 +422,7 @@ class CAFrame
|
|
|
407
422
|
QUANTILE_SUFFIXES = %w[p0 p25 p50 p75 p100].freeze
|
|
408
423
|
private_constant :QUANTILE_SUFFIXES
|
|
409
424
|
|
|
410
|
-
private def reduce_all_rows
|
|
425
|
+
private def reduce_all_rows(reduction, *names, min_count: nil, fill_value: nil, keep_rows: false, args: [])
|
|
411
426
|
walk = ColumnWalk.new(self)
|
|
412
427
|
walk.__send__(:refuse_unknown_fill_columns, reduction, fill_value)
|
|
413
428
|
walk.__send__(:refuse_wrong_reduction_arguments, reduction, args,
|
|
@@ -418,8 +433,9 @@ class CAFrame
|
|
|
418
433
|
end
|
|
419
434
|
end
|
|
420
435
|
|
|
421
|
-
private def reduce_all_rows_to_a_value
|
|
422
|
-
|
|
436
|
+
private def reduce_all_rows_to_a_value(reduction, *names, keep_rows: false, **keywords)
|
|
437
|
+
keywords = ColumnWalk.new(self).__send__(:value_reduction_keywords, reduction, keywords)
|
|
438
|
+
return reduce_down_the_rows(reduction, names, keep_rows) { |_| keywords }
|
|
423
439
|
end
|
|
424
440
|
|
|
425
441
|
# Each named column, or every column whose type has the reduction,
|
|
@@ -427,7 +443,7 @@ class CAFrame
|
|
|
427
443
|
# it keeps its type as the column's reduction keeps it. The block gives a
|
|
428
444
|
# column's keywords. A frame of one row with no index; with +keep_rows+,
|
|
429
445
|
# each row holding the result, as a group's +keep_rows+ gives it.
|
|
430
|
-
private def reduce_down_the_rows
|
|
446
|
+
private def reduce_down_the_rows(reduction, names, keep_rows, args = [])
|
|
431
447
|
meth = reduction == :count ? :count_not_masked : reduction
|
|
432
448
|
named = {}
|
|
433
449
|
out = ColumnWalk.new(self).over(reduction, names, [], args) do |col, name|
|
|
@@ -442,7 +458,7 @@ class CAFrame
|
|
|
442
458
|
return CAFrame.new(named, axis_name: @axis_name, index: @index&.copy)
|
|
443
459
|
end
|
|
444
460
|
|
|
445
|
-
# Values computed down the rows, one per row, as +
|
|
461
|
+
# Values computed down the rows, one per row, as +CAGroupedFrame+ computes
|
|
446
462
|
# them within each group, with every row in one group: each row's rank
|
|
447
463
|
# (+order+, int64, 0 the smallest) and the running values up to and
|
|
448
464
|
# including it. Given names, one name gives the CArray and several a frame
|
|
@@ -450,9 +466,9 @@ class CAFrame
|
|
|
450
466
|
# the operation. A frame is on the frame's rows and index.
|
|
451
467
|
#
|
|
452
468
|
# df["total"] = df.cumsum("rain")
|
|
453
|
-
# df["rank"] = df.
|
|
469
|
+
# df["rank"] = df.rank_index("temp", descending: true)
|
|
454
470
|
#
|
|
455
|
-
# @!method
|
|
471
|
+
# @!method rank_index(*names, descending: false, method: :ordinal, kind: :quick)
|
|
456
472
|
# @return [CAFrame, CArray]
|
|
457
473
|
# @!method cumsum(*names)
|
|
458
474
|
# @return [CAFrame, CArray]
|
|
@@ -465,9 +481,9 @@ class CAFrame
|
|
|
465
481
|
# @!method cumcount(*names)
|
|
466
482
|
# @return [CAFrame, CArray]
|
|
467
483
|
CArray.send(:define_named_methods, self,
|
|
468
|
-
[:
|
|
484
|
+
[:rank_index, :cumsum, :cumprod, :cummax, :cummin, :cumcount], :run_down_all_rows)
|
|
469
485
|
|
|
470
|
-
private def run_down_all_rows
|
|
486
|
+
private def run_down_all_rows(operation, *names, **keywords)
|
|
471
487
|
# The keywords are the core's to check, before any column is read.
|
|
472
488
|
CArray.float64(1).public_send(operation, **keywords)
|
|
473
489
|
out = ColumnWalk.new(self).over(operation, names, [], [], keywords) do |col, _|
|
|
@@ -484,11 +500,11 @@ class CAFrame
|
|
|
484
500
|
class ColumnWalk
|
|
485
501
|
include CAFrame::ColumnVerbs
|
|
486
502
|
|
|
487
|
-
def initialize
|
|
503
|
+
def initialize(frame)
|
|
488
504
|
@frame = frame
|
|
489
505
|
end
|
|
490
506
|
|
|
491
|
-
def over
|
|
507
|
+
def over(verb, names, key_names = [], args = [], keywords = {}, &block)
|
|
492
508
|
return over_named_columns(verb, names, key_names, args, keywords, &block)
|
|
493
509
|
end
|
|
494
510
|
end
|
|
@@ -508,7 +524,7 @@ class CAFrame
|
|
|
508
524
|
# df["dtemp"] = df["temp"] - df.lag("temp", before: "1 hour")
|
|
509
525
|
# df.lag("temp", n: 1) # the previous row
|
|
510
526
|
#
|
|
511
|
-
# +
|
|
527
|
+
# +CAGroupedFrame#lag+ does the same within each group. Given names, one
|
|
512
528
|
# name gives the CArray and several a frame of those columns; without
|
|
513
529
|
# names, a frame of every column (the +on:+ column left out). A frame is
|
|
514
530
|
# on the frame's rows and index. A result shows the column's attributes, as +CArray#lag+'s
|
|
@@ -524,7 +540,7 @@ class CAFrame
|
|
|
524
540
|
# @raise [ArgumentError] when +n:+, +before:+ and +after:+ are combined,
|
|
525
541
|
# when +on:+ comes without a distance, when there is no key, or when the
|
|
526
542
|
# key repeats (the row at a key would not be one row).
|
|
527
|
-
def lag
|
|
543
|
+
def lag(*names, n: nil, before: nil, after: nil, on: nil)
|
|
528
544
|
distance = lag_distance("lag", n, before, after, on)
|
|
529
545
|
walk = ColumnWalk.new(self)
|
|
530
546
|
if distance.nil?
|
|
@@ -545,7 +561,7 @@ class CAFrame
|
|
|
545
561
|
end
|
|
546
562
|
|
|
547
563
|
# [key name, key, sign, width] for a lag by distance; nil for one by rows.
|
|
548
|
-
private def lag_distance
|
|
564
|
+
private def lag_distance(verb, n, before, after, on)
|
|
549
565
|
if before.nil? && after.nil?
|
|
550
566
|
raise ArgumentError, "#{verb}: on: goes with before: or after:" unless on.nil?
|
|
551
567
|
return nil
|
|
@@ -579,7 +595,7 @@ class CAFrame
|
|
|
579
595
|
# key moved by the width; masked where there is none. A row in no set is
|
|
580
596
|
# masked too, and so is a row whose key is NaN or infinite, which is no
|
|
581
597
|
# distance from any key (as in windows).
|
|
582
|
-
private def lag_addresses
|
|
598
|
+
private def lag_addresses(verb, row_sets, _on_name, key, sign, width)
|
|
583
599
|
key = key.mask_invalid if key.float?
|
|
584
600
|
addr = CArray.int64(nrow)
|
|
585
601
|
addr[] = UNDEF
|
|
@@ -598,10 +614,10 @@ class CAFrame
|
|
|
598
614
|
|
|
599
615
|
# The keys moved by the width. A time is moved on the finer of its unit and
|
|
600
616
|
# the width's, so that an hour back from a day is not rounded onto the day.
|
|
601
|
-
private def lag_target
|
|
617
|
+
private def lag_target(keys, sign, width)
|
|
602
618
|
if width.is_a?(CATimedelta::Element) &&
|
|
603
|
-
|
|
604
|
-
keys = keys.to_unit(
|
|
619
|
+
CATime::UnitAlgebra.same_group?(keys.unit.base, width.unit.base)
|
|
620
|
+
keys = keys.to_unit(CATime::UnitAlgebra.finer(keys.unit, width.unit))
|
|
605
621
|
end
|
|
606
622
|
return lag_integer_target(keys, sign, width) if keys.integer?
|
|
607
623
|
return keys.public_send(sign, width)
|
|
@@ -614,7 +630,7 @@ class CAFrame
|
|
|
614
630
|
# for a 64-bit key a step does not fit in). A target outside the key's
|
|
615
631
|
# range is masked, so it matches no row rather than wrapping onto one; a
|
|
616
632
|
# width that is not a whole number matches no row either.
|
|
617
|
-
private def lag_integer_target
|
|
633
|
+
private def lag_integer_target(keys, sign, width)
|
|
618
634
|
bits = keys.bytes * 8
|
|
619
635
|
if LAG_UNSIGNED.include?(keys.data_type)
|
|
620
636
|
lo, hi = 0, 2**bits - 1
|
|
@@ -645,7 +661,7 @@ class CAFrame
|
|
|
645
661
|
|
|
646
662
|
end
|
|
647
663
|
|
|
648
|
-
#
|
|
664
|
+
# CAGroupedFrame — the result of +CAFrame#group_by+ (memo §11.6, §16).
|
|
649
665
|
#
|
|
650
666
|
# Holds the grouping categorical once (grouping is per-key, computed once
|
|
651
667
|
# and shared across every column). The surfaces:
|
|
@@ -653,7 +669,7 @@ end
|
|
|
653
669
|
# aggregate(spec) -> declarative per-column reductions into a new frame
|
|
654
670
|
# aggregate { |grp| } -> columns built from iterators, run once
|
|
655
671
|
# table { |g| } -> Ruby escape, g is a per-group view-frame
|
|
656
|
-
class
|
|
672
|
+
class CAGroupedFrame
|
|
657
673
|
include CAFrame::ColumnVerbs
|
|
658
674
|
# +key_names+ are the frame columns the grouping was keyed on. They become
|
|
659
675
|
# the result's index, so the reduction shortcuts must not also return them as
|
|
@@ -668,7 +684,7 @@ class GroupedFrame
|
|
|
668
684
|
# key, one value per row) become its first columns, each holding the key's
|
|
669
685
|
# value at each group's first row; +label_columns:+ (name => one value per
|
|
670
686
|
# group, in code order) give them directly, for groups that may have no
|
|
671
|
-
# rows (resample with
|
|
687
|
+
# rows (resample with keep_empty: true).
|
|
672
688
|
#
|
|
673
689
|
# The grouping and the index are taken here, so a later change to the key
|
|
674
690
|
# does not rename the groups; the values are read when a reduction is.
|
|
@@ -686,24 +702,24 @@ class GroupedFrame
|
|
|
686
702
|
end
|
|
687
703
|
|
|
688
704
|
# Number of groups.
|
|
689
|
-
def
|
|
705
|
+
def ngroups
|
|
690
706
|
@labels.size
|
|
691
707
|
end
|
|
692
708
|
|
|
693
709
|
# The number of rows in each group, as a frame of one column, "size"
|
|
694
710
|
# unless +name:+ says otherwise, labelled as a reduction's result is. A row
|
|
695
|
-
#
|
|
696
|
-
# frame's nrow.
|
|
711
|
+
# counts whatever its values are, masked or not; a row whose key is masked
|
|
712
|
+
# is in no group, so the sizes need not add up to the frame's nrow.
|
|
697
713
|
#
|
|
698
714
|
# @param name [String] the result column's name.
|
|
699
715
|
# @return [CAFrame]
|
|
700
716
|
# @raise [ArgumentError] when a key has that name.
|
|
701
|
-
def
|
|
702
|
-
refuse_changed_rows("
|
|
703
|
-
name = @frame.__send__(:column_name_arg, name, "
|
|
717
|
+
def group_sizes(name: "size")
|
|
718
|
+
refuse_changed_rows("group_sizes")
|
|
719
|
+
name = @frame.__send__(:column_name_arg, name, "group_sizes")
|
|
704
720
|
if @key_names.include?(name) || @axis_name == name
|
|
705
721
|
raise ArgumentError,
|
|
706
|
-
"
|
|
722
|
+
"group_sizes: a key is named #{name.inspect}; name the result otherwise, as in group_sizes(name: \"n\")"
|
|
707
723
|
end
|
|
708
724
|
return result_frame({ name => @cat.category_sizes.int64 }, false)
|
|
709
725
|
end
|
|
@@ -716,11 +732,11 @@ class GroupedFrame
|
|
|
716
732
|
#
|
|
717
733
|
# @yieldparam label [Object, Array] the group's key value(s).
|
|
718
734
|
# @yieldparam rows [CAFrame] the group's rows.
|
|
719
|
-
# @return [
|
|
735
|
+
# @return [CAGroupedFrame] self
|
|
720
736
|
def each
|
|
721
|
-
return enum_for(:each) {
|
|
737
|
+
return enum_for(:each) { ngroups } unless block_given?
|
|
722
738
|
refuse_changed_rows("each")
|
|
723
|
-
|
|
739
|
+
ngroups.times { |k| yield @labels[k], @frame[group_address(k)] }
|
|
724
740
|
return self
|
|
725
741
|
end
|
|
726
742
|
|
|
@@ -799,6 +815,7 @@ class GroupedFrame
|
|
|
799
815
|
# @raise [ArgumentError] when both a spec and a block are given, or the
|
|
800
816
|
# block returns something other than a Hash of per-group CArrays.
|
|
801
817
|
def aggregate(spec = nil, keep_rows: false, **named, &block)
|
|
818
|
+
keep_rows = CArray.send(:bool_arg, keep_rows, "keep_rows", "aggregate")
|
|
802
819
|
refuse_changed_rows("aggregate")
|
|
803
820
|
if block
|
|
804
821
|
unless spec.nil? && named.empty?
|
|
@@ -844,6 +861,7 @@ class GroupedFrame
|
|
|
844
861
|
# first appear; a group whose Hash lacks a name is UNDEF there, as
|
|
845
862
|
# +from_records+ reads a record that lacks a key.
|
|
846
863
|
def table(keep_rows: false)
|
|
864
|
+
keep_rows = CArray.send(:bool_arg, keep_rows, "keep_rows", "table")
|
|
847
865
|
raise ArgumentError, "table: give a block" unless block_given?
|
|
848
866
|
refuse_changed_rows("table")
|
|
849
867
|
outs = []
|
|
@@ -932,7 +950,7 @@ class GroupedFrame
|
|
|
932
950
|
[:sum, :mean, :min, :max, :prod, :accumulate, :median,
|
|
933
951
|
:variance, :stddev, :variancep, :stddevp], :group_reduction)
|
|
934
952
|
|
|
935
|
-
private def group_reduction
|
|
953
|
+
private def group_reduction(red, *names, min_count: nil, fill_value: nil, keep_rows: false)
|
|
936
954
|
refuse_changed_rows(red)
|
|
937
955
|
reduce_columns(red, names, keep_rows, min_count: min_count, fill_value: fill_value)
|
|
938
956
|
end
|
|
@@ -946,8 +964,9 @@ class GroupedFrame
|
|
|
946
964
|
# @param names [Array<String>] the columns; default every column that has
|
|
947
965
|
# a percentile.
|
|
948
966
|
# @return [CAFrame, CArray] one row per group, as {#sum} returns.
|
|
949
|
-
def percentile
|
|
950
|
-
|
|
967
|
+
def percentile(p, *names, min_count: nil, fill_value: nil, keep_rows: false)
|
|
968
|
+
keep_rows = CArray.send(:bool_arg, keep_rows, "keep_rows", "percentile")
|
|
969
|
+
p = CAFrame.__send__(:check_percentage, p)
|
|
951
970
|
refuse_changed_rows(:percentile)
|
|
952
971
|
reduce_columns(:percentile, names, keep_rows, args: [p], min_count: min_count, fill_value: fill_value)
|
|
953
972
|
end
|
|
@@ -961,13 +980,20 @@ class GroupedFrame
|
|
|
961
980
|
#
|
|
962
981
|
# @param names [Array<String>] the columns; default every column that has
|
|
963
982
|
# a quantile.
|
|
983
|
+
# @param min_count [Integer, nil] as for {#percentile}.
|
|
984
|
+
# @param fill_value [Object, Hash, nil] as for {#percentile}.
|
|
964
985
|
# @return [CAFrame]
|
|
965
|
-
def quantile
|
|
986
|
+
def quantile(*names, min_count: nil, fill_value: nil, keep_rows: false)
|
|
987
|
+
keep_rows = CArray.send(:bool_arg, keep_rows, "keep_rows", "quantile")
|
|
966
988
|
refuse_changed_rows(:quantile)
|
|
967
989
|
refuse_key_as_result(:quantile, names) unless keep_rows
|
|
990
|
+
refuse_unknown_fill_columns(:quantile, fill_value)
|
|
991
|
+
refuse_wrong_reduction_arguments(:quantile, [], { min_count: min_count }.compact)
|
|
968
992
|
named = {}
|
|
969
993
|
over_named_columns(:quantile, names, @key_names) do |col, name|
|
|
970
|
-
|
|
994
|
+
fill = fill_value.is_a?(Hash) ? fill_value[name] : fill_value
|
|
995
|
+
kw = { min_count: min_count, fill_value: fill }.compact
|
|
996
|
+
named[name] = group_reduce(col, :quantile, **kw).map { |q| keep_rows ? spread_to_rows(q) : q }
|
|
971
997
|
end
|
|
972
998
|
cols = CAFrame.__send__(:quantile_columns, named)
|
|
973
999
|
return result_frame(cols, false) unless keep_rows
|
|
@@ -992,20 +1018,22 @@ class GroupedFrame
|
|
|
992
1018
|
# Returns each group's number of present values (int64), a masked cell
|
|
993
1019
|
# left out.
|
|
994
1020
|
# @return [CAFrame] one row per group.
|
|
995
|
-
# @!method all(*names, keep_rows: false)
|
|
1021
|
+
# @!method all(*names, keep_rows: false, skip_masked: true)
|
|
996
1022
|
# Returns whether every present value of the group is true, for the
|
|
997
|
-
# boolean columns.
|
|
1023
|
+
# boolean columns. With +skip_masked: false+ a group is UNDEF when a
|
|
1024
|
+
# masked cell could change it, as +CArray#all+ folds it.
|
|
998
1025
|
# @return [CAFrame] one row per group.
|
|
999
|
-
# @!method any(*names, keep_rows: false)
|
|
1026
|
+
# @!method any(*names, keep_rows: false, skip_masked: true)
|
|
1000
1027
|
# Returns whether any present value of the group is true, for the
|
|
1001
|
-
# boolean columns
|
|
1028
|
+
# boolean columns; +skip_masked:+ as for +all+.
|
|
1002
1029
|
# @return [CAFrame] one row per group.
|
|
1003
1030
|
CArray.send(:define_named_methods, self,
|
|
1004
1031
|
[:first, :last, :nunique, :count, :all, :any], :group_value)
|
|
1005
1032
|
|
|
1006
|
-
private def group_value
|
|
1033
|
+
private def group_value(red, *names, keep_rows: false, **keywords)
|
|
1034
|
+
keywords = value_reduction_keywords(red, keywords)
|
|
1007
1035
|
refuse_changed_rows(red)
|
|
1008
|
-
reduce_columns(red, names, keep_rows)
|
|
1036
|
+
reduce_columns(red, names, keep_rows, **keywords)
|
|
1009
1037
|
end
|
|
1010
1038
|
|
|
1011
1039
|
# Values computed within each group and kept on the frame's rows, in row
|
|
@@ -1017,7 +1045,7 @@ class GroupedFrame
|
|
|
1017
1045
|
# columns (several) on the frame's rows and index. An N-D column runs along the rows.
|
|
1018
1046
|
#
|
|
1019
1047
|
# df["dtemp"] = df["temp"] - grp.lag("temp") # change since the station's previous row
|
|
1020
|
-
# grp.
|
|
1048
|
+
# grp.rank_index("temp", descending: true) # rank within the station
|
|
1021
1049
|
# grp.cumsum("rain") # running total per station
|
|
1022
1050
|
#
|
|
1023
1051
|
# "Previous" is the previous row of the frame: sort the frame first
|
|
@@ -1030,9 +1058,9 @@ class GroupedFrame
|
|
|
1030
1058
|
# (the index, or the column +on:+ names) is that distance earlier (or
|
|
1031
1059
|
# later), as +CAFrame#lag+ gives it.
|
|
1032
1060
|
# @return [CAFrame, CArray]
|
|
1033
|
-
# @!method
|
|
1061
|
+
# @!method rank_index(*names, descending: false, method: :ordinal, kind: :quick)
|
|
1034
1062
|
# Returns each row's rank among its group's rows (int64, 0 the
|
|
1035
|
-
# smallest), as +CArray#
|
|
1063
|
+
# smallest), as +CArray#rank_index+ ranks them.
|
|
1036
1064
|
# @return [CAFrame, CArray]
|
|
1037
1065
|
# @!method cumsum(*names)
|
|
1038
1066
|
# Returns the running sum within each group, as +CArray#cumsum+.
|
|
@@ -1049,12 +1077,12 @@ class GroupedFrame
|
|
|
1049
1077
|
# @!method cumcount(*names)
|
|
1050
1078
|
# Returns the running count of present values within each group (int64).
|
|
1051
1079
|
# @return [CAFrame, CArray]
|
|
1052
|
-
def lag
|
|
1080
|
+
def lag(*names, n: nil, before: nil, after: nil, on: nil)
|
|
1053
1081
|
distance = @frame.__send__(:lag_distance, "lag", n, before, after, on)
|
|
1054
1082
|
return transform_columns(:lag, names, [n.nil? ? 1 : n], {}) if distance.nil?
|
|
1055
1083
|
refuse_changed_rows("lag")
|
|
1056
1084
|
refuse_key_on_rows(:lag, names)
|
|
1057
|
-
addr = @frame.__send__(:lag_addresses, "lag", (0...
|
|
1085
|
+
addr = @frame.__send__(:lag_addresses, "lag", (0...ngroups).map { |k| group_address(k) }, *distance)
|
|
1058
1086
|
out = over_named_columns(:lag, names, @key_names | [distance.first], [1], {}) do |col|
|
|
1059
1087
|
col[addr, *([nil] * (col.ndim - 1))].copy
|
|
1060
1088
|
end
|
|
@@ -1062,86 +1090,91 @@ class GroupedFrame
|
|
|
1062
1090
|
CAFrame.new(out, axis_name: @frame.axis_name, index: @frame.index&.copy)
|
|
1063
1091
|
end
|
|
1064
1092
|
|
|
1065
|
-
def
|
|
1066
|
-
|
|
1093
|
+
def rank_index(*names, descending: false, method: :ordinal, kind: :quick)
|
|
1094
|
+
descending = CArray.send(:bool_arg, descending, "descending", "rank_index")
|
|
1095
|
+
transform_columns(:rank_index, names, [], { descending: descending, method: method, kind: kind })
|
|
1067
1096
|
end
|
|
1068
1097
|
|
|
1069
1098
|
CArray.send(:define_named_methods, self,
|
|
1070
1099
|
[:cumsum, :cumprod, :cummax, :cummin, :cumcount], :group_scan)
|
|
1071
1100
|
|
|
1072
|
-
private def group_scan
|
|
1101
|
+
private def group_scan(op, *names)
|
|
1073
1102
|
transform_columns(op, names, [], {})
|
|
1074
1103
|
end
|
|
1075
1104
|
|
|
1076
1105
|
# Fill the masked cells of a column from the rows of the same group, in
|
|
1077
|
-
# place, as +CAFrame#
|
|
1078
|
-
# group's last present value forward in row order, +:
|
|
1106
|
+
# place, as +CAFrame#unmask+ does for the whole column: +:forward+ carries the
|
|
1107
|
+
# group's last present value forward in row order, +:backward+ its next one
|
|
1079
1108
|
# back, and +:linear+ interpolates between the group's present values
|
|
1080
1109
|
# against the frame's index (against the row position within the group
|
|
1081
1110
|
# when the frame has no index). A fill never reaches across into another
|
|
1082
1111
|
# group, so a cell with nothing to carry or interpolate from in its group
|
|
1083
1112
|
# stays masked, and a row whose key is masked (in no group) is left as it
|
|
1084
|
-
# is. A constant fills every masked cell, as +CAFrame#
|
|
1113
|
+
# is. A constant fills every masked cell, as +CAFrame#unmask+ does. Returns
|
|
1085
1114
|
# self.
|
|
1086
1115
|
#
|
|
1087
|
-
# df.group_by("station").
|
|
1088
|
-
# df.group_by("station").
|
|
1089
|
-
# df.group_by("station").
|
|
1090
|
-
# df.group_by("station").
|
|
1091
|
-
# df.group_by("station").
|
|
1116
|
+
# df.group_by("station").unmask("temp", method: :forward)
|
|
1117
|
+
# df.group_by("station").unmask("temp", method: :linear)
|
|
1118
|
+
# df.group_by("station").unmask(method: { "temp" => :linear, "rain" => :forward })
|
|
1119
|
+
# df.group_by("station").unmask("temp", "rain", method: :linear)
|
|
1120
|
+
# df.group_by("station").unmask(method: :forward) # every column with a masked cell
|
|
1121
|
+
# df.group_by("station").unmask("rain", 0.0)
|
|
1092
1122
|
#
|
|
1093
|
-
# +:
|
|
1123
|
+
# +:forward+ and +:backward+ follow the row order: sort the frame first
|
|
1094
1124
|
# (+sort_by_key("time")+) when its rows are not in time order. +:linear+
|
|
1095
|
-
# reads each group in the order of the index, as +CAFrame#
|
|
1096
|
-
#
|
|
1097
|
-
# @param args [Array] the columns, as +CAFrame#
|
|
1098
|
-
#
|
|
1099
|
-
# @
|
|
1100
|
-
#
|
|
1101
|
-
#
|
|
1102
|
-
|
|
1103
|
-
|
|
1125
|
+
# reads each group in the order of the index, as +CAFrame#unmask+ does.
|
|
1126
|
+
#
|
|
1127
|
+
# @param args [Array] the columns, as +CAFrame#unmask+ takes them, then a
|
|
1128
|
+
# constant (a Symbol is a constant); or one Hash of constants.
|
|
1129
|
+
# @param method [Symbol, Hash, nil] +:forward+, +:backward+ or +:linear+,
|
|
1130
|
+
# or a Hash of them by column.
|
|
1131
|
+
# @return [CAGroupedFrame] self
|
|
1132
|
+
# @raise [ArgumentError] for another method, or for +:linear+ on a column
|
|
1133
|
+
# or index +CAFrame#unmask+ cannot interpolate.
|
|
1134
|
+
def unmask(*args, method: nil, **named)
|
|
1135
|
+
plan = @frame.__send__(:fill_arguments, args, named, method, @key_names)
|
|
1104
1136
|
return self if plan.nil?
|
|
1105
1137
|
refuse_key_on_rows(:fill, plan.is_a?(Hash) ? plan.keys : [plan.first])
|
|
1106
1138
|
if plan.is_a?(Hash)
|
|
1107
|
-
@frame.__send__(:fill_by_map, plan, "
|
|
1139
|
+
@frame.__send__(:fill_by_map, plan, "unmask") do |frame, key, how|
|
|
1108
1140
|
grouped = if frame.equal?(@frame)
|
|
1109
1141
|
self
|
|
1110
1142
|
else
|
|
1111
1143
|
# key-labels: the groups' index as it is, on a copy of the frame
|
|
1112
|
-
|
|
1144
|
+
CAGroupedFrame.new(frame, @cat, @axis_name, @key_names, index: @index,
|
|
1113
1145
|
label_columns: @label_columns)
|
|
1114
1146
|
end
|
|
1115
|
-
grouped.
|
|
1147
|
+
grouped.__send__(:unmask_column, key, how)
|
|
1116
1148
|
end
|
|
1117
1149
|
return self
|
|
1118
1150
|
end
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1151
|
+
unmask_column(*plan)
|
|
1152
|
+
end
|
|
1153
|
+
|
|
1154
|
+
private def unmask_column(name, how)
|
|
1155
|
+
refuse_changed_rows("unmask")
|
|
1156
|
+
unless how.is_a?(CAFrame.const_get(:FillMethod))
|
|
1157
|
+
@frame.unmask(name, how)
|
|
1158
|
+
return self
|
|
1159
|
+
end
|
|
1160
|
+
case how.name
|
|
1161
|
+
when :forward, :backward
|
|
1123
1162
|
@frame.__send__(:refuse_if_frozen)
|
|
1124
|
-
key = @frame.__send__(:column_key, name, "
|
|
1125
|
-
col = @frame.__send__(:named_column, key, "
|
|
1163
|
+
key = @frame.__send__(:column_key, name, "unmask")
|
|
1164
|
+
col = @frame.__send__(:named_column, key, "unmask")
|
|
1126
1165
|
# A row in no group is UNDEF in the fill, so writing it changes nothing.
|
|
1127
|
-
held = group_transform(col, :strip_mask, [], { method:
|
|
1166
|
+
held = group_transform(col, :strip_mask, [], { method: how.name })
|
|
1128
1167
|
@frame.__send__(:write_filled_cells, col, held)
|
|
1129
1168
|
when :linear
|
|
1130
1169
|
@frame.__send__(:refuse_if_frozen)
|
|
1131
|
-
key = @frame.__send__(:column_key, name, "
|
|
1132
|
-
# The column and the index are checked as CAFrame#
|
|
1170
|
+
key = @frame.__send__(:column_key, name, "unmask")
|
|
1171
|
+
# The column and the index are checked as CAFrame#unmask checks them,
|
|
1133
1172
|
# on no rows, so a call without groups is refused as one with groups.
|
|
1134
|
-
@frame.__send__(:select_rows, CA_INT64([])).
|
|
1173
|
+
@frame.__send__(:select_rows, CA_INT64([])).unmask(key, method: :linear)
|
|
1135
1174
|
# Each group's rows as a frame of views, so its fill writes through.
|
|
1136
|
-
|
|
1137
|
-
@frame.__send__(:select_rows, group_address(k)).
|
|
1175
|
+
ngroups.times do |k|
|
|
1176
|
+
@frame.__send__(:select_rows, group_address(k)).unmask(key, method: :linear)
|
|
1138
1177
|
end
|
|
1139
|
-
when Symbol
|
|
1140
|
-
raise ArgumentError,
|
|
1141
|
-
"fill: unknown method #{method_or_value.inspect} " \
|
|
1142
|
-
"(:ffill | :bfill | :linear, or a constant fill value)"
|
|
1143
|
-
else
|
|
1144
|
-
@frame.fill(name, method_or_value)
|
|
1145
1178
|
end
|
|
1146
1179
|
self
|
|
1147
1180
|
end
|
|
@@ -1155,7 +1188,7 @@ class GroupedFrame
|
|
|
1155
1188
|
#
|
|
1156
1189
|
# @param n [Integer] how many rows per group, 0 or more (default 5).
|
|
1157
1190
|
# @return [CAFrame]
|
|
1158
|
-
def head
|
|
1191
|
+
def head(n = 5)
|
|
1159
1192
|
return rows_at_the_ends_of_groups("head", n) { |addr| addr.first(n) }
|
|
1160
1193
|
end
|
|
1161
1194
|
|
|
@@ -1163,17 +1196,17 @@ class GroupedFrame
|
|
|
1163
1196
|
#
|
|
1164
1197
|
# @param n [Integer] how many rows per group, 0 or more (default 5).
|
|
1165
1198
|
# @return [CAFrame]
|
|
1166
|
-
def tail
|
|
1199
|
+
def tail(n = 5)
|
|
1167
1200
|
return rows_at_the_ends_of_groups("tail", n) { |addr| addr.last(n) }
|
|
1168
1201
|
end
|
|
1169
1202
|
|
|
1170
|
-
private def rows_at_the_ends_of_groups
|
|
1203
|
+
private def rows_at_the_ends_of_groups(verb, n)
|
|
1171
1204
|
refuse_changed_rows(verb)
|
|
1172
1205
|
unless n.is_a?(Integer)
|
|
1173
1206
|
raise TypeError, "#{verb}: the count is an Integer (got #{n.inspect})"
|
|
1174
1207
|
end
|
|
1175
1208
|
raise ArgumentError, "#{verb}: the count must be 0 or more (got #{n})" if n < 0
|
|
1176
|
-
picked = (0...
|
|
1209
|
+
picked = (0...ngroups).flat_map { |k| yield(group_address(k).to_a) }.sort
|
|
1177
1210
|
return @frame[CA_INT64(picked)]
|
|
1178
1211
|
end
|
|
1179
1212
|
|
|
@@ -1190,7 +1223,7 @@ class GroupedFrame
|
|
|
1190
1223
|
# @param keys [Array<String>] the key columns (or the index axis name).
|
|
1191
1224
|
# @return [CAFrame]
|
|
1192
1225
|
# @raise [ArgumentError] when +n+ is negative or no key is given.
|
|
1193
|
-
def nlargest
|
|
1226
|
+
def nlargest(n, *keys)
|
|
1194
1227
|
return largest_rows_of_groups("nlargest", n, keys, true)
|
|
1195
1228
|
end
|
|
1196
1229
|
|
|
@@ -1201,11 +1234,11 @@ class GroupedFrame
|
|
|
1201
1234
|
# @param keys [Array<String>] the key columns (or the index axis name).
|
|
1202
1235
|
# @return [CAFrame]
|
|
1203
1236
|
# @raise [ArgumentError] when +n+ is negative or no key is given.
|
|
1204
|
-
def nsmallest
|
|
1237
|
+
def nsmallest(n, *keys)
|
|
1205
1238
|
return largest_rows_of_groups("nsmallest", n, keys, false)
|
|
1206
1239
|
end
|
|
1207
1240
|
|
|
1208
|
-
private def largest_rows_of_groups
|
|
1241
|
+
private def largest_rows_of_groups(verb, n, keys, descending)
|
|
1209
1242
|
refuse_changed_rows(verb)
|
|
1210
1243
|
unless n.is_a?(Integer)
|
|
1211
1244
|
raise TypeError, "#{verb}: the count is an Integer (got #{n.inspect})"
|
|
@@ -1215,7 +1248,7 @@ class GroupedFrame
|
|
|
1215
1248
|
cols = keys.map do |key|
|
|
1216
1249
|
@frame.__send__(:key_column, @frame.__send__(:column_key, key, verb), verb)
|
|
1217
1250
|
end
|
|
1218
|
-
sort_keys = cols.map { |col| descending ? col.
|
|
1251
|
+
sort_keys = cols.map { |col| descending ? col.rank_index(descending: true, method: :dense) : col }
|
|
1219
1252
|
# Rows sorted by group, then by the keys; a row in no group comes last.
|
|
1220
1253
|
addr = CArray.sort_addr(group_codes, *sort_keys, masked_position: :last).to_a
|
|
1221
1254
|
picked = []
|
|
@@ -1240,12 +1273,12 @@ class GroupedFrame
|
|
|
1240
1273
|
# undetermined, as is a row whose key is masked (it belongs to no group):
|
|
1241
1274
|
# such rows are not kept, as +CAFrame#filter+ does not keep them.
|
|
1242
1275
|
#
|
|
1243
|
-
# @yieldparam grouped [
|
|
1276
|
+
# @yieldparam grouped [CAGroupedFrame] self.
|
|
1244
1277
|
# @yieldreturn [CArray] boolean, one value per group.
|
|
1245
1278
|
# @return [CAFrame]
|
|
1246
1279
|
# @raise [ArgumentError] when the block does not return a boolean CArray of
|
|
1247
|
-
# shape [
|
|
1248
|
-
def filter
|
|
1280
|
+
# shape [ngroups].
|
|
1281
|
+
def filter(**options)
|
|
1249
1282
|
CAFrame.__send__(:refuse_filter_keywords, options, "filter")
|
|
1250
1283
|
raise ArgumentError, "filter: give a block" unless block_given?
|
|
1251
1284
|
refuse_changed_rows("filter")
|
|
@@ -1255,10 +1288,10 @@ class GroupedFrame
|
|
|
1255
1288
|
"filter block must return a boolean CArray (got #{chosen.is_a?(CArray) ? "a #{CArray::Inspector.type_label(chosen)} CArray" : chosen.class})" +
|
|
1256
1289
|
CAFrame.__send__(:equality_hint, chosen)
|
|
1257
1290
|
end
|
|
1258
|
-
unless chosen.shape == [
|
|
1291
|
+
unless chosen.shape == [ngroups]
|
|
1259
1292
|
raise ArgumentError,
|
|
1260
1293
|
"filter mask has shape #{chosen.shape.inspect}, " \
|
|
1261
|
-
"expected [#{
|
|
1294
|
+
"expected [#{ngroups}] (one value per group)"
|
|
1262
1295
|
end
|
|
1263
1296
|
rows = spread_to_rows(chosen)
|
|
1264
1297
|
@frame.filter(rows)
|
|
@@ -1269,7 +1302,7 @@ class GroupedFrame
|
|
|
1269
1302
|
# Nothing is copied, so a write to a cell reaches the grouped frame's
|
|
1270
1303
|
# rows; adding or dropping a column changes the new frame only.
|
|
1271
1304
|
#
|
|
1272
|
-
# def warm_stations
|
|
1305
|
+
# def warm_stations(grp)
|
|
1273
1306
|
# grp.filter { |g| g["temp"].mean > 15.0 } # the rows of some groups
|
|
1274
1307
|
# end
|
|
1275
1308
|
# grp.to_frame # all of its rows
|
|
@@ -1282,7 +1315,7 @@ class GroupedFrame
|
|
|
1282
1315
|
|
|
1283
1316
|
# @return [String]
|
|
1284
1317
|
def inspect
|
|
1285
|
-
"#<
|
|
1318
|
+
"#<CAGroupedFrame ngroups=#{ngroups} by=#{@axis_name.inspect}>"
|
|
1286
1319
|
end
|
|
1287
1320
|
|
|
1288
1321
|
# A within-group transform of the named columns, or of every column whose
|
|
@@ -1355,7 +1388,7 @@ class GroupedFrame
|
|
|
1355
1388
|
private def refuse_key_as_result(reduction, names)
|
|
1356
1389
|
named = names.map { |n| n.is_a?(String) ? n : nil }.compact & @key_names
|
|
1357
1390
|
return if named.empty?
|
|
1358
|
-
hint = reduction == :count ? "; the number of rows of each group is
|
|
1391
|
+
hint = reduction == :count ? "; the number of rows of each group is group_sizes" : ""
|
|
1359
1392
|
raise ArgumentError,
|
|
1360
1393
|
"#{reduction}: #{named.first.inspect} is the group key, already in the result#{hint}"
|
|
1361
1394
|
end
|
|
@@ -1420,11 +1453,11 @@ class GroupedFrame
|
|
|
1420
1453
|
end
|
|
1421
1454
|
out.each_with_object({}) do |(name, col), cols|
|
|
1422
1455
|
name = @frame.__send__(:column_name_arg, name, "aggregate")
|
|
1423
|
-
unless col.is_a?(CArray) && col.ndim >= 1 && col.shape[0] ==
|
|
1456
|
+
unless col.is_a?(CArray) && col.ndim >= 1 && col.shape[0] == ngroups
|
|
1424
1457
|
got = col.is_a?(CArray) ? "shape #{col.shape.inspect}" : col.class.to_s
|
|
1425
1458
|
raise ArgumentError,
|
|
1426
1459
|
"aggregate: #{name.inspect} must be a CArray with one value per group " \
|
|
1427
|
-
"(#{
|
|
1460
|
+
"(#{ngroups}), got #{got}"
|
|
1428
1461
|
end
|
|
1429
1462
|
cols[name] = col
|
|
1430
1463
|
end
|
|
@@ -1456,13 +1489,13 @@ class GroupedFrame
|
|
|
1456
1489
|
|
|
1457
1490
|
private def each_group_slice(in_name)
|
|
1458
1491
|
col = @frame.__send__(:named_column, in_name, "aggregate")
|
|
1459
|
-
|
|
1492
|
+
ngroups.times do |k|
|
|
1460
1493
|
yield col[group_address(k), *([nil] * (col.ndim - 1))]
|
|
1461
1494
|
end
|
|
1462
1495
|
end
|
|
1463
1496
|
|
|
1464
1497
|
private def each_group_frame
|
|
1465
|
-
|
|
1498
|
+
ngroups.times { |k| yield @frame[group_address(k)] }
|
|
1466
1499
|
end
|
|
1467
1500
|
|
|
1468
1501
|
# The first row of each group, in code order.
|
|
@@ -1472,13 +1505,13 @@ class GroupedFrame
|
|
|
1472
1505
|
# group without rows keeps no row (UNDEF). With more groups the columns
|
|
1473
1506
|
# are gathered through the sort anyway, so the first rows are read off it.
|
|
1474
1507
|
private def first_rows
|
|
1475
|
-
if
|
|
1476
|
-
return group_perm[group_bounds[0...
|
|
1508
|
+
if ngroups > CACategoricalIterator::SCATTER_CATEGORIES_MAX
|
|
1509
|
+
return group_perm[group_bounds[0...ngroups]]
|
|
1477
1510
|
end
|
|
1478
1511
|
n = @cat.elements
|
|
1479
|
-
first = CArray.int64(
|
|
1512
|
+
first = CArray.int64(ngroups).fill(n)
|
|
1480
1513
|
first.scatter_min!(@cat.codes, CArray.int64(n).seq!)
|
|
1481
|
-
first[first.eq(n)] = UNDEF if
|
|
1514
|
+
first[first.eq(n)] = UNDEF if ngroups > 0
|
|
1482
1515
|
first
|
|
1483
1516
|
end
|
|
1484
1517
|
|