carray 3.0.5 → 3.0.7
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/CHANGELOG.md +343 -0
- data/README.md +25 -4
- data/carray.gemspec +3 -2
- data/ext/ca_axis_dispatch.c +6 -0
- data/ext/ca_axis_group.c +7 -7
- data/ext/ca_binop_dispatch.c +8 -0
- data/ext/ca_binop_dispatch.h +1 -0
- data/ext/ca_categorical_iterator.c +14 -14
- data/ext/ca_kernel_iterator.c +116 -231
- data/ext/ca_kernel_iterator.h +416 -258
- data/ext/ca_obj_array.c +4 -4
- data/ext/ca_obj_bincmp.c +28 -4
- data/ext/ca_obj_binop.c +46 -12
- data/ext/ca_obj_bitarray.c +2 -4
- data/ext/ca_obj_bitfield.c +88 -4
- data/ext/ca_obj_byte_swap.c +4 -4
- data/ext/ca_obj_const_string.c +422 -257
- data/ext/ca_obj_face.c +73 -33
- data/ext/ca_obj_fake.c +114 -13
- data/ext/ca_obj_farray.c +7 -1
- data/ext/ca_obj_fixlen_string.c +51 -17
- data/ext/ca_obj_grid.c +179 -19
- data/ext/ca_obj_meld.c +74 -118
- data/ext/ca_obj_moncmp.c +9 -1
- data/ext/ca_obj_monop.c +100 -36
- data/ext/ca_obj_object.c +63 -29
- data/ext/ca_obj_reduce.c +17 -5
- data/ext/ca_obj_refer.c +162 -23
- data/ext/ca_obj_remap.c +69 -10
- data/ext/ca_obj_repeat.c +1 -1
- data/ext/ca_obj_roll.c +3 -4
- data/ext/ca_obj_select.c +12 -7
- data/ext/ca_obj_select_axis.c +9 -6
- data/ext/ca_obj_shift.c +1 -0
- data/ext/ca_obj_stack.c +315 -116
- data/ext/ca_obj_stride.c +177 -53
- data/ext/ca_obj_string.c +20 -4
- data/ext/ca_obj_tile.c +3 -4
- data/ext/ca_obj_triop.c +24 -13
- data/ext/ca_obj_window.c +23 -15
- data/ext/ca_op_ipower.c +71 -23
- data/ext/ca_op_powi.h +6 -29
- data/ext/ca_sort_kernels.h +3 -0
- data/ext/ca_sweep_engine.c +90 -23
- data/ext/ca_sweep_engine.h +5 -6
- data/ext/caframe_csv_reader.c +2 -2
- data/ext/carray.h +5 -2
- data/ext/carray_access.c +125 -103
- data/ext/carray_address_basis.c +209 -1
- data/ext/carray_attribute.c +8 -0
- data/ext/carray_broadcast.c +5 -6
- data/ext/carray_build_flags.h +1 -1
- data/ext/carray_call_cfunc.c +5 -8
- data/ext/carray_cast.c +319 -48
- data/ext/carray_cast_func.rb +37 -5
- data/ext/carray_conversion.c +65 -7
- data/ext/carray_copy.c +24 -0
- data/ext/carray_core.c +874 -72
- data/ext/carray_count.c +28 -9
- data/ext/carray_element.c +22 -12
- data/ext/carray_factorize.c +11 -11
- data/ext/carray_histogram.c +27 -5
- data/ext/carray_index_classifier.c +7 -5
- data/ext/carray_internal.h +241 -12
- data/ext/carray_kernels_bincmp.c +2007 -3
- data/ext/carray_kernels_binop.c +25502 -5336
- data/ext/carray_kernels_init.c +22 -22
- data/ext/carray_kernels_monop.c +10739 -3771
- data/ext/carray_kernels_reduce_aggregate.c +4429 -702
- data/ext/carray_kernels_reduce_boolean.c +1533 -15
- data/ext/carray_kernels_reduce_cumulative.c +1808 -248
- data/ext/carray_kernels_reduce_extreme.c +4508 -511
- data/ext/carray_kernels_reduce_variance.c +2284 -152
- data/ext/carray_kernels_scan.c +240 -240
- data/ext/carray_kernels_search.c +4211 -264
- data/ext/carray_kernels_sort.c +367 -450
- data/ext/carray_lazy.c +63 -1
- data/ext/carray_loop.c +10 -3
- data/ext/carray_mask.c +247 -149
- data/ext/carray_median_percentile.c +17 -12
- data/ext/carray_memory_view.c +54 -29
- data/ext/carray_operator.c +219 -55
- data/ext/carray_order.c +55 -11
- data/ext/carray_partition.c +1 -1
- data/ext/carray_random.c +5 -2
- data/ext/carray_round.c +24 -1
- data/ext/carray_scatter.c +4 -0
- data/ext/carray_segment.c +1 -1
- data/ext/carray_slab.c +110 -25
- data/ext/carray_sort.c +7 -4
- data/ext/carray_sort_kernel.c +24 -0
- data/ext/carray_struct.c +21 -6
- data/ext/carray_test.c +154 -53
- data/ext/carray_time_text.c +8 -2
- data/ext/carray_utils.c +124 -54
- data/ext/extconf.rb +11 -0
- data/ext/mk_call_cfunc.rb +6 -11
- data/ext/mkkernel.rb +383 -121
- data/ext/ruby_carray.c +4 -1
- data/ext/version.h +4 -4
- data/lib/carray/attribute.rb +25 -1
- data/lib/carray/autoload_carray.rb +3 -3
- data/lib/carray/autoload_method_extension.rb +5 -3
- data/lib/carray/axis_group.rb +22 -13
- data/lib/carray/basics.rb +27 -9
- data/lib/carray/bincount_nd.rb +8 -2
- data/lib/carray/block_iterator.rb +65 -24
- data/lib/carray/categorical.rb +27 -16
- data/lib/carray/categorical_iterator.rb +4 -3
- data/lib/carray/complex.rb +2 -2
- data/lib/carray/conditional.rb +55 -6
- data/lib/carray/const_string.rb +74 -0
- data/lib/carray/construct.rb +2 -2
- data/lib/carray/core_extensions.rb +19 -68
- data/lib/carray/data_type_extension.rb +1 -0
- data/lib/carray/fixlen_string.rb +83 -1
- data/lib/carray/frame/column_verbs.rb +102 -16
- data/lib/carray/frame/concat.rb +50 -6
- data/lib/carray/frame/convert.rb +19 -0
- data/lib/carray/frame/csv_parser.rb +45 -36
- data/lib/carray/frame/describe.rb +4 -2
- data/lib/carray/frame/frame.rb +436 -83
- data/lib/carray/frame/group.rb +731 -78
- data/lib/carray/frame/io.rb +216 -73
- data/lib/carray/frame/join.rb +166 -43
- data/lib/carray/frame/records.rb +9 -7
- data/lib/carray/frame/reshape.rb +109 -11
- data/lib/carray/frame/sort.rb +57 -9
- data/lib/carray/frame/verbs.rb +448 -60
- data/lib/carray/frame/windows.rb +71 -9
- data/lib/carray/fuse_source.rb +4 -2
- data/lib/carray/inspect.rb +7 -2
- data/lib/carray/iterator.rb +40 -0
- data/lib/carray/key_windows.rb +10 -4
- data/lib/carray/lazy.rb +28 -24
- data/lib/carray/mask_gap_fill.rb +37 -17
- data/lib/carray/meld_reduce.rb +1 -1
- data/lib/carray/methods/bin.rb +3 -0
- data/lib/carray/methods/choose.rb +3 -2
- data/lib/carray/methods/composition.rb +1 -1
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +4 -1
- data/lib/carray/methods/is_in.rb +23 -3
- data/lib/carray/methods/locate_addr.rb +63 -12
- data/lib/carray/methods/meshgrid.rb +3 -1
- data/lib/carray/methods/repeat.rb +11 -1
- data/lib/carray/record_order.rb +35 -6
- data/lib/carray/runtime.rb +57 -8
- data/lib/carray/segment_iterator.rb +42 -24
- data/lib/carray/serialize.rb +56 -17
- data/lib/carray/slab_iterator.rb +1 -1
- data/lib/carray/stack.rb +21 -9
- data/lib/carray/string.rb +6 -6
- data/lib/carray/string_operation_extension.rb +25 -4
- data/lib/carray/struct_builder.rb +4 -1
- data/lib/carray/time.rb +195 -46
- data/lib/carray/window_iterator.rb +64 -16
- data/yard-stubs/ca_obj_array.rb +26 -1
- data/yard-stubs/ca_obj_bitfield.rb +4 -1
- data/yard-stubs/ca_obj_refer.rb +3 -0
- data/yard-stubs/ca_obj_window.rb +2 -1
- data/yard-stubs/carray_access.rb +4 -2
- data/yard-stubs/carray_cast.rb +14 -5
- data/yard-stubs/carray_conversion.rb +8 -2
- data/yard-stubs/carray_count.rb +6 -3
- data/yard-stubs/carray_mask.rb +20 -1
- data/yard-stubs/carray_math.rb +36 -16
- data/yard-stubs/carray_memory_view.rb +7 -0
- data/yard-stubs/carray_order.rb +6 -0
- data/yard-stubs/carray_scatter.rb +2 -1
- data/yard-stubs/carray_test.rb +23 -7
- metadata +17 -3
data/lib/carray/frame/verbs.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# CAFrame column verbs (memo §11.4, §12-A, §13.2).
|
|
2
2
|
#
|
|
3
|
-
#
|
|
4
|
-
# returns
|
|
5
|
-
# (
|
|
6
|
-
#
|
|
3
|
+
# A verb that changes which columns the frame has, their names or their
|
|
4
|
+
# types returns a new frame and leaves this one as it is; a verb that edits
|
|
5
|
+
# cells (fill, mask_eq) edits them in place and returns self. The new frame
|
|
6
|
+
# shares the columns it did not replace, so a write to a cell through it
|
|
7
|
+
# reaches this frame too: only the table (names, index, column types) is its
|
|
8
|
+
# own.
|
|
7
9
|
|
|
8
10
|
class CAFrame
|
|
9
11
|
# Add (or replace) a column, returning a new frame — the column set changes,
|
|
@@ -49,11 +51,12 @@ class CAFrame
|
|
|
49
51
|
end
|
|
50
52
|
|
|
51
53
|
# Remove one or more columns, returning a new frame (column set changes,
|
|
52
|
-
# memo §3.8). Column data is untouched and shared with the original.
|
|
54
|
+
# memo §3.8). Column data is untouched and shared with the original. Each
|
|
55
|
+
# argument is a name, a Range of names or a Regexp, as +select+ takes them.
|
|
53
56
|
def drop(*names)
|
|
54
57
|
cols = @columns.dup
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
names_selected(names, "drop").each do |key|
|
|
59
|
+
refuse_missing_column(key) unless cols.key?(key)
|
|
57
60
|
cols.delete(key)
|
|
58
61
|
end
|
|
59
62
|
rebuild(cols)
|
|
@@ -61,21 +64,87 @@ class CAFrame
|
|
|
61
64
|
|
|
62
65
|
# Rename columns, returning a new frame (column names change, memo §3.8).
|
|
63
66
|
# Column order is preserved (memo §13.2 "column order = insertion order");
|
|
64
|
-
# columns are shared by reference.
|
|
65
|
-
|
|
67
|
+
# columns are shared by reference. The new names are given as a Hash of
|
|
68
|
+
# old name => new name, or by a block that receives each column's name and
|
|
69
|
+
# returns its new one. The index has a name too, and is renamed the same
|
|
70
|
+
# way. Names may be exchanged; two columns, or a column and the index, may
|
|
71
|
+
# not end up with one name.
|
|
72
|
+
#
|
|
73
|
+
# df.rename("temp" => "T")
|
|
74
|
+
# df.rename { |name| name.upcase }
|
|
75
|
+
# df.group_by("shop").sum.rename("shop" => "store") # the index
|
|
76
|
+
def rename(mapping = nil)
|
|
77
|
+
index_name = @index ? @axis_name : nil
|
|
78
|
+
if block_given?
|
|
79
|
+
raise ArgumentError, "rename: give a Hash or a block, not both" unless mapping.nil?
|
|
80
|
+
mapping = (@columns.keys + [index_name].compact).to_h { |name| [name, yield(name)] }
|
|
81
|
+
end
|
|
82
|
+
raise ArgumentError, "rename: give a Hash of old name => new name, or a block" if mapping.nil?
|
|
66
83
|
norm = {}
|
|
67
84
|
mapping.each do |old, new|
|
|
68
85
|
o = column_key(old, "rename")
|
|
69
86
|
n = column_name_arg(new, "rename")
|
|
70
|
-
|
|
71
|
-
if n != o && @columns.key?(n)
|
|
72
|
-
raise ArgumentError, "rename target #{n.inspect} already exists"
|
|
73
|
-
end
|
|
87
|
+
refuse_missing_column(o, "rename") unless @columns.key?(o) || o == index_name
|
|
74
88
|
norm[o] = n
|
|
75
89
|
end
|
|
90
|
+
names = @columns.keys.map { |k| norm[k] || k }
|
|
91
|
+
names << (norm[index_name] || index_name) if index_name
|
|
92
|
+
CAFrame.__send__(:refuse_repeated_names, names, "rename", "the columns and index would be named")
|
|
76
93
|
rebuilt = {}
|
|
77
94
|
@columns.each { |k, v| rebuilt[norm[k] || k] = v }
|
|
78
|
-
rebuild(rebuilt)
|
|
95
|
+
return rebuild(rebuilt) unless index_name && norm.key?(index_name)
|
|
96
|
+
CAFrame.new(rebuilt, axis_name: norm[index_name], index: @index, attrs: @attrs)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# A new frame with each column replaced by what the block computes from it,
|
|
100
|
+
# the names, the index and the attributes kept; this frame is left as it
|
|
101
|
+
# is. Without names every column is given to the block; names pick the
|
|
102
|
+
# columns as +select+ does (a name, a Range of names, a Regexp), and the
|
|
103
|
+
# others are kept as they are. The block receives the column and its name
|
|
104
|
+
# and returns a CArray (or an Array) of one value per row. A single value,
|
|
105
|
+
# such as a reduction's, is refused: to give every row its column's mean,
|
|
106
|
+
# use +mean(name, keep_rows: true)+.
|
|
107
|
+
#
|
|
108
|
+
# df.transform { |col| col.round(1) }
|
|
109
|
+
# df.transform("tmax".."tmin") { |col| (col - col.mean) / col.stddev }
|
|
110
|
+
# df.transform(/^t/) { |col, name| ... }
|
|
111
|
+
#
|
|
112
|
+
# What the block returns becomes the column as it is: a view stays a view
|
|
113
|
+
# of what it was made from, so a write to a cell reaches it (+copy+ cuts
|
|
114
|
+
# that). A lazy expression is evaluated, as a column holds its values.
|
|
115
|
+
#
|
|
116
|
+
# @param names [Array<String, Range, Regexp>] the columns; default all.
|
|
117
|
+
# @yieldparam col [CArray] a column.
|
|
118
|
+
# @yieldparam name [String] its name.
|
|
119
|
+
# @yieldreturn [CArray, Array] one value per row.
|
|
120
|
+
# @return [CAFrame]
|
|
121
|
+
# @raise [ArgumentError] without a block, or when the block gives a single
|
|
122
|
+
# value or a column of another length.
|
|
123
|
+
def transform (*names)
|
|
124
|
+
raise ArgumentError, "transform needs a block that computes each column" unless block_given?
|
|
125
|
+
keys = names.empty? ? @columns.keys : names_selected(names, "transform")
|
|
126
|
+
keys.each { |key| refuse_missing_column(key) unless @columns.key?(key) }
|
|
127
|
+
cols = @columns.dup
|
|
128
|
+
computed = keys.map do |key|
|
|
129
|
+
out = yield(@columns[key], key)
|
|
130
|
+
unless out.is_a?(CArray) || out.is_a?(Array)
|
|
131
|
+
raise ArgumentError,
|
|
132
|
+
"transform: the block gave #{out.class} for column #{key.inspect}, not a column; " \
|
|
133
|
+
"return one value per row (to give every row a summary, use " \
|
|
134
|
+
"a reduction with keep_rows: true)"
|
|
135
|
+
end
|
|
136
|
+
# An empty Array on a frame with no rows has no value to type it by,
|
|
137
|
+
# so the column keeps the type it was given as.
|
|
138
|
+
out = out.is_a?(Array) && out.empty? && @nrow == 0 ? @columns[key].copy : coerce_column(out)
|
|
139
|
+
unless out.ndim >= 1 && out.shape[0] == @nrow
|
|
140
|
+
raise ArgumentError,
|
|
141
|
+
"transform: the block gave #{out.ndim >= 1 ? out.shape[0] : "no"} rows for " \
|
|
142
|
+
"column #{key.inspect}; the frame has #{@nrow}"
|
|
143
|
+
end
|
|
144
|
+
[key, out]
|
|
145
|
+
end
|
|
146
|
+
computed.each { |key, out| cols[key] = out }
|
|
147
|
+
return rebuild(cols)
|
|
79
148
|
end
|
|
80
149
|
|
|
81
150
|
# Split a text column at +sep+ into the columns named by +into:+,
|
|
@@ -98,7 +167,7 @@ class CAFrame
|
|
|
98
167
|
# fewer than two columns or one twice, or names a column already there.
|
|
99
168
|
def split_column(name, sep, into:)
|
|
100
169
|
key = column_key(name, "split_column")
|
|
101
|
-
col = @columns.fetch(key) {
|
|
170
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
102
171
|
unless string_column?(col) && col.ndim == 1
|
|
103
172
|
raise ArgumentError, "split_column: #{key.inspect} is not a one-dimensional text column"
|
|
104
173
|
end
|
|
@@ -121,6 +190,9 @@ class CAFrame
|
|
|
121
190
|
pieces.each { |p| p[] = UNDEF }
|
|
122
191
|
time_text_of(col).to_a.each_with_index do |cell, i|
|
|
123
192
|
next if UNDEF.equal?(cell) || cell.nil?
|
|
193
|
+
unless cell.is_a?(String)
|
|
194
|
+
raise ArgumentError, "split_column: row #{i} of #{key.inspect} holds #{cell.inspect}, which is not text"
|
|
195
|
+
end
|
|
124
196
|
cell.split(sep, n).each_with_index { |part, k| pieces[k][i] = part }
|
|
125
197
|
end
|
|
126
198
|
rebuilt = {}
|
|
@@ -159,7 +231,7 @@ class CAFrame
|
|
|
159
231
|
# already a column; and as CACategorical#contrast raises.
|
|
160
232
|
def contrast_columns(name, scheme = :dummy, base: nil, full: false, into: nil)
|
|
161
233
|
key = column_key(name, "contrast_columns")
|
|
162
|
-
col = @columns.fetch(key) {
|
|
234
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
163
235
|
unless col.ndim == 1
|
|
164
236
|
raise ArgumentError, "contrast_columns: #{key.inspect} is not a one-dimensional column"
|
|
165
237
|
end
|
|
@@ -258,7 +330,15 @@ class CAFrame
|
|
|
258
330
|
raise ArgumentError,
|
|
259
331
|
"stack_columns: shape: #{layer.inspect} does not hold the #{names.size} columns"
|
|
260
332
|
end
|
|
261
|
-
|
|
333
|
+
CAFrame.__send__(:refuse_rounded_pieces, parts, "stack_columns", "column #{names.first.inspect}")
|
|
334
|
+
begin
|
|
335
|
+
col = CArray.stack(parts, axis: 1)
|
|
336
|
+
rescue ArgumentError => e
|
|
337
|
+
raise unless e.message.start_with?("stack: ")
|
|
338
|
+
raise ArgumentError,
|
|
339
|
+
"stack_columns: the columns do not meet as one column " \
|
|
340
|
+
"(#{e.message.delete_prefix("stack: ")}); cast them to one type first"
|
|
341
|
+
end
|
|
262
342
|
col = col.reshape(nrow, *layer, *trailing) if layer.size > 1
|
|
263
343
|
rebuilt = {}
|
|
264
344
|
@columns.each do |k, v|
|
|
@@ -290,7 +370,7 @@ class CAFrame
|
|
|
290
370
|
# @return [CAFrame] a new frame; the other columns are shared.
|
|
291
371
|
def unstack_column(name, into: nil)
|
|
292
372
|
key = column_key(name, "unstack_column")
|
|
293
|
-
col = @columns.fetch(key) {
|
|
373
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
294
374
|
if col.ndim < 2
|
|
295
375
|
raise ArgumentError, "unstack_column: #{key.inspect} is one-dimensional; there is nothing to unstack"
|
|
296
376
|
end
|
|
@@ -350,15 +430,15 @@ class CAFrame
|
|
|
350
430
|
unless from.is_a?(String) && to.is_a?(String)
|
|
351
431
|
raise ArgumentError, "#{verb}: a Range of column names takes two names (got #{selection.inspect})"
|
|
352
432
|
end
|
|
353
|
-
i = keys.index(from) or
|
|
354
|
-
j = keys.index(to) or
|
|
433
|
+
i = keys.index(from) or refuse_missing_column(from, verb)
|
|
434
|
+
j = keys.index(to) or refuse_missing_column(to, verb)
|
|
355
435
|
if j < i
|
|
356
436
|
raise ArgumentError, "#{verb}: #{to.inspect} comes before #{from.inspect} in the frame"
|
|
357
437
|
end
|
|
358
438
|
keys[i..(selection.exclude_end? ? j - 1 : j)]
|
|
359
439
|
when Array
|
|
360
440
|
selection.map { |n| column_key(n, verb) }.each do |n|
|
|
361
|
-
|
|
441
|
+
refuse_missing_column(n, verb) unless @columns.key?(n)
|
|
362
442
|
end
|
|
363
443
|
when Regexp
|
|
364
444
|
keys.grep(selection)
|
|
@@ -389,9 +469,11 @@ class CAFrame
|
|
|
389
469
|
# column cast to :time is parsed into a CATime column, in the finest unit
|
|
390
470
|
# its text shows (:D for dates alone, :s with a time of day, :ms / :us /
|
|
391
471
|
# :ns for fractions of a second); parse_to_time takes a format and a
|
|
392
|
-
# unit. A column cast to :
|
|
472
|
+
# unit. A text column cast to :timedelta reads the text to_csv writes for
|
|
473
|
+
# a duration ("194400s", "3M", "3 @ 10 m") into a CATimedelta column, in
|
|
474
|
+
# the finest unit its cells show. A column cast to :categorical becomes a CACategorical of its
|
|
393
475
|
# values, the labels in the order they first appear (categorize); a text
|
|
394
|
-
# column read by
|
|
476
|
+
# column read by read_csv with types: gets there without a String made per
|
|
395
477
|
# cell. Other targets use to_type.
|
|
396
478
|
# The call shapes are told apart by the fact that column names are always
|
|
397
479
|
# Strings and types always Symbols (§3.7):
|
|
@@ -416,8 +498,9 @@ class CAFrame
|
|
|
416
498
|
#
|
|
417
499
|
# A name that is not a column raises KeyError, and a Symbol key other than
|
|
418
500
|
# +:default+ raises TypeError; both before any column is read. Every
|
|
419
|
-
# column is read before any is
|
|
420
|
-
#
|
|
501
|
+
# column is read before any is converted. Returns a new frame holding the
|
|
502
|
+
# converted columns; this frame keeps its own, and the columns not named are
|
|
503
|
+
# shared by the two.
|
|
421
504
|
#
|
|
422
505
|
# +on_error:+ says what to do with a text cell that holds something but
|
|
423
506
|
# does not read as the number type ("x", "1.5" for an integer, "300" for
|
|
@@ -431,12 +514,16 @@ class CAFrame
|
|
|
431
514
|
# cell; no column is rebound
|
|
432
515
|
#
|
|
433
516
|
# +option_name:+ is for a method that takes the map as one of its own
|
|
434
|
-
# options and hands it to cast, as
|
|
517
|
+
# options and hands it to cast, as read_csv does with +types:+. An error
|
|
435
518
|
# about the map then names that option instead of cast, and the map must
|
|
436
519
|
# be a Hash or :infer:
|
|
437
520
|
#
|
|
438
521
|
# frame.cast(types, on_error: on_error, option_name: :types)
|
|
439
|
-
def cast(
|
|
522
|
+
def cast(...)
|
|
523
|
+
return dup.__send__(:cast_in_place, ...)
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
private def cast_in_place(name_or_map = nil, type = nil, on_error: :mask, option_name: nil, **map)
|
|
440
527
|
refuse_if_frozen
|
|
441
528
|
label = option_name ? "#{option_name}:" : "cast"
|
|
442
529
|
# A brace-less map (cast("temp" => :float64, on_error: :warn)) arrives as
|
|
@@ -460,8 +547,9 @@ class CAFrame
|
|
|
460
547
|
"on_error: must be :mask, :warn or :raise (got #{on_error.inspect})"
|
|
461
548
|
end
|
|
462
549
|
name_or_map = { default: :infer } if name_or_map == :infer && type.nil?
|
|
550
|
+
name_or_map = { default: :string } if name_or_map == :string && type.nil?
|
|
463
551
|
if option_name && !name_or_map.is_a?(Hash)
|
|
464
|
-
raise ArgumentError, "#{label} takes a map of column types or :
|
|
552
|
+
raise ArgumentError, "#{label} takes a map of column types, :infer or :string " \
|
|
465
553
|
"(got #{name_or_map.inspect})"
|
|
466
554
|
end
|
|
467
555
|
types =
|
|
@@ -471,7 +559,7 @@ class CAFrame
|
|
|
471
559
|
end
|
|
472
560
|
resolve_cast_map(name_or_map, label)
|
|
473
561
|
else
|
|
474
|
-
{ column_key(name_or_map, label) => type }
|
|
562
|
+
resolve_cast_map({ column_key(name_or_map, label) => type }, label)
|
|
475
563
|
end
|
|
476
564
|
# Read every column before rebinding any, so a raise leaves the frame as
|
|
477
565
|
# it was.
|
|
@@ -500,15 +588,17 @@ class CAFrame
|
|
|
500
588
|
end
|
|
501
589
|
end
|
|
502
590
|
named.each_key do |name|
|
|
503
|
-
|
|
591
|
+
refuse_missing_column(name, label.chomp(":")) unless @columns.key?(name)
|
|
504
592
|
end
|
|
505
|
-
|
|
593
|
+
inferred, named = named.partition { |_, t| t == :infer }.map(&:to_h)
|
|
594
|
+
rest = @columns.keys - named.keys - inferred.keys
|
|
506
595
|
types =
|
|
507
596
|
case default
|
|
508
597
|
when nil then {}
|
|
509
598
|
when :infer then rest.empty? ? {} : select(*rest).infer_types
|
|
510
599
|
else rest.to_h { |name| [name, default] }
|
|
511
600
|
end
|
|
601
|
+
types.merge!(select(*inferred.keys).infer_types) unless inferred.empty?
|
|
512
602
|
types.merge!(named.compact)
|
|
513
603
|
end
|
|
514
604
|
|
|
@@ -533,11 +623,13 @@ class CAFrame
|
|
|
533
623
|
# none present is not listed.
|
|
534
624
|
#
|
|
535
625
|
# A number with a leading zero ("007") is a code and keeps its column as
|
|
536
|
-
# text, and so does an integer too long for int64 (an identifier),
|
|
537
|
-
# float would round. A day-first or
|
|
538
|
-
# read as time, since which one it
|
|
539
|
-
# are looked at -- an object column
|
|
540
|
-
# CAConstString); columns that already have a
|
|
626
|
+
# text, and so does an integer too long for int64 (an identifier), or one
|
|
627
|
+
# past 2**53 among decimals, which a float would round. A day-first or
|
|
628
|
+
# month-first date ("01/02/2024") is not read as time, since which one it
|
|
629
|
+
# is cannot be told. Only text columns are looked at -- an object column
|
|
630
|
+
# or a string Face (CAString, CAConstString); columns that already have a
|
|
631
|
+
# type are not listed. An object column's cells are read as their text,
|
|
632
|
+
# so Ruby numbers there are typed as that text would be.
|
|
541
633
|
#
|
|
542
634
|
# df.cast(df.infer_types) # what cast(:infer) does
|
|
543
635
|
# df.cast(df.infer_types.merge("code" => :int32)) # as cast(default: :infer, "code" => :int32)
|
|
@@ -545,19 +637,30 @@ class CAFrame
|
|
|
545
637
|
@columns.each_with_object({}) do |(key, col), types|
|
|
546
638
|
next unless string_column?(col)
|
|
547
639
|
type = text_for_decimal_reading(col).__infer_number_type_of_text__
|
|
640
|
+
next if type == :float64 && integer_text_past_float?(col)
|
|
548
641
|
type ||= :boolean if boolean_word_column?(col)
|
|
549
642
|
type ||= :time if first_present_cell_can_be_time_text?(col) && time_text_column?(time_text_of(col))
|
|
550
643
|
types[key] = type if type
|
|
551
644
|
end
|
|
552
645
|
end
|
|
553
646
|
|
|
647
|
+
# Whether a cell is an integer a float64 would round. Fewer than 16 digits
|
|
648
|
+
# cannot be, which leaves a const string column's other cells unread.
|
|
649
|
+
private def integer_text_past_float?(col)
|
|
650
|
+
long = col.is_a?(CAConstString) ? col[col.byte_length.ge(16).strip_mask(false)].to_a : time_text_of(col).to_a
|
|
651
|
+
long.any? do |text|
|
|
652
|
+
next text.abs > 2**53 if text.is_a?(Integer) # an object cell read as its text
|
|
653
|
+
text.is_a?(String) && text.match?(/\A\s*[-+]?\d{16,}\s*\z/) && Integer(text.strip, 10).abs > 2**53
|
|
654
|
+
end
|
|
655
|
+
end
|
|
656
|
+
|
|
554
657
|
# Bring every column to one common data type and rebind them (memo §11.4).
|
|
555
658
|
# The frame-level counterpart of +CArray.promote_list+: where +cast+ forces
|
|
556
659
|
# named columns to a type, +promote+ widens the whole table until it has a
|
|
557
|
-
# single type. Returns
|
|
558
|
-
#
|
|
559
|
-
#
|
|
560
|
-
# alone.
|
|
660
|
+
# single type. Returns a new frame; a column that changes type is a fresh
|
|
661
|
+
# one, and one that has the type already is shared with this frame, as
|
|
662
|
+
# +promote+ with no argument leaves it. The index is not a column and is
|
|
663
|
+
# left alone.
|
|
561
664
|
#
|
|
562
665
|
# df.promote # the common type CArray.result_type would pick
|
|
563
666
|
# df.promote(:object) # force the widest type: anything fits
|
|
@@ -574,14 +677,19 @@ class CAFrame
|
|
|
574
677
|
# and therefore always accepted -- it is the way a frame mixing text,
|
|
575
678
|
# Face-typed and numeric columns becomes a single-type table (and so the
|
|
576
679
|
# way +to_ca+ can hand back a matrix for it).
|
|
577
|
-
def promote(
|
|
680
|
+
def promote(...)
|
|
681
|
+
return dup.__send__(:promote_in_place, ...)
|
|
682
|
+
end
|
|
683
|
+
|
|
684
|
+
private def promote_in_place(type = nil)
|
|
578
685
|
refuse_if_frozen
|
|
579
686
|
return self if @columns.empty?
|
|
580
687
|
type.nil? ? promote_to_common : promote_to_type(type)
|
|
581
688
|
self
|
|
582
689
|
end
|
|
583
690
|
|
|
584
|
-
#
|
|
691
|
+
# A new frame with a string column parsed into a time column (memo §11.2);
|
|
692
|
+
# this frame keeps the text column.
|
|
585
693
|
# The column must be string-bearing (an object CArray of Strings, or a
|
|
586
694
|
# CAString / CAConstString / CAFixlenString) — this is the "text -> time"
|
|
587
695
|
# mode, distinct from the integer-serial mode of +to_time+.
|
|
@@ -603,14 +711,18 @@ class CAFrame
|
|
|
603
711
|
# df.parse_to_time("time").set_index("time")
|
|
604
712
|
# df.parse_to_time("date", "%d/%m/%Y")
|
|
605
713
|
# df.parse_to_time("date", :infer)
|
|
606
|
-
def parse_to_time(
|
|
714
|
+
def parse_to_time(...)
|
|
715
|
+
return dup.__send__(:parse_to_time_in_place, ...)
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
private def parse_to_time_in_place(name, format = nil, unit: nil, on_error: :mask)
|
|
607
719
|
refuse_if_frozen
|
|
608
720
|
unless CAST_ON_ERROR.include?(on_error)
|
|
609
721
|
raise ArgumentError,
|
|
610
722
|
"on_error: must be :mask, :warn or :raise (got #{on_error.inspect})"
|
|
611
723
|
end
|
|
612
724
|
key = column_key(name, "parse_to_time")
|
|
613
|
-
col = @columns.fetch(key) {
|
|
725
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
614
726
|
unless string_column?(col)
|
|
615
727
|
raise ArgumentError,
|
|
616
728
|
"parse_to_time needs a string column (object / CAString / " \
|
|
@@ -632,15 +744,15 @@ class CAFrame
|
|
|
632
744
|
# df.infer_time_format("date") # => "%d/%m/%Y"
|
|
633
745
|
def infer_time_format(name)
|
|
634
746
|
key = column_key(name, "infer_time_format")
|
|
635
|
-
col = @columns.fetch(key) {
|
|
747
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
636
748
|
unless string_column?(col)
|
|
637
749
|
raise ArgumentError, "column #{key.inspect} is #{col.data_type}, not text"
|
|
638
750
|
end
|
|
639
751
|
inferred_time_format(key, col)&.first&.format
|
|
640
752
|
end
|
|
641
753
|
|
|
642
|
-
#
|
|
643
|
-
# §11.2). This is the "serial -> time" mode: each value is a count of
|
|
754
|
+
# A new frame with an integer column read as time serial counts (memo
|
|
755
|
+
# §11.2); this frame keeps the integer column. This is the "serial -> time" mode: each value is a count of
|
|
644
756
|
# +unit+ resolution since +epoch+ (default the Unix epoch, 1970-01-01 UTC).
|
|
645
757
|
# +epoch+ takes any time literal (String / Time / Integer), so a column
|
|
646
758
|
# measured from another origin — a netCDF "hours since 1990-01-01" axis, an
|
|
@@ -660,10 +772,14 @@ class CAFrame
|
|
|
660
772
|
# df.to_time("time", unit: :h, epoch: "1990-01-01").set_index("time")
|
|
661
773
|
# df.to_time("time", CATime::Grid.parse("hours since 1990-01-01"))
|
|
662
774
|
# df.to_time("time", CATime::Grid.parse("days since 1980-01-01 12:00"))
|
|
663
|
-
def to_time(
|
|
775
|
+
def to_time(...)
|
|
776
|
+
return dup.__send__(:to_time_in_place, ...)
|
|
777
|
+
end
|
|
778
|
+
|
|
779
|
+
private def to_time_in_place(name, grid = nil, unit: :s, epoch: nil)
|
|
664
780
|
refuse_if_frozen
|
|
665
781
|
key = column_key(name, "to_time")
|
|
666
|
-
col = @columns.fetch(key) {
|
|
782
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
667
783
|
raw = integer_serial_column(col, key)
|
|
668
784
|
grid = unit if unit.is_a?(CATime::Grid)
|
|
669
785
|
if grid.is_a?(CATime::Grid)
|
|
@@ -698,10 +814,27 @@ class CAFrame
|
|
|
698
814
|
# CATimedelta) column. A categorical column's codes are read-only
|
|
699
815
|
# (memo §13.4), so any fill on it raises — rebind a filled copy instead:
|
|
700
816
|
# `df.append(name, df[name].strip_mask(method: :forward))`.
|
|
701
|
-
|
|
817
|
+
#
|
|
818
|
+
# Several columns, or every one, are filled the same way: the method or
|
|
819
|
+
# value comes last, after the columns as +select+ takes them; alone, it
|
|
820
|
+
# fills every column that has a masked cell, as +strip_mask+ does. A Hash
|
|
821
|
+
# fills several columns, each its own way. Every column is checked before
|
|
822
|
+
# any is filled:
|
|
823
|
+
#
|
|
824
|
+
# fill("temp", "wind", :ffill)
|
|
825
|
+
# fill(:numeric, 0)
|
|
826
|
+
# fill(:ffill) # every column with a masked cell
|
|
827
|
+
# fill("temp" => :linear, "wind" => 0.0)
|
|
828
|
+
def fill(*args)
|
|
829
|
+
plan = fill_arguments(args)
|
|
830
|
+
return self if plan.nil?
|
|
831
|
+
if plan.is_a?(Hash)
|
|
832
|
+
return fill_by_map(plan, "fill") { |frame, key, how| frame.fill(key, how) }
|
|
833
|
+
end
|
|
834
|
+
name, method_or_value = plan
|
|
702
835
|
refuse_if_frozen
|
|
703
836
|
key = column_key(name, "fill")
|
|
704
|
-
|
|
837
|
+
refuse_missing_column(key) unless @columns.key?(key)
|
|
705
838
|
col = @columns[key]
|
|
706
839
|
# An N-D column is filled down the rows: each position on its trailing
|
|
707
840
|
# axes is a series of its own.
|
|
@@ -715,17 +848,197 @@ class CAFrame
|
|
|
715
848
|
"fill: unknown method #{method_or_value.inspect} " \
|
|
716
849
|
"(:ffill | :bfill | :linear, or a constant fill value)"
|
|
717
850
|
else
|
|
718
|
-
|
|
851
|
+
fill_constant(key, col, method_or_value)
|
|
719
852
|
end
|
|
720
853
|
self
|
|
721
854
|
end
|
|
722
855
|
|
|
723
|
-
|
|
856
|
+
# A constant fill, in place, naming the column when the value does not go
|
|
857
|
+
# into it, as strip_mask names it.
|
|
858
|
+
private def fill_constant (key, col, value)
|
|
859
|
+
if col.read_only?
|
|
860
|
+
raise "fill: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) can not modify read-only array"
|
|
861
|
+
end
|
|
862
|
+
col.unmask(value)
|
|
863
|
+
rescue ArgumentError, TypeError, RangeError, FloatDomainError => e
|
|
864
|
+
raise e.exception("fill: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) " \
|
|
865
|
+
"cannot take #{value.inspect}: #{e.message}")
|
|
866
|
+
end
|
|
867
|
+
|
|
868
|
+
# A frame with the masked cells filled, this frame left as it is: the
|
|
869
|
+
# sibling of +fill+ that returns a new frame, as CArray#strip_mask is of
|
|
870
|
+
# CArray#unmask. The value or the method is given as CArray#strip_mask
|
|
871
|
+
# takes it, for every column or, with a Hash, column by column:
|
|
872
|
+
#
|
|
873
|
+
# df.strip_mask(0.0) # every column, one value
|
|
874
|
+
# df.strip_mask("temp" => Float::NAN, "n" => -1)
|
|
875
|
+
# df.strip_mask(method: :forward) # every column, one method
|
|
876
|
+
# df.strip_mask(method: { "temp" => :linear, "station" => :forward })
|
|
877
|
+
#
|
|
878
|
+
# A positional Symbol is a value, as in CArray#strip_mask. The methods are
|
|
879
|
+
# :forward and :backward (carry the last or the next present value down the
|
|
880
|
+
# rows; :ffill and :bfill are the same) and :linear, which interpolates
|
|
881
|
+
# against this frame's index when it has one, as +fill+ does, and against
|
|
882
|
+
# the row position when it has none. An N-D column is filled down the rows.
|
|
883
|
+
# A cell a method cannot reach (before the first present value, say) stays
|
|
884
|
+
# masked.
|
|
885
|
+
#
|
|
886
|
+
# The forms for every column fill the columns that have a masked cell and
|
|
887
|
+
# leave the others alone, so one value serves a frame whose complete
|
|
888
|
+
# columns could not hold it (NaN beside a complete integer column). A
|
|
889
|
+
# column named in a Hash is filled whatever it holds. A column that cannot
|
|
890
|
+
# take its value is refused, by name, and no frame is returned.
|
|
891
|
+
#
|
|
892
|
+
# The filled columns are new arrays; the others are this frame's own, shared
|
|
893
|
+
# as +select+ shares them. The index is not filled.
|
|
894
|
+
#
|
|
895
|
+
# @return [CAFrame]
|
|
896
|
+
# @raise [KeyError] for a name that is no column.
|
|
897
|
+
# @raise [ArgumentError] for neither or both of a value and method:, or an
|
|
898
|
+
# unknown method.
|
|
899
|
+
def strip_mask (fill = STRIP_MASK_UNSET, method: nil, **named)
|
|
900
|
+
# df.strip_mask("x" => 0.0) arrives as keywords: the String keys are
|
|
901
|
+
# column names, never an option.
|
|
902
|
+
unless named.empty?
|
|
903
|
+
unless fill.equal?(STRIP_MASK_UNSET)
|
|
904
|
+
raise ArgumentError, "strip_mask: give a fill value or a Hash of them, not both"
|
|
905
|
+
end
|
|
906
|
+
fill = named
|
|
907
|
+
end
|
|
908
|
+
given = !fill.equal?(STRIP_MASK_UNSET)
|
|
909
|
+
if given && method
|
|
910
|
+
raise ArgumentError, "strip_mask: pass either a fill value or method:, not both"
|
|
911
|
+
end
|
|
912
|
+
unless given || method
|
|
913
|
+
raise ArgumentError, "strip_mask: a fill value is required (or method:)"
|
|
914
|
+
end
|
|
915
|
+
how = given ? fill : method
|
|
916
|
+
plan = if how.is_a?(Hash)
|
|
917
|
+
raise ArgumentError, "strip_mask: the Hash names no column" if how.empty?
|
|
918
|
+
keys = column_keys(how.keys, "strip_mask")
|
|
919
|
+
keys.each { |key| refuse_missing_column(key) unless @columns.key?(key) }
|
|
920
|
+
keys.zip(how.values)
|
|
921
|
+
else
|
|
922
|
+
@columns.select { |_, col| col.any_masked? }.keys.map { |key| [key, how] }
|
|
923
|
+
end
|
|
924
|
+
unless given
|
|
925
|
+
plan.each do |_, m|
|
|
926
|
+
next if STRIP_MASK_METHODS.include?(m)
|
|
927
|
+
raise ArgumentError,
|
|
928
|
+
"strip_mask: unknown method #{m.inspect} (:forward | :backward | :linear)"
|
|
929
|
+
end
|
|
930
|
+
end
|
|
931
|
+
cols = @columns.dup
|
|
932
|
+
plan.each do |key, h|
|
|
933
|
+
cols[key] = given ? strip_mask_by_value(key, @columns[key], h)
|
|
934
|
+
: strip_mask_by_method(key, @columns[key], h)
|
|
935
|
+
end
|
|
936
|
+
return rebuild(cols)
|
|
937
|
+
end
|
|
938
|
+
|
|
939
|
+
STRIP_MASK_UNSET = ::Object.new
|
|
940
|
+
Ractor.make_shareable(STRIP_MASK_UNSET)
|
|
941
|
+
private_constant :STRIP_MASK_UNSET
|
|
942
|
+
|
|
943
|
+
STRIP_MASK_METHODS = %i[forward backward ffill bfill linear].freeze
|
|
944
|
+
private_constant :STRIP_MASK_METHODS
|
|
945
|
+
|
|
946
|
+
private def strip_mask_by_value (key, col, value)
|
|
947
|
+
return col.strip_mask(value)
|
|
948
|
+
rescue ArgumentError, TypeError, RangeError, FloatDomainError => e
|
|
949
|
+
raise e.exception("strip_mask: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) " \
|
|
950
|
+
"cannot take #{value.inspect}: #{e.message}")
|
|
951
|
+
end
|
|
952
|
+
|
|
953
|
+
private def strip_mask_by_method (key, col, method)
|
|
954
|
+
if method == :linear && @index
|
|
955
|
+
out = col.copy
|
|
956
|
+
fill_linear_column(key, out, "strip_mask")
|
|
957
|
+
return out
|
|
958
|
+
end
|
|
959
|
+
begin
|
|
960
|
+
return col.strip_mask(method: method, **(col.ndim > 1 ? { axis: 0 } : {}))
|
|
961
|
+
rescue ArgumentError, TypeError => e
|
|
962
|
+
raise e.exception("strip_mask: column #{key.inspect}: #{e.message}")
|
|
963
|
+
end
|
|
964
|
+
end
|
|
965
|
+
|
|
966
|
+
private def linear_call (verb, key)
|
|
967
|
+
return "fill(#{key.inspect}, :linear)" if verb == "fill"
|
|
968
|
+
return "strip_mask(method: :linear) of column #{key.inspect}"
|
|
969
|
+
end
|
|
970
|
+
|
|
971
|
+
# A fill Hash, name => method or value. Each column is filled first on a
|
|
972
|
+
# copy, in a frame holding the copy, by the block (frame, name, how) -- so
|
|
973
|
+
# every check a single fill makes applies -- and the stored columns are
|
|
974
|
+
# written only when every one has passed: a refusal leaves them all as
|
|
975
|
+
# they were. A read-only column is given to the block as it is, to refuse
|
|
976
|
+
# as a single fill does before anything is written.
|
|
977
|
+
# What fill's arguments ask for: [name, how] for one column, or a Hash of
|
|
978
|
+
# name => how for several (the columns named before the method or value,
|
|
979
|
+
# or, with none named, every column with a masked cell, +key_names+ left
|
|
980
|
+
# out; nil when there is none).
|
|
981
|
+
private def fill_arguments (args, key_names = [])
|
|
982
|
+
return args.first if args.size == 1 && args.first.is_a?(Hash)
|
|
983
|
+
if args.empty? || args.first.is_a?(Hash)
|
|
984
|
+
raise ArgumentError,
|
|
985
|
+
"fill: give the columns and a method or value, a method or value alone, " \
|
|
986
|
+
"or one Hash of them"
|
|
987
|
+
end
|
|
988
|
+
*names, how = args
|
|
989
|
+
if names.empty?
|
|
990
|
+
if how.is_a?(String)
|
|
991
|
+
raise ArgumentError,
|
|
992
|
+
"fill: give a method or value after the column #{how.inspect}; " \
|
|
993
|
+
"a String alone is a column name"
|
|
994
|
+
end
|
|
995
|
+
refuse_unknown_fill_method(how, "fill")
|
|
996
|
+
map = @columns.select { |key, col| !key_names.include?(key) && col.any_masked? }
|
|
997
|
+
.keys.to_h { |key| [key, how] }
|
|
998
|
+
return map.empty? ? nil : map
|
|
999
|
+
end
|
|
1000
|
+
return [names.first, how] if names.size == 1 && names.first.is_a?(String)
|
|
1001
|
+
return names_selected(names, "fill").to_h { |key| [key, how] }
|
|
1002
|
+
end
|
|
1003
|
+
|
|
1004
|
+
private def refuse_unknown_fill_method (how, verb)
|
|
1005
|
+
return unless how.is_a?(Symbol) && !%i[ffill bfill linear].include?(how)
|
|
1006
|
+
raise ArgumentError,
|
|
1007
|
+
"#{verb}: unknown method #{how.inspect} (:ffill | :bfill | :linear, or a constant fill value)"
|
|
1008
|
+
end
|
|
1009
|
+
|
|
1010
|
+
private def fill_by_map (map, verb)
|
|
1011
|
+
raise ArgumentError, "#{verb}: the Hash names no column" if map.empty?
|
|
1012
|
+
refuse_if_frozen
|
|
1013
|
+
keys = column_keys(map.keys, verb)
|
|
1014
|
+
keys.each { |key| refuse_missing_column(key) unless @columns.key?(key) }
|
|
1015
|
+
map.each_value { |how| refuse_unknown_fill_method(how, verb) }
|
|
1016
|
+
pairs = keys.zip(map.values)
|
|
1017
|
+
pairs.each { |key, how| yield self, key, how if @columns[key].read_only? }
|
|
1018
|
+
filled = pairs.map do |key, how|
|
|
1019
|
+
next if @columns[key].read_only?
|
|
1020
|
+
probe = rebuild(@columns.merge(key => @columns[key].copy))
|
|
1021
|
+
yield probe, key, how
|
|
1022
|
+
[key, probe[key]]
|
|
1023
|
+
end
|
|
1024
|
+
filled.compact.each { |key, col| write_filled_cells(@columns[key], col) }
|
|
1025
|
+
return self
|
|
1026
|
+
end
|
|
1027
|
+
|
|
1028
|
+
# Only the cells a fill gave a value: a view may show one stored cell
|
|
1029
|
+
# twice, and writing a copy that stayed masked would mask it again.
|
|
1030
|
+
private def write_filled_cells(col, filled)
|
|
1031
|
+
return unless col.has_mask?
|
|
1032
|
+
target = col.is_masked & filled.is_not_masked
|
|
1033
|
+
col[target] = filled[target] if target.any
|
|
1034
|
+
end
|
|
1035
|
+
|
|
1036
|
+
private def fill_linear_column(key, col, verb = "fill")
|
|
724
1037
|
# A Face column interpolates through its own linear_fetch (a time
|
|
725
1038
|
# column's keeps the Face and its unit), as the core gap fill does.
|
|
726
1039
|
unless col.__send__(:interpolable?)
|
|
727
1040
|
raise ArgumentError,
|
|
728
|
-
"
|
|
1041
|
+
"#{linear_call(verb, key)}: numeric or time column required " \
|
|
729
1042
|
"(got #{col.data_type_name})"
|
|
730
1043
|
end
|
|
731
1044
|
# Without an index coordinate x is the cell position, which is exactly what
|
|
@@ -736,20 +1049,20 @@ class CAFrame
|
|
|
736
1049
|
unless @index.numeric? || @index.is_a?(CATime) || @index.is_a?(CATimedelta)
|
|
737
1050
|
along = col.ndim > 1 ? ", axis: 0" : ""
|
|
738
1051
|
raise ArgumentError,
|
|
739
|
-
"
|
|
1052
|
+
"#{linear_call(verb, key)}: the index #{@axis_name.inspect} is " \
|
|
740
1053
|
"#{@index.data_type_name}, not numeric or time, so it cannot be the x of " \
|
|
741
1054
|
"the interpolation; to interpolate by row position, use " \
|
|
742
|
-
"df[#{key.inspect}]
|
|
1055
|
+
"df[#{key.inspect}].#{verb == "fill" ? "unmask" : "strip_mask"}(method: :linear#{along})"
|
|
743
1056
|
end
|
|
744
1057
|
# With the index as x, an N-D column is filled one trailing position at a
|
|
745
1058
|
# time; each position is a 1-D view, so the fill writes through to it.
|
|
746
1059
|
if col.ndim > 1
|
|
747
1060
|
col.shape[1..].map { |d| (0...d).to_a }.inject { |a, b| a.product(b).map { |e| Array(e).flatten } }
|
|
748
|
-
.each { |pos| fill_linear_column(key, col[nil, *Array(pos)]) }
|
|
1061
|
+
.each { |pos| fill_linear_column(key, col[nil, *Array(pos)], verb) }
|
|
749
1062
|
return
|
|
750
1063
|
end
|
|
751
1064
|
# The same fill as the core, with the index as x.
|
|
752
|
-
col
|
|
1065
|
+
write_filled_cells(col, col.send(:linear_fill_along, @index))
|
|
753
1066
|
end
|
|
754
1067
|
|
|
755
1068
|
# Text cells are data, not Ruby literals: an object column cast to a
|
|
@@ -757,8 +1070,20 @@ class CAFrame
|
|
|
757
1070
|
# are not numbers). Other targets go through to_type.
|
|
758
1071
|
private def cast_column(name, type, on_error)
|
|
759
1072
|
key = name.to_s
|
|
760
|
-
col = @columns.fetch(key) {
|
|
1073
|
+
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
761
1074
|
return cast_time_column(key, col, on_error) if type == :time
|
|
1075
|
+
return cast_timedelta_column(key, col, on_error) if type == :timedelta
|
|
1076
|
+
# Text stays text, an object column of Strings becomes a CAString, and
|
|
1077
|
+
# anything else is refused as CArray.string refuses it (a number is
|
|
1078
|
+
# written as text with #format).
|
|
1079
|
+
if type == :string
|
|
1080
|
+
return col if col.is_a?(CArray::StringOperationMixin)
|
|
1081
|
+
begin
|
|
1082
|
+
return CArray.string(col).copy
|
|
1083
|
+
rescue CArray::DataTypeError, TypeError => e
|
|
1084
|
+
raise e.class, "column #{key.inspect} cannot be cast to :string: #{e.message}"
|
|
1085
|
+
end
|
|
1086
|
+
end
|
|
762
1087
|
if type == :categorical
|
|
763
1088
|
return col.is_a?(CACategorical) ? col : col.categorize
|
|
764
1089
|
end
|
|
@@ -849,6 +1174,63 @@ class CAFrame
|
|
|
849
1174
|
parse_time_column(key, col, nil, nil, on_error)
|
|
850
1175
|
end
|
|
851
1176
|
|
|
1177
|
+
# cast(name => :timedelta): the text #to_csv writes for a duration -- a
|
|
1178
|
+
# signed integer and its unit ("194400s", "3M"), or "<n> @ <count> <unit>"
|
|
1179
|
+
# for a unit of several base ticks ("3 @ 10 m") -- to CATimedelta. Cells in
|
|
1180
|
+
# different units are brought to the finest of them; a column that mixes
|
|
1181
|
+
# calendar units (Y, M) with fixed-length ones has no common unit.
|
|
1182
|
+
private def cast_timedelta_column(key, col, on_error)
|
|
1183
|
+
return col if col.is_a?(CATimedelta)
|
|
1184
|
+
unless string_column?(col)
|
|
1185
|
+
raise ArgumentError,
|
|
1186
|
+
"cast: column #{key.inspect} is #{col.data_type}, not text; " \
|
|
1187
|
+
"#timedelta(unit:) reads a column of counts as durations"
|
|
1188
|
+
end
|
|
1189
|
+
cells = time_text_of(col).flatten
|
|
1190
|
+
present = cells.is_not_masked
|
|
1191
|
+
values = Array.new(cells.elements)
|
|
1192
|
+
units = Array.new(cells.elements)
|
|
1193
|
+
bad = []
|
|
1194
|
+
cells.elements.times do |addr|
|
|
1195
|
+
next unless present[addr]
|
|
1196
|
+
text = cells[addr]
|
|
1197
|
+
m = text.is_a?(String) && TIMEDELTA_TEXT.match(text)
|
|
1198
|
+
value = m && Integer(m[1], 10)
|
|
1199
|
+
count = m && (m[2] ? Integer(m[2], 10) : 1)
|
|
1200
|
+
if m && value.bit_length < 64 && count.positive? && count.bit_length < 64
|
|
1201
|
+
values[addr] = value
|
|
1202
|
+
units[addr] = CATime::Resolution.new(count, m[3].to_sym)
|
|
1203
|
+
elsif !text.nil? && !(text.is_a?(String) && text.strip.empty?)
|
|
1204
|
+
bad << addr
|
|
1205
|
+
end
|
|
1206
|
+
end
|
|
1207
|
+
report_unreadable(key, col, :timedelta, bad, on_error) unless bad.empty? || on_error == :mask
|
|
1208
|
+
distinct = units.compact.uniq
|
|
1209
|
+
calendar = distinct.partition { |u| %i[Y M].include?(u.base) }
|
|
1210
|
+
unless calendar.all?(&:empty?) || calendar.any?(&:empty?)
|
|
1211
|
+
raise ArgumentError,
|
|
1212
|
+
"cast: column #{key.inspect} mixes calendar units (Y, M) with " \
|
|
1213
|
+
"fixed-length ones (#{distinct.map(&:to_s).join(', ')}); " \
|
|
1214
|
+
"they have no common unit"
|
|
1215
|
+
end
|
|
1216
|
+
unit = distinct.reduce { |a, b| CATimeUnitAlgebra.finer(a, b) } || CATime::Resolution.new(1, :s)
|
|
1217
|
+
ticks = CArray.int64(cells.elements)
|
|
1218
|
+
ticks[nil] = UNDEF
|
|
1219
|
+
distinct.each do |u|
|
|
1220
|
+
addrs = units.each_index.select { |i| units[i] == u }
|
|
1221
|
+
begin
|
|
1222
|
+
scaled = CATimeUnitAlgebra.convert_scale!(CA_INT64(addrs.map { |i| values[i] }), u, unit)
|
|
1223
|
+
rescue RangeError
|
|
1224
|
+
raise RangeError, "cast: column #{key.inspect} holds durations too long for their finest unit, #{unit}"
|
|
1225
|
+
end
|
|
1226
|
+
ticks[CA_INT64(addrs)] = scaled
|
|
1227
|
+
end
|
|
1228
|
+
ticks.reshape(*col.shape).timedelta(unit: unit)
|
|
1229
|
+
end
|
|
1230
|
+
|
|
1231
|
+
TIMEDELTA_TEXT = /\A\s*([-+]?\d+)\s*(?:@\s*(\d+)\s+)?(Y|M|W|D|h|ms|us|ns|ps|fs|as|m|s)\s*\z/
|
|
1232
|
+
private_constant :TIMEDELTA_TEXT
|
|
1233
|
+
|
|
852
1234
|
# Text to CATime. Without a format only year-first text is read; a
|
|
853
1235
|
# strptime format, or :mixed for a guess at each cell, goes through
|
|
854
1236
|
# CArray.time. Cells that hold something but do not parse are reported as
|
|
@@ -1086,7 +1468,13 @@ class CAFrame
|
|
|
1086
1468
|
# to say about a surface it cannot read, so the widening check -- which
|
|
1087
1469
|
# is about primitive promotion -- applies to plain columns only.
|
|
1088
1470
|
@columns.each { |key, col| refuse_narrowing(key, col, type) unless col.face? }
|
|
1089
|
-
|
|
1471
|
+
# A column that has the type already stays as it is, as promote leaves it
|
|
1472
|
+
# with no target.
|
|
1473
|
+
name = CArray.data_type_name(type)
|
|
1474
|
+
@columns.each_key do |key|
|
|
1475
|
+
col = @columns[key]
|
|
1476
|
+
@columns[key] = col.to_type(type) unless !col.face? && col.data_type_name == name
|
|
1477
|
+
end
|
|
1090
1478
|
end
|
|
1091
1479
|
|
|
1092
1480
|
private def refuse_narrowing(key, col, type)
|