carray 3.0.6 → 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 -1830
- data/README.md +62 -35
- 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 +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- 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 -1504
- 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 +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- 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 +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- 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 +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- 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 +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- 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 +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- 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 +8 -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 +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- 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 +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- 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 +23 -13
- 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 +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- 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 +6 -2
- 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 +10 -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 +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- 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 +10 -5
- 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 +162 -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 +72 -33
- 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 +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- 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 -25387
- 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 -261
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/frame/verbs.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
#
|
|
3
3
|
# A verb that changes which columns the frame has, their names or their
|
|
4
4
|
# types returns a new frame and leaves this one as it is; a verb that edits
|
|
5
|
-
# cells (
|
|
5
|
+
# cells (unmask, mask) edits them in place and returns self. The new frame
|
|
6
6
|
# shares the columns it did not replace, so a write to a cell through it
|
|
7
7
|
# reaches this frame too: only the table (names, index, column types) is its
|
|
8
8
|
# own.
|
|
@@ -53,7 +53,10 @@ class CAFrame
|
|
|
53
53
|
# Remove one or more columns, returning a new frame (column set changes,
|
|
54
54
|
# memo §3.8). Column data is untouched and shared with the original. Each
|
|
55
55
|
# argument is a name, a Range of names or a Regexp, as +select+ takes them.
|
|
56
|
-
|
|
56
|
+
# At least one is given, and no block: rows are dropped by +filter+.
|
|
57
|
+
def drop(*names, &block)
|
|
58
|
+
refuse_column_verb_block("drop") if block
|
|
59
|
+
raise ArgumentError, "drop requires at least one column name" if names.empty?
|
|
57
60
|
cols = @columns.dup
|
|
58
61
|
names_selected(names, "drop").each do |key|
|
|
59
62
|
refuse_missing_column(key) unless cols.key?(key)
|
|
@@ -111,7 +114,7 @@ class CAFrame
|
|
|
111
114
|
#
|
|
112
115
|
# What the block returns becomes the column as it is: a view stays a view
|
|
113
116
|
# of what it was made from, so a write to a cell reaches it (+copy+ cuts
|
|
114
|
-
# that).
|
|
117
|
+
# that). A lazy expression is evaluated, as a column holds its values.
|
|
115
118
|
#
|
|
116
119
|
# @param names [Array<String, Range, Regexp>] the columns; default all.
|
|
117
120
|
# @yieldparam col [CArray] a column.
|
|
@@ -120,7 +123,7 @@ class CAFrame
|
|
|
120
123
|
# @return [CAFrame]
|
|
121
124
|
# @raise [ArgumentError] without a block, or when the block gives a single
|
|
122
125
|
# value or a column of another length.
|
|
123
|
-
def transform
|
|
126
|
+
def transform(*names)
|
|
124
127
|
raise ArgumentError, "transform needs a block that computes each column" unless block_given?
|
|
125
128
|
keys = names.empty? ? @columns.keys : names_selected(names, "transform")
|
|
126
129
|
keys.each { |key| refuse_missing_column(key) unless @columns.key?(key) }
|
|
@@ -190,6 +193,9 @@ class CAFrame
|
|
|
190
193
|
pieces.each { |p| p[] = UNDEF }
|
|
191
194
|
time_text_of(col).to_a.each_with_index do |cell, i|
|
|
192
195
|
next if UNDEF.equal?(cell) || cell.nil?
|
|
196
|
+
unless cell.is_a?(String)
|
|
197
|
+
raise ArgumentError, "split_column: row #{i} of #{key.inspect} holds #{cell.inspect}, which is not text"
|
|
198
|
+
end
|
|
193
199
|
cell.split(sep, n).each_with_index { |part, k| pieces[k][i] = part }
|
|
194
200
|
end
|
|
195
201
|
rebuilt = {}
|
|
@@ -227,6 +233,7 @@ class CAFrame
|
|
|
227
233
|
# names a different number of columns or one twice, or a new name is
|
|
228
234
|
# already a column; and as CACategorical#contrast raises.
|
|
229
235
|
def contrast_columns(name, scheme = :dummy, base: nil, full: false, into: nil)
|
|
236
|
+
full = CArray.send(:bool_arg, full, "full", "contrast_columns")
|
|
230
237
|
key = column_key(name, "contrast_columns")
|
|
231
238
|
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
232
239
|
unless col.ndim == 1
|
|
@@ -327,7 +334,15 @@ class CAFrame
|
|
|
327
334
|
raise ArgumentError,
|
|
328
335
|
"stack_columns: shape: #{layer.inspect} does not hold the #{names.size} columns"
|
|
329
336
|
end
|
|
330
|
-
|
|
337
|
+
CAFrame.__send__(:refuse_rounded_pieces, parts, "stack_columns", "column #{names.first.inspect}")
|
|
338
|
+
begin
|
|
339
|
+
col = CArray.stack(parts, axis: 1)
|
|
340
|
+
rescue ArgumentError => e
|
|
341
|
+
raise unless e.message.start_with?("stack: ")
|
|
342
|
+
raise ArgumentError,
|
|
343
|
+
"stack_columns: the columns do not meet as one column " \
|
|
344
|
+
"(#{e.message.delete_prefix("stack: ")}); cast them to one type first"
|
|
345
|
+
end
|
|
331
346
|
col = col.reshape(nrow, *layer, *trailing) if layer.size > 1
|
|
332
347
|
rebuilt = {}
|
|
333
348
|
@columns.each do |k, v|
|
|
@@ -445,9 +460,9 @@ class CAFrame
|
|
|
445
460
|
# In-place on the column (write-through, §4.3): numeric/object columns
|
|
446
461
|
# mask in place; a categorical column's codes are read-only (§13.4) so this
|
|
447
462
|
# raises — recode by rebuilding and rebinding instead.
|
|
448
|
-
def
|
|
463
|
+
def mask(name, value)
|
|
449
464
|
refuse_if_frozen
|
|
450
|
-
self[column_key(name, "
|
|
465
|
+
self[column_key(name, "mask")][:eq, value] = UNDEF
|
|
451
466
|
self
|
|
452
467
|
end
|
|
453
468
|
|
|
@@ -457,7 +472,7 @@ class CAFrame
|
|
|
457
472
|
# value the type cannot hold, becomes UNDEF (parse-mask, §6-2). A text
|
|
458
473
|
# column cast to :time is parsed into a CATime column, in the finest unit
|
|
459
474
|
# its text shows (:D for dates alone, :s with a time of day, :ms / :us /
|
|
460
|
-
# :ns for fractions of a second);
|
|
475
|
+
# :ns for fractions of a second); parse_time takes a format and a
|
|
461
476
|
# unit. A text column cast to :timedelta reads the text to_csv writes for
|
|
462
477
|
# a duration ("194400s", "3M", "3 @ 10 m") into a CATimedelta column, in
|
|
463
478
|
# the finest unit its cells show. A column cast to :categorical becomes a CACategorical of its
|
|
@@ -548,7 +563,7 @@ class CAFrame
|
|
|
548
563
|
end
|
|
549
564
|
resolve_cast_map(name_or_map, label)
|
|
550
565
|
else
|
|
551
|
-
{ column_key(name_or_map, label) => type }
|
|
566
|
+
resolve_cast_map({ column_key(name_or_map, label) => type }, label)
|
|
552
567
|
end
|
|
553
568
|
# Read every column before rebinding any, so a raise leaves the frame as
|
|
554
569
|
# it was.
|
|
@@ -579,13 +594,15 @@ class CAFrame
|
|
|
579
594
|
named.each_key do |name|
|
|
580
595
|
refuse_missing_column(name, label.chomp(":")) unless @columns.key?(name)
|
|
581
596
|
end
|
|
582
|
-
|
|
597
|
+
inferred, named = named.partition { |_, t| t == :infer }.map(&:to_h)
|
|
598
|
+
rest = @columns.keys - named.keys - inferred.keys
|
|
583
599
|
types =
|
|
584
600
|
case default
|
|
585
601
|
when nil then {}
|
|
586
602
|
when :infer then rest.empty? ? {} : select(*rest).infer_types
|
|
587
603
|
else rest.to_h { |name| [name, default] }
|
|
588
604
|
end
|
|
605
|
+
types.merge!(select(*inferred.keys).infer_types) unless inferred.empty?
|
|
589
606
|
types.merge!(named.compact)
|
|
590
607
|
end
|
|
591
608
|
|
|
@@ -610,11 +627,13 @@ class CAFrame
|
|
|
610
627
|
# none present is not listed.
|
|
611
628
|
#
|
|
612
629
|
# A number with a leading zero ("007") is a code and keeps its column as
|
|
613
|
-
# text, and so does an integer too long for int64 (an identifier),
|
|
614
|
-
# float would round. A day-first or
|
|
615
|
-
# read as time, since which one it
|
|
616
|
-
# are looked at -- an object column
|
|
617
|
-
# CAConstString); columns that already have a
|
|
630
|
+
# text, and so does an integer too long for int64 (an identifier), or one
|
|
631
|
+
# past 2**53 among decimals, which a float would round. A day-first or
|
|
632
|
+
# month-first date ("01/02/2024") is not read as time, since which one it
|
|
633
|
+
# is cannot be told. Only text columns are looked at -- an object column
|
|
634
|
+
# or a string Face (CAString, CAConstString); columns that already have a
|
|
635
|
+
# type are not listed. An object column's cells are read as their text,
|
|
636
|
+
# so Ruby numbers there are typed as that text would be.
|
|
618
637
|
#
|
|
619
638
|
# df.cast(df.infer_types) # what cast(:infer) does
|
|
620
639
|
# df.cast(df.infer_types.merge("code" => :int32)) # as cast(default: :infer, "code" => :int32)
|
|
@@ -622,18 +641,30 @@ class CAFrame
|
|
|
622
641
|
@columns.each_with_object({}) do |(key, col), types|
|
|
623
642
|
next unless string_column?(col)
|
|
624
643
|
type = text_for_decimal_reading(col).__infer_number_type_of_text__
|
|
644
|
+
next if type == :float64 && integer_text_past_float?(col)
|
|
625
645
|
type ||= :boolean if boolean_word_column?(col)
|
|
626
646
|
type ||= :time if first_present_cell_can_be_time_text?(col) && time_text_column?(time_text_of(col))
|
|
627
647
|
types[key] = type if type
|
|
628
648
|
end
|
|
629
649
|
end
|
|
630
650
|
|
|
651
|
+
# Whether a cell is an integer a float64 would round. Fewer than 16 digits
|
|
652
|
+
# cannot be, which leaves a const string column's other cells unread.
|
|
653
|
+
private def integer_text_past_float?(col)
|
|
654
|
+
long = col.is_a?(CAConstString) ? col[col.bytesize.ge(16).strip_mask(false)].to_a : time_text_of(col).to_a
|
|
655
|
+
long.any? do |text|
|
|
656
|
+
next text.abs > 2**53 if text.is_a?(Integer) # an object cell read as its text
|
|
657
|
+
text.is_a?(String) && text.match?(/\A\s*[-+]?\d{16,}\s*\z/) && Integer(text.strip, 10).abs > 2**53
|
|
658
|
+
end
|
|
659
|
+
end
|
|
660
|
+
|
|
631
661
|
# Bring every column to one common data type and rebind them (memo §11.4).
|
|
632
662
|
# The frame-level counterpart of +CArray.promote_list+: where +cast+ forces
|
|
633
663
|
# named columns to a type, +promote+ widens the whole table until it has a
|
|
634
|
-
# single type. Returns a new frame;
|
|
635
|
-
#
|
|
636
|
-
#
|
|
664
|
+
# single type. Returns a new frame; a column that changes type is a fresh
|
|
665
|
+
# one, and one that has the type already is shared with this frame, as
|
|
666
|
+
# +promote+ with no argument leaves it. The index is not a column and is
|
|
667
|
+
# left alone.
|
|
637
668
|
#
|
|
638
669
|
# df.promote # the common type CArray.result_type would pick
|
|
639
670
|
# df.promote(:object) # force the widest type: anything fits
|
|
@@ -681,31 +712,31 @@ class CAFrame
|
|
|
681
712
|
# does. Make it the index with +set_index+ afterward.
|
|
682
713
|
# +cast(name => :time)+ is the call without a format.
|
|
683
714
|
#
|
|
684
|
-
# df.
|
|
685
|
-
# df.
|
|
686
|
-
# df.
|
|
687
|
-
def
|
|
688
|
-
return dup.__send__(:
|
|
715
|
+
# df.parse_time("time").set_index("time")
|
|
716
|
+
# df.parse_time("date", "%d/%m/%Y")
|
|
717
|
+
# df.parse_time("date", :infer)
|
|
718
|
+
def parse_time(...)
|
|
719
|
+
return dup.__send__(:parse_time_in_place, ...)
|
|
689
720
|
end
|
|
690
721
|
|
|
691
|
-
private def
|
|
722
|
+
private def parse_time_in_place(name, format = nil, unit: nil, on_error: :mask)
|
|
692
723
|
refuse_if_frozen
|
|
693
724
|
unless CAST_ON_ERROR.include?(on_error)
|
|
694
725
|
raise ArgumentError,
|
|
695
726
|
"on_error: must be :mask, :warn or :raise (got #{on_error.inspect})"
|
|
696
727
|
end
|
|
697
|
-
key = column_key(name, "
|
|
728
|
+
key = column_key(name, "parse_time")
|
|
698
729
|
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
699
730
|
unless string_column?(col)
|
|
700
731
|
raise ArgumentError,
|
|
701
|
-
"
|
|
732
|
+
"parse_time needs a string column (object / CAString / " \
|
|
702
733
|
"CAConstString / CAFixlenString); #{key.inspect} is #{col.data_type}"
|
|
703
734
|
end
|
|
704
735
|
@columns[key] = col.__send__(:carry_attrs, parse_time_column(key, col, format, unit, on_error))
|
|
705
736
|
self
|
|
706
737
|
end
|
|
707
738
|
|
|
708
|
-
# The strptime format
|
|
739
|
+
# The strptime format parse_time(name, :infer) reads the column with,
|
|
709
740
|
# or nil when the text is written year first and needs none. The first
|
|
710
741
|
# present cell gives the candidates and each later cell drops those it
|
|
711
742
|
# does not fit, until one is left. CAFrame::UnreadableColumn when none fits
|
|
@@ -726,117 +757,130 @@ class CAFrame
|
|
|
726
757
|
|
|
727
758
|
# A new frame with an integer column read as time serial counts (memo
|
|
728
759
|
# §11.2); this frame keeps the integer column. This is the "serial -> time" mode: each value is a count of
|
|
729
|
-
# +unit+ resolution since +
|
|
730
|
-
# +
|
|
760
|
+
# +unit+ resolution since +origin+ (default the Unix epoch, 1970-01-01 UTC).
|
|
761
|
+
# +origin+ takes any time literal (String / Time / Date / element), so a column
|
|
731
762
|
# measured from another origin — a netCDF "hours since 1990-01-01" axis, an
|
|
732
|
-
# Excel serial date (
|
|
763
|
+
# Excel serial date (origin "1899-12-30", unit :D) — converts directly.
|
|
733
764
|
#
|
|
734
765
|
# A float column is accepted only when every value is whole (no fractional
|
|
735
766
|
# part); a fractional serial has sub-unit precision that a finer +unit+
|
|
736
767
|
# should carry, so it raises CAFrame::UnreadableColumn rather than
|
|
737
768
|
# silently truncate. Make it the index with +set_index+ afterward.
|
|
738
769
|
#
|
|
739
|
-
# A +CATime::Grid+ carries the same (unit,
|
|
740
|
-
# netCDF +units+ attribute goes straight in
|
|
741
|
-
#
|
|
742
|
-
#
|
|
770
|
+
# A +CATime::Grid+ given as +unit:+ carries the same (unit, origin) pair as
|
|
771
|
+
# one value, so a netCDF +units+ attribute goes straight in; +origin:+ is
|
|
772
|
+
# then refused, since the grid has its own. It also carries a phase the
|
|
773
|
+
# keyword form cannot: the keyword +origin+ is read on the +unit+ grid, so
|
|
774
|
+
# an origin off that grid ("days since 1980-01-01 12:00") loses its
|
|
743
775
|
# time-of-day, while a grid resolves the finer storage that holds it.
|
|
744
776
|
#
|
|
745
|
-
# df.to_time("time", unit: :h,
|
|
746
|
-
# df.to_time("time", CATime::Grid.parse("hours since 1990-01-01"))
|
|
747
|
-
# df.to_time("time", CATime::Grid.parse("days since 1980-01-01 12:00"))
|
|
777
|
+
# df.to_time("time", unit: :h, origin: "1990-01-01").set_index("time")
|
|
778
|
+
# df.to_time("time", unit: CATime::Grid.parse("hours since 1990-01-01"))
|
|
779
|
+
# df.to_time("time", unit: CATime::Grid.parse("days since 1980-01-01 12:00"))
|
|
748
780
|
def to_time(...)
|
|
749
781
|
return dup.__send__(:to_time_in_place, ...)
|
|
750
782
|
end
|
|
751
783
|
|
|
752
|
-
private def to_time_in_place(name,
|
|
784
|
+
private def to_time_in_place(name, unit: :s, origin: nil)
|
|
753
785
|
refuse_if_frozen
|
|
754
786
|
key = column_key(name, "to_time")
|
|
755
787
|
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
756
788
|
raw = integer_serial_column(col, key)
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
@columns[key] = col.__send__(:carry_attrs,
|
|
789
|
+
if unit.is_a?(CATime::Grid)
|
|
790
|
+
CATime::Grid.resolve(unit, origin, "to_time")
|
|
791
|
+
@columns[key] = col.__send__(:carry_attrs, unit.at(raw))
|
|
760
792
|
return self
|
|
761
793
|
end
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
"the positional argument must be a CATime::Grid (got #{grid.class})"
|
|
765
|
-
end
|
|
766
|
-
if epoch
|
|
767
|
-
# An Integer epoch is an instant in Unix seconds, whatever the unit.
|
|
768
|
-
raw = raw + CATimeLiteral.to_time_array(epoch, CATime::Resolution.parse(unit), nil, :raise).ticks[0]
|
|
794
|
+
if origin
|
|
795
|
+
raw = raw + CATime::Literal.to_time_array(origin, CATime::Resolution.parse(unit), nil, :raise).ticks[0]
|
|
769
796
|
end
|
|
770
797
|
@columns[key] = col.__send__(:carry_attrs, raw.time(unit: unit))
|
|
771
798
|
self
|
|
772
799
|
end
|
|
773
800
|
|
|
774
|
-
# Fill masked cells of a column
|
|
775
|
-
#
|
|
776
|
-
#
|
|
777
|
-
#
|
|
801
|
+
# Fill the masked cells of a column, in place (the frame's +unmask+, as
|
|
802
|
+
# +CArray#unmask+ is the array's; +strip_mask+ is the form that returns a
|
|
803
|
+
# new frame). The value or the method is given as CArray#unmask takes
|
|
804
|
+
# it: a value after the columns, or the method as +method:+. A positional
|
|
805
|
+
# Symbol is a value, as in CArray#unmask. The fill is write-through: it
|
|
806
|
+
# edits the live shared column rather than rebinding a filled copy.
|
|
807
|
+
# Returns self so calls chain.
|
|
778
808
|
#
|
|
779
|
-
#
|
|
780
|
-
#
|
|
781
|
-
#
|
|
782
|
-
#
|
|
783
|
-
#
|
|
809
|
+
# unmask("temp", method: :forward) # carry the last present value
|
|
810
|
+
# unmask("temp", method: :backward) # carry the next present value
|
|
811
|
+
# unmask("temp", method: :linear) # linear interpolation; x = this frame's
|
|
812
|
+
# # index when present, else the row position
|
|
813
|
+
# unmask("temp", 0.0) # constant fill
|
|
784
814
|
#
|
|
785
|
-
# :
|
|
815
|
+
# :forward / :backward work for any writable column data_type (numeric, time,
|
|
786
816
|
# object, fixlen, …); :linear needs a numeric or time (CATime /
|
|
787
817
|
# CATimedelta) column. A categorical column's codes are read-only
|
|
788
818
|
# (memo §13.4), so any fill on it raises — rebind a filled copy instead:
|
|
789
819
|
# `df.append(name, df[name].strip_mask(method: :forward))`.
|
|
790
820
|
#
|
|
791
|
-
# Several columns, or every one, are filled the same way: the
|
|
792
|
-
#
|
|
793
|
-
#
|
|
794
|
-
# fills several columns, each its own
|
|
795
|
-
#
|
|
821
|
+
# Several columns, or every one, are filled the same way: the columns come
|
|
822
|
+
# first, as +select+ takes them, then the value (or +method:+); with no
|
|
823
|
+
# column named, every column that has a masked cell is filled, as
|
|
824
|
+
# +strip_mask+ does. A Hash fills several columns, each with its own value,
|
|
825
|
+
# and +method:+ takes a Hash of methods the same way. Every column is
|
|
826
|
+
# checked before any is filled:
|
|
796
827
|
#
|
|
797
|
-
#
|
|
798
|
-
#
|
|
799
|
-
#
|
|
800
|
-
#
|
|
801
|
-
|
|
802
|
-
|
|
828
|
+
# unmask("temp", "wind", method: :forward)
|
|
829
|
+
# unmask(:numeric, 0)
|
|
830
|
+
# unmask(method: :forward) # every column with a masked cell
|
|
831
|
+
# unmask("temp" => 0.0, "wind" => 0.0)
|
|
832
|
+
# unmask(method: { "temp" => :linear, "station" => :forward })
|
|
833
|
+
def unmask(*args, method: nil, **named)
|
|
834
|
+
plan = fill_arguments(args, named, method)
|
|
803
835
|
return self if plan.nil?
|
|
804
836
|
if plan.is_a?(Hash)
|
|
805
|
-
return fill_by_map(plan, "
|
|
837
|
+
return fill_by_map(plan, "unmask") { |frame, key, how| frame.__send__(:unmask_column, key, how) }
|
|
806
838
|
end
|
|
807
|
-
|
|
839
|
+
unmask_column(*plan)
|
|
840
|
+
end
|
|
841
|
+
|
|
842
|
+
# How a column is filled by a method (:forward, :backward, :linear), as
|
|
843
|
+
# against by a value, which is any other object, a Symbol included.
|
|
844
|
+
FillMethod = Data.define(:name)
|
|
845
|
+
private_constant :FillMethod
|
|
846
|
+
|
|
847
|
+
private def unmask_column(name, how)
|
|
808
848
|
refuse_if_frozen
|
|
809
|
-
key = column_key(name, "
|
|
849
|
+
key = column_key(name, "unmask")
|
|
810
850
|
refuse_missing_column(key) unless @columns.key?(key)
|
|
811
851
|
col = @columns[key]
|
|
812
852
|
# An N-D column is filled down the rows: each position on its trailing
|
|
813
853
|
# axes is a series of its own.
|
|
814
854
|
along = col.ndim > 1 ? { axis: 0 } : {}
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
raise ArgumentError,
|
|
821
|
-
"fill: unknown method #{method_or_value.inspect} " \
|
|
822
|
-
"(:ffill | :bfill | :linear, or a constant fill value)"
|
|
855
|
+
if how.is_a?(FillMethod)
|
|
856
|
+
case how.name
|
|
857
|
+
when :forward, :backward then col.unmask(method: how.name, **along)
|
|
858
|
+
when :linear then fill_linear_column(key, col)
|
|
859
|
+
end
|
|
823
860
|
else
|
|
824
|
-
fill_constant(key, col,
|
|
861
|
+
fill_constant(key, col, how)
|
|
825
862
|
end
|
|
826
863
|
self
|
|
827
864
|
end
|
|
828
865
|
|
|
829
866
|
# A constant fill, in place, naming the column when the value does not go
|
|
830
867
|
# into it, as strip_mask names it.
|
|
831
|
-
private def fill_constant
|
|
868
|
+
private def fill_constant(key, col, value)
|
|
869
|
+
if col.read_only?
|
|
870
|
+
raise "unmask: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) can not modify read-only array"
|
|
871
|
+
end
|
|
832
872
|
col.unmask(value)
|
|
833
873
|
rescue ArgumentError, TypeError, RangeError, FloatDomainError => e
|
|
834
|
-
|
|
835
|
-
|
|
874
|
+
hint = FILL_METHOD_NAMES.include?(value) ? "; to fill by a method, pass method: #{value.inspect}" : ""
|
|
875
|
+
raise e.exception("unmask: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) " \
|
|
876
|
+
"cannot take #{value.inspect}: #{e.message}#{hint}")
|
|
836
877
|
end
|
|
837
878
|
|
|
879
|
+
FILL_METHOD_NAMES = %i[forward backward linear].freeze
|
|
880
|
+
private_constant :FILL_METHOD_NAMES
|
|
881
|
+
|
|
838
882
|
# A frame with the masked cells filled, this frame left as it is: the
|
|
839
|
-
# sibling of +
|
|
883
|
+
# sibling of +unmask+ that returns a new frame, as CArray#strip_mask is of
|
|
840
884
|
# CArray#unmask. The value or the method is given as CArray#strip_mask
|
|
841
885
|
# takes it, for every column or, with a Hash, column by column:
|
|
842
886
|
#
|
|
@@ -847,8 +891,8 @@ class CAFrame
|
|
|
847
891
|
#
|
|
848
892
|
# A positional Symbol is a value, as in CArray#strip_mask. The methods are
|
|
849
893
|
# :forward and :backward (carry the last or the next present value down the
|
|
850
|
-
# rows; :
|
|
851
|
-
# against this frame's index when it has one, as +
|
|
894
|
+
# rows; :forward and :backward are the same) and :linear, which interpolates
|
|
895
|
+
# against this frame's index when it has one, as +unmask+ does, and against
|
|
852
896
|
# the row position when it has none. An N-D column is filled down the rows.
|
|
853
897
|
# A cell a method cannot reach (before the first present value, say) stays
|
|
854
898
|
# masked.
|
|
@@ -866,7 +910,7 @@ class CAFrame
|
|
|
866
910
|
# @raise [KeyError] for a name that is no column.
|
|
867
911
|
# @raise [ArgumentError] for neither or both of a value and method:, or an
|
|
868
912
|
# unknown method.
|
|
869
|
-
def strip_mask
|
|
913
|
+
def strip_mask(fill = STRIP_MASK_UNSET, method: nil, **named)
|
|
870
914
|
# df.strip_mask("x" => 0.0) arrives as keywords: the String keys are
|
|
871
915
|
# column names, never an option.
|
|
872
916
|
unless named.empty?
|
|
@@ -910,17 +954,17 @@ class CAFrame
|
|
|
910
954
|
Ractor.make_shareable(STRIP_MASK_UNSET)
|
|
911
955
|
private_constant :STRIP_MASK_UNSET
|
|
912
956
|
|
|
913
|
-
STRIP_MASK_METHODS = %i[forward backward
|
|
957
|
+
STRIP_MASK_METHODS = %i[forward backward linear].freeze
|
|
914
958
|
private_constant :STRIP_MASK_METHODS
|
|
915
959
|
|
|
916
|
-
private def strip_mask_by_value
|
|
960
|
+
private def strip_mask_by_value(key, col, value)
|
|
917
961
|
return col.strip_mask(value)
|
|
918
962
|
rescue ArgumentError, TypeError, RangeError, FloatDomainError => e
|
|
919
963
|
raise e.exception("strip_mask: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) " \
|
|
920
964
|
"cannot take #{value.inspect}: #{e.message}")
|
|
921
965
|
end
|
|
922
966
|
|
|
923
|
-
private def strip_mask_by_method
|
|
967
|
+
private def strip_mask_by_method(key, col, method)
|
|
924
968
|
if method == :linear && @index
|
|
925
969
|
out = col.copy
|
|
926
970
|
fill_linear_column(key, out, "strip_mask")
|
|
@@ -933,8 +977,8 @@ class CAFrame
|
|
|
933
977
|
end
|
|
934
978
|
end
|
|
935
979
|
|
|
936
|
-
private def linear_call
|
|
937
|
-
return "
|
|
980
|
+
private def linear_call(verb, key)
|
|
981
|
+
return "unmask(#{key.inspect}, method: :linear)" if verb == "unmask"
|
|
938
982
|
return "strip_mask(method: :linear) of column #{key.inspect}"
|
|
939
983
|
end
|
|
940
984
|
|
|
@@ -944,42 +988,60 @@ class CAFrame
|
|
|
944
988
|
# written only when every one has passed: a refusal leaves them all as
|
|
945
989
|
# they were. A read-only column is given to the block as it is, to refuse
|
|
946
990
|
# as a single fill does before anything is written.
|
|
947
|
-
# What
|
|
948
|
-
# name => how for several (the columns named before the
|
|
949
|
-
# or, with none named, every column with a masked cell,
|
|
950
|
-
# out; nil when there is none).
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
991
|
+
# What unmask's arguments ask for: [name, how] for one column, or a Hash of
|
|
992
|
+
# name => how for several (the columns named before the value, or named
|
|
993
|
+
# with method:, or, with none named, every column with a masked cell,
|
|
994
|
+
# +key_names+ left out; nil when there is none). A method is held as a
|
|
995
|
+
# FillMethod, and any other +how+ is a value. +named+ is a Hash of values
|
|
996
|
+
# given as keywords (unmask("temp" => 0.0)).
|
|
997
|
+
private def fill_arguments(args, named, method, key_names = [])
|
|
998
|
+
unless named.empty?
|
|
999
|
+
if !args.empty? || method
|
|
1000
|
+
raise ArgumentError, "unmask: give a Hash of values alone, without other columns or method:"
|
|
1001
|
+
end
|
|
1002
|
+
return named
|
|
957
1003
|
end
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
if
|
|
1004
|
+
if method.is_a?(Hash)
|
|
1005
|
+
raise ArgumentError, "unmask: give a Hash of methods alone, without columns" unless args.empty?
|
|
1006
|
+
raise ArgumentError, "unmask: the Hash names no column" if method.empty?
|
|
1007
|
+
method.each_value { |m| refuse_unknown_fill_method(m, "unmask") }
|
|
1008
|
+
return method.transform_values { |m| FillMethod.new(m) }
|
|
1009
|
+
end
|
|
1010
|
+
if method
|
|
1011
|
+
refuse_unknown_fill_method(method, "unmask")
|
|
1012
|
+
names, how = args, FillMethod.new(method)
|
|
1013
|
+
else
|
|
1014
|
+
return args.first if args.size == 1 && args.first.is_a?(Hash)
|
|
1015
|
+
if args.empty? || args.first.is_a?(Hash)
|
|
961
1016
|
raise ArgumentError,
|
|
962
|
-
"
|
|
1017
|
+
"unmask: give the columns and a value, a value alone, method:, or one Hash of values"
|
|
1018
|
+
end
|
|
1019
|
+
*names, how = args
|
|
1020
|
+
if names.empty? && how.is_a?(String)
|
|
1021
|
+
raise ArgumentError,
|
|
1022
|
+
"unmask: give a value or method: after the column #{how.inspect}; " \
|
|
963
1023
|
"a String alone is a column name"
|
|
964
1024
|
end
|
|
1025
|
+
end
|
|
1026
|
+
if names.empty?
|
|
965
1027
|
map = @columns.select { |key, col| !key_names.include?(key) && col.any_masked? }
|
|
966
1028
|
.keys.to_h { |key| [key, how] }
|
|
967
1029
|
return map.empty? ? nil : map
|
|
968
1030
|
end
|
|
969
1031
|
return [names.first, how] if names.size == 1 && names.first.is_a?(String)
|
|
970
|
-
return names_selected(names, "
|
|
1032
|
+
return names_selected(names, "unmask").to_h { |key| [key, how] }
|
|
971
1033
|
end
|
|
972
1034
|
|
|
973
|
-
private def
|
|
1035
|
+
private def refuse_unknown_fill_method(how, verb)
|
|
1036
|
+
return if FILL_METHOD_NAMES.include?(how)
|
|
1037
|
+
raise ArgumentError, "#{verb}: unknown method #{how.inspect} (:forward | :backward | :linear)"
|
|
1038
|
+
end
|
|
1039
|
+
|
|
1040
|
+
private def fill_by_map(map, verb)
|
|
974
1041
|
raise ArgumentError, "#{verb}: the Hash names no column" if map.empty?
|
|
975
1042
|
refuse_if_frozen
|
|
976
1043
|
keys = column_keys(map.keys, verb)
|
|
977
1044
|
keys.each { |key| refuse_missing_column(key) unless @columns.key?(key) }
|
|
978
|
-
map.each_value do |how|
|
|
979
|
-
next unless how.is_a?(Symbol) && !%i[ffill bfill linear].include?(how)
|
|
980
|
-
raise ArgumentError,
|
|
981
|
-
"#{verb}: unknown method #{how.inspect} (:ffill | :bfill | :linear, or a constant fill value)"
|
|
982
|
-
end
|
|
983
1045
|
pairs = keys.zip(map.values)
|
|
984
1046
|
pairs.each { |key, how| yield self, key, how if @columns[key].read_only? }
|
|
985
1047
|
filled = pairs.map do |key, how|
|
|
@@ -988,11 +1050,19 @@ class CAFrame
|
|
|
988
1050
|
yield probe, key, how
|
|
989
1051
|
[key, probe[key]]
|
|
990
1052
|
end
|
|
991
|
-
filled.compact.each { |key, col| @columns[key]
|
|
1053
|
+
filled.compact.each { |key, col| write_filled_cells(@columns[key], col) }
|
|
992
1054
|
return self
|
|
993
1055
|
end
|
|
994
1056
|
|
|
995
|
-
|
|
1057
|
+
# Only the cells a fill gave a value: a view may show one stored cell
|
|
1058
|
+
# twice, and writing a copy that stayed masked would mask it again.
|
|
1059
|
+
private def write_filled_cells(col, filled)
|
|
1060
|
+
return unless col.has_mask?
|
|
1061
|
+
target = col.is_masked & filled.is_not_masked
|
|
1062
|
+
col[target] = filled[target] if target.any
|
|
1063
|
+
end
|
|
1064
|
+
|
|
1065
|
+
private def fill_linear_column(key, col, verb = "unmask")
|
|
996
1066
|
# A Face column interpolates through its own linear_fetch (a time
|
|
997
1067
|
# column's keeps the Face and its unit), as the core gap fill does.
|
|
998
1068
|
unless col.__send__(:interpolable?)
|
|
@@ -1011,7 +1081,7 @@ class CAFrame
|
|
|
1011
1081
|
"#{linear_call(verb, key)}: the index #{@axis_name.inspect} is " \
|
|
1012
1082
|
"#{@index.data_type_name}, not numeric or time, so it cannot be the x of " \
|
|
1013
1083
|
"the interpolation; to interpolate by row position, use " \
|
|
1014
|
-
"df[#{key.inspect}].#{verb
|
|
1084
|
+
"df[#{key.inspect}].#{verb}(method: :linear#{along})"
|
|
1015
1085
|
end
|
|
1016
1086
|
# With the index as x, an N-D column is filled one trailing position at a
|
|
1017
1087
|
# time; each position is a 1-D view, so the fill writes through to it.
|
|
@@ -1021,7 +1091,7 @@ class CAFrame
|
|
|
1021
1091
|
return
|
|
1022
1092
|
end
|
|
1023
1093
|
# The same fill as the core, with the index as x.
|
|
1024
|
-
col
|
|
1094
|
+
write_filled_cells(col, col.send(:linear_fill_along, @index))
|
|
1025
1095
|
end
|
|
1026
1096
|
|
|
1027
1097
|
# Text cells are data, not Ruby literals: an object column cast to a
|
|
@@ -1038,7 +1108,7 @@ class CAFrame
|
|
|
1038
1108
|
if type == :string
|
|
1039
1109
|
return col if col.is_a?(CArray::StringOperationMixin)
|
|
1040
1110
|
begin
|
|
1041
|
-
return CArray.string(col)
|
|
1111
|
+
return CArray.string(col).copy
|
|
1042
1112
|
rescue CArray::DataTypeError, TypeError => e
|
|
1043
1113
|
raise e.class, "column #{key.inspect} cannot be cast to :string: #{e.message}"
|
|
1044
1114
|
end
|
|
@@ -1154,9 +1224,11 @@ class CAFrame
|
|
|
1154
1224
|
next unless present[addr]
|
|
1155
1225
|
text = cells[addr]
|
|
1156
1226
|
m = text.is_a?(String) && TIMEDELTA_TEXT.match(text)
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1227
|
+
value = m && Integer(m[1], 10)
|
|
1228
|
+
count = m && (m[2] ? Integer(m[2], 10) : 1)
|
|
1229
|
+
if m && value.bit_length < 64 && count.positive? && count.bit_length < 64
|
|
1230
|
+
values[addr] = value
|
|
1231
|
+
units[addr] = CATime::Resolution.new(count, m[3].to_sym)
|
|
1160
1232
|
elsif !text.nil? && !(text.is_a?(String) && text.strip.empty?)
|
|
1161
1233
|
bad << addr
|
|
1162
1234
|
end
|
|
@@ -1170,12 +1242,16 @@ class CAFrame
|
|
|
1170
1242
|
"fixed-length ones (#{distinct.map(&:to_s).join(', ')}); " \
|
|
1171
1243
|
"they have no common unit"
|
|
1172
1244
|
end
|
|
1173
|
-
unit = distinct.reduce { |a, b|
|
|
1245
|
+
unit = distinct.reduce { |a, b| CATime::UnitAlgebra.finer(a, b) } || CATime::Resolution.new(1, :s)
|
|
1174
1246
|
ticks = CArray.int64(cells.elements)
|
|
1175
1247
|
ticks[nil] = UNDEF
|
|
1176
1248
|
distinct.each do |u|
|
|
1177
1249
|
addrs = units.each_index.select { |i| units[i] == u }
|
|
1178
|
-
|
|
1250
|
+
begin
|
|
1251
|
+
scaled = CATime::UnitAlgebra.convert_scale!(CA_INT64(addrs.map { |i| values[i] }), u, unit)
|
|
1252
|
+
rescue RangeError
|
|
1253
|
+
raise RangeError, "cast: column #{key.inspect} holds durations too long for their finest unit, #{unit}"
|
|
1254
|
+
end
|
|
1179
1255
|
ticks[CA_INT64(addrs)] = scaled
|
|
1180
1256
|
end
|
|
1181
1257
|
ticks.reshape(*col.shape).timedelta(unit: unit)
|
|
@@ -1192,10 +1268,10 @@ class CAFrame
|
|
|
1192
1268
|
case format
|
|
1193
1269
|
when nil then read_time_text(key, col, unit, on_error)
|
|
1194
1270
|
when String
|
|
1195
|
-
parse_time_by_format(key, col, format, unit ||
|
|
1271
|
+
parse_time_by_format(key, col, format, unit || CATime::Literal.format_unit(format), on_error)
|
|
1196
1272
|
when :infer then parse_time_inferred(key, col, unit)
|
|
1197
1273
|
when :mixed
|
|
1198
|
-
unit ||=
|
|
1274
|
+
unit ||= CATime::Literal.unit_shown(time_text_of(col), nil)
|
|
1199
1275
|
parse_time_by_format(key, col, nil, unit, on_error)
|
|
1200
1276
|
else
|
|
1201
1277
|
raise ArgumentError,
|
|
@@ -1204,7 +1280,7 @@ class CAFrame
|
|
|
1204
1280
|
end
|
|
1205
1281
|
rescue RangeError => e
|
|
1206
1282
|
raise unless e.message.include?("does not fit int64 ticks")
|
|
1207
|
-
raise RangeError, "column #{key.inspect}: #{e.message};
|
|
1283
|
+
raise RangeError, "column #{key.inspect}: #{e.message}; parse_time takes " \
|
|
1208
1284
|
"unit: to read it in a coarser unit (:us spans 292,000 years)"
|
|
1209
1285
|
end
|
|
1210
1286
|
|
|
@@ -1226,10 +1302,10 @@ class CAFrame
|
|
|
1226
1302
|
digits > 6 ? :ns : digits > 3 ? :us : :ms
|
|
1227
1303
|
end
|
|
1228
1304
|
res = CATime::Resolution.parse(unit)
|
|
1229
|
-
unless
|
|
1305
|
+
unless CATime::Literal.tick_kind(res)
|
|
1230
1306
|
return parse_time_by_format(key, col, f.format, unit, :raise)
|
|
1231
1307
|
end
|
|
1232
|
-
|
|
1308
|
+
CATime::Literal.ticks_from_fields(fields, col, res, f.format, :raise).time(unit: res)
|
|
1233
1309
|
end
|
|
1234
1310
|
|
|
1235
1311
|
# [format, fields, address of the first cell not in it] for :infer, or nil
|
|
@@ -1250,7 +1326,7 @@ class CAFrame
|
|
|
1250
1326
|
return nil # year first, only too far out for its unit
|
|
1251
1327
|
end
|
|
1252
1328
|
return nil if unreadable.empty?
|
|
1253
|
-
|
|
1329
|
+
CATime::Literal.infer_time_format(text, first)
|
|
1254
1330
|
end
|
|
1255
1331
|
|
|
1256
1332
|
# The column as an object array of its text. A CAString already holds one,
|
|
@@ -1294,10 +1370,10 @@ class CAFrame
|
|
|
1294
1370
|
private def parse_time_by_format(key, col, format, unit, on_error)
|
|
1295
1371
|
res = CATime::Resolution.parse(unit)
|
|
1296
1372
|
text = time_text_of(col)
|
|
1297
|
-
fields = format &&
|
|
1373
|
+
fields = format && CATime::Literal.tick_kind(res) &&
|
|
1298
1374
|
text.__strptime_fields__(format, false)
|
|
1299
1375
|
if fields
|
|
1300
|
-
parsed =
|
|
1376
|
+
parsed = CATime::Literal.ticks_from_fields(fields, text, res, format, :mask)
|
|
1301
1377
|
.time(unit: res)
|
|
1302
1378
|
return parsed if on_error == :mask
|
|
1303
1379
|
cells = text.flatten.to_a
|
|
@@ -1392,7 +1468,8 @@ class CAFrame
|
|
|
1392
1468
|
promoted = CArray.promote_list(cols)
|
|
1393
1469
|
rescue ArgumentError, TypeError, CArray::DataTypeError => e
|
|
1394
1470
|
raise ArgumentError,
|
|
1395
|
-
"#{e.message
|
|
1471
|
+
"promote: #{e.message.delete_prefix("promote_list: ")} -- " \
|
|
1472
|
+
"promote(:object) brings every column to its " \
|
|
1396
1473
|
"surface values, which any column set can share; a numeric target " \
|
|
1397
1474
|
"works too when every Face column declares #to_numeric"
|
|
1398
1475
|
end
|
|
@@ -1421,7 +1498,13 @@ class CAFrame
|
|
|
1421
1498
|
# to say about a surface it cannot read, so the widening check -- which
|
|
1422
1499
|
# is about primitive promotion -- applies to plain columns only.
|
|
1423
1500
|
@columns.each { |key, col| refuse_narrowing(key, col, type) unless col.face? }
|
|
1424
|
-
|
|
1501
|
+
# A column that has the type already stays as it is, as promote leaves it
|
|
1502
|
+
# with no target.
|
|
1503
|
+
name = CArray.data_type_name(type)
|
|
1504
|
+
@columns.each_key do |key|
|
|
1505
|
+
col = @columns[key]
|
|
1506
|
+
@columns[key] = col.to_type(type) unless !col.face? && col.data_type_name == name
|
|
1507
|
+
end
|
|
1425
1508
|
end
|
|
1426
1509
|
|
|
1427
1510
|
private def refuse_narrowing(key, col, type)
|