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.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
@@ -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 (fill, mask_eq) edits them in place and returns self. The new frame
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
- def drop(*names)
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 (*names)
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
- col = CArray.stack(parts, axis: 1)
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 mask_eq(name, value)
463
+ def mask(name, value)
449
464
  refuse_if_frozen
450
- self[column_key(name, "mask_eq")][:eq, value] = UNDEF
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); parse_to_time takes a format and a
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
- rest = @columns.keys - named.keys
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), which a
614
- # float would round. A day-first or month-first date ("01/02/2024") is not
615
- # read as time, since which one it is cannot be told. Only text columns
616
- # are looked at -- an object column or a string Face (CAString,
617
- # CAConstString); columns that already have a type are not listed.
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; like +cast+ it allocates fresh columns,
635
- # so this frame keeps its own. The index is not a column and is left
636
- # alone.
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.parse_to_time("time").set_index("time")
685
- # df.parse_to_time("date", "%d/%m/%Y")
686
- # df.parse_to_time("date", :infer)
687
- def parse_to_time(...)
688
- return dup.__send__(:parse_to_time_in_place, ...)
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 parse_to_time_in_place(name, format = nil, unit: nil, on_error: :mask)
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, "parse_to_time")
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
- "parse_to_time needs a string column (object / CAString / " \
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 parse_to_time(name, :infer) reads the column with,
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 +epoch+ (default the Unix epoch, 1970-01-01 UTC).
730
- # +epoch+ takes any time literal (String / Time / Integer), so a column
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 (epoch "1899-12-30", unit :D) — converts directly.
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, epoch) pair as one value, so a
740
- # netCDF +units+ attribute goes straight in. It also carries a phase the
741
- # keyword form cannot: the keyword +epoch+ is read on the +unit+ grid, so
742
- # an epoch off that grid ("days since 1980-01-01 12:00") loses its
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, epoch: "1990-01-01").set_index("time")
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, grid = nil, unit: :s, epoch: nil)
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
- grid = unit if unit.is_a?(CATime::Grid)
758
- if grid.is_a?(CATime::Grid)
759
- @columns[key] = col.__send__(:carry_attrs, grid.at(raw))
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
- unless grid.nil?
763
- raise ArgumentError,
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 (memo §6, §11.8). A Symbol selects a
775
- # scan method; anything else is a constant fill value. The fill is
776
- # write-through: it edits the live shared column rather than rebinding a
777
- # filled copy (memo §3.8). Returns self so calls chain.
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
- # fill("temp", :ffill) # forward hold (carry last valid value)
780
- # fill("temp", :bfill) # backward hold (carry next valid value)
781
- # fill("temp", :linear) # linear interpolation; x = this frame's index
782
- # # coordinate when present, else the cell position
783
- # fill("temp", 0.0) # constant fill
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
- # :ffill / :bfill work for any writable column data_type (numeric, time,
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 method or
792
- # value comes last, after the columns as +select+ takes them; alone, it
793
- # fills every column that has a masked cell, as +strip_mask+ does. A Hash
794
- # fills several columns, each its own way. Every column is checked before
795
- # any is filled:
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
- # fill("temp", "wind", :ffill)
798
- # fill(:numeric, 0)
799
- # fill(:ffill) # every column with a masked cell
800
- # fill("temp" => :linear, "wind" => 0.0)
801
- def fill(*args)
802
- plan = fill_arguments(args)
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, "fill") { |frame, key, how| frame.fill(key, how) }
837
+ return fill_by_map(plan, "unmask") { |frame, key, how| frame.__send__(:unmask_column, key, how) }
806
838
  end
807
- name, method_or_value = plan
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, "fill")
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
- case method_or_value
816
- when :ffill then col.unmask(method: :forward, **along)
817
- when :bfill then col.unmask(method: :backward, **along)
818
- when :linear then fill_linear_column(key, col)
819
- when Symbol
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, method_or_value)
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 (key, col, value)
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
- raise e.exception("fill: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) " \
835
- "cannot take #{value.inspect}: #{e.message}")
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 +fill+ that returns a new frame, as CArray#strip_mask is 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; :ffill and :bfill are the same) and :linear, which interpolates
851
- # against this frame's index when it has one, as +fill+ does, and against
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 (fill = STRIP_MASK_UNSET, method: nil, **named)
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 ffill bfill linear].freeze
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 (key, col, 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 (key, col, 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 (verb, key)
937
- return "fill(#{key.inspect}, :linear)" if verb == "fill"
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 fill's arguments ask for: [name, how] for one column, or a Hash of
948
- # name => how for several (the columns named before the method or value,
949
- # or, with none named, every column with a masked cell, +key_names+ left
950
- # out; nil when there is none).
951
- private def fill_arguments (args, key_names = [])
952
- return args.first if args.size == 1 && args.first.is_a?(Hash)
953
- if args.empty? || args.first.is_a?(Hash)
954
- raise ArgumentError,
955
- "fill: give the columns and a method or value, a method or value alone, " \
956
- "or one Hash of them"
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
- *names, how = args
959
- if names.empty?
960
- if how.is_a?(String)
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
- "fill: give a method or value after the column #{how.inspect}; " \
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, "fill").to_h { |key| [key, how] }
1032
+ return names_selected(names, "unmask").to_h { |key| [key, how] }
971
1033
  end
972
1034
 
973
- private def fill_by_map (map, verb)
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][] = col }
1053
+ filled.compact.each { |key, col| write_filled_cells(@columns[key], col) }
992
1054
  return self
993
1055
  end
994
1056
 
995
- private def fill_linear_column(key, col, verb = "fill")
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 == "fill" ? "unmask" : "strip_mask"}(method: :linear#{along})"
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[] = col.send(:linear_fill_along, @index) # write-through
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
- if m
1158
- values[addr] = Integer(m[1], 10)
1159
- units[addr] = CATime::Resolution.new(m[2] ? Integer(m[2], 10) : 1, m[3].to_sym)
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| CATimeUnitAlgebra.finer(a, b) } || CATime::Resolution.new(1, :s)
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
- scaled = CATimeUnitAlgebra.convert_scale!(CA_INT64(addrs.map { |i| values[i] }), u, unit)
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 || CATimeLiteral.format_unit(format), on_error)
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 ||= CATimeLiteral.unit_shown(time_text_of(col), nil)
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}; parse_to_time takes " \
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 CATimeLiteral.tick_kind(res)
1305
+ unless CATime::Literal.tick_kind(res)
1230
1306
  return parse_time_by_format(key, col, f.format, unit, :raise)
1231
1307
  end
1232
- CATimeLiteral.ticks_from_fields(fields, col, res, f.format, :raise).time(unit: res)
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
- CATimeLiteral.infer_time_format(text, first)
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 && CATimeLiteral.tick_kind(res) &&
1373
+ fields = format && CATime::Literal.tick_kind(res) &&
1298
1374
  text.__strptime_fields__(format, false)
1299
1375
  if fields
1300
- parsed = CATimeLiteral.ticks_from_fields(fields, text, res, format, :mask)
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} -- promote(:object) brings every column to its " \
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
- @columns.each_key { |key| @columns[key] = @columns[key].to_type(type) }
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)