carray 3.0.7 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1940
- data/README.md +38 -32
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +6 -9
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +27 -0
- data/ext/ca_binop_dispatch.h +22 -47
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1506
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +90 -163
- data/ext/ca_obj_bincmp.c +47 -46
- data/ext/ca_obj_binop.c +38 -38
- data/ext/ca_obj_bitarray.c +5 -17
- data/ext/ca_obj_bitfield.c +15 -30
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +32 -30
- data/ext/ca_obj_const_string.c +57 -62
- data/ext/ca_obj_face.c +99 -91
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +15 -21
- data/ext/ca_obj_farray.c +7 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +8 -19
- data/ext/ca_obj_grid.c +137 -106
- data/ext/ca_obj_meld.c +23 -21
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +24 -35
- data/ext/ca_obj_object.c +51 -37
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +8 -10
- data/ext/ca_obj_refer.c +42 -29
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +18 -24
- data/ext/ca_obj_select.c +10 -22
- data/ext/ca_obj_select_axis.c +27 -36
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +28 -24
- data/ext/ca_obj_stride.c +53 -54
- data/ext/ca_obj_string.c +6 -18
- data/ext/ca_obj_tile.c +12 -12
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +35 -39
- data/ext/ca_obj_window.c +67 -86
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +34 -12
- data/ext/ca_op_powi.h +4 -0
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +20 -26
- data/ext/ca_sweep_engine.c +2 -3
- data/ext/ca_sweep_engine.h +17 -9
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +11 -11
- data/ext/carray.h +300 -1011
- data/ext/carray_access.c +234 -180
- data/ext/carray_address_basis.c +33 -38
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +30 -26
- data/ext/carray_call_cfunc.c +169 -277
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +912 -346
- data/ext/carray_cast_func.rb +31 -3
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +43 -38
- data/ext/carray_copy.c +28 -26
- data/ext/carray_core.c +217 -126
- data/ext/carray_count.c +27 -15
- data/ext/carray_element.c +40 -25
- data/ext/carray_factorize.c +53 -52
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +8 -10
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +123 -228
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1274 -160
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +21 -36
- data/ext/carray_loop.c +19 -22
- data/ext/carray_mask.c +123 -145
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +95 -94
- data/ext/carray_memory_view.c +42 -47
- data/ext/carray_operator.c +423 -122
- data/ext/carray_order.c +121 -85
- data/ext/carray_partition.c +13 -15
- data/ext/carray_random.c +47 -41
- data/ext/carray_round.c +9 -4
- data/ext/carray_scatter.c +54 -40
- data/ext/carray_segment.c +41 -45
- data/ext/carray_slab.c +64 -47
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +32 -29
- data/ext/carray_sort_kernel.c +12 -10
- data/ext/carray_struct.c +38 -43
- data/ext/carray_test.c +47 -43
- data/ext/carray_time_text.c +24 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +258 -94
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +19 -20
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +782 -461
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +264 -180
- data/lib/carray/basics.rb +272 -48
- data/lib/carray/bincount_nd.rb +33 -16
- data/lib/carray/block_iterator.rb +123 -102
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +63 -51
- data/lib/carray/categorical_iterator.rb +89 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +49 -22
- data/lib/carray/const_string.rb +54 -62
- data/lib/carray/core_extensions.rb +1 -1
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +25 -24
- data/lib/carray/frame/column_verbs.rb +20 -8
- data/lib/carray/frame/concat.rb +3 -3
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +7 -1
- data/lib/carray/frame/frame.rb +35 -25
- data/lib/carray/frame/group.rb +183 -150
- data/lib/carray/frame/io.rb +26 -7
- data/lib/carray/frame/join.rb +37 -12
- data/lib/carray/frame/records.rb +1 -1
- data/lib/carray/frame/reshape.rb +3 -3
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +148 -118
- data/lib/carray/frame/windows.rb +232 -21
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +24 -21
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +97 -24
- data/lib/carray/key_windows.rb +83 -57
- data/lib/carray/lazy.rb +196 -94
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +5 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +72 -71
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/gather_nd.rb +106 -106
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +165 -145
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +150 -138
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +12 -12
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +18 -17
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +190 -22
- data/lib/carray/segment_iterator.rb +251 -129
- data/lib/carray/serialize.rb +618 -571
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +11 -12
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +76 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +243 -72
- data/lib/carray/time.rb +496 -342
- data/lib/carray/window_iterator.rb +191 -150
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +4 -3
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +7 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +26 -15
- data/yard-stubs/carray_access.rb +51 -4
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +118 -165
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +5 -3
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +157 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +67 -34
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +10 -2
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +62 -24
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25337
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -262
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/frame/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)
|
|
@@ -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) }
|
|
@@ -230,6 +233,7 @@ class CAFrame
|
|
|
230
233
|
# names a different number of columns or one twice, or a new name is
|
|
231
234
|
# already a column; and as CACategorical#contrast raises.
|
|
232
235
|
def contrast_columns(name, scheme = :dummy, base: nil, full: false, into: nil)
|
|
236
|
+
full = CArray.send(:bool_arg, full, "full", "contrast_columns")
|
|
233
237
|
key = column_key(name, "contrast_columns")
|
|
234
238
|
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
235
239
|
unless col.ndim == 1
|
|
@@ -456,9 +460,9 @@ class CAFrame
|
|
|
456
460
|
# In-place on the column (write-through, §4.3): numeric/object columns
|
|
457
461
|
# mask in place; a categorical column's codes are read-only (§13.4) so this
|
|
458
462
|
# raises — recode by rebuilding and rebinding instead.
|
|
459
|
-
def
|
|
463
|
+
def mask(name, value)
|
|
460
464
|
refuse_if_frozen
|
|
461
|
-
self[column_key(name, "
|
|
465
|
+
self[column_key(name, "mask")][:eq, value] = UNDEF
|
|
462
466
|
self
|
|
463
467
|
end
|
|
464
468
|
|
|
@@ -468,7 +472,7 @@ class CAFrame
|
|
|
468
472
|
# value the type cannot hold, becomes UNDEF (parse-mask, §6-2). A text
|
|
469
473
|
# column cast to :time is parsed into a CATime column, in the finest unit
|
|
470
474
|
# its text shows (:D for dates alone, :s with a time of day, :ms / :us /
|
|
471
|
-
# :ns for fractions of a second);
|
|
475
|
+
# :ns for fractions of a second); parse_time takes a format and a
|
|
472
476
|
# unit. A text column cast to :timedelta reads the text to_csv writes for
|
|
473
477
|
# a duration ("194400s", "3M", "3 @ 10 m") into a CATimedelta column, in
|
|
474
478
|
# the finest unit its cells show. A column cast to :categorical becomes a CACategorical of its
|
|
@@ -647,7 +651,7 @@ class CAFrame
|
|
|
647
651
|
# Whether a cell is an integer a float64 would round. Fewer than 16 digits
|
|
648
652
|
# cannot be, which leaves a const string column's other cells unread.
|
|
649
653
|
private def integer_text_past_float?(col)
|
|
650
|
-
long = col.is_a?(CAConstString) ? col[col.
|
|
654
|
+
long = col.is_a?(CAConstString) ? col[col.bytesize.ge(16).strip_mask(false)].to_a : time_text_of(col).to_a
|
|
651
655
|
long.any? do |text|
|
|
652
656
|
next text.abs > 2**53 if text.is_a?(Integer) # an object cell read as its text
|
|
653
657
|
text.is_a?(String) && text.match?(/\A\s*[-+]?\d{16,}\s*\z/) && Integer(text.strip, 10).abs > 2**53
|
|
@@ -708,31 +712,31 @@ class CAFrame
|
|
|
708
712
|
# does. Make it the index with +set_index+ afterward.
|
|
709
713
|
# +cast(name => :time)+ is the call without a format.
|
|
710
714
|
#
|
|
711
|
-
# df.
|
|
712
|
-
# df.
|
|
713
|
-
# df.
|
|
714
|
-
def
|
|
715
|
-
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, ...)
|
|
716
720
|
end
|
|
717
721
|
|
|
718
|
-
private def
|
|
722
|
+
private def parse_time_in_place(name, format = nil, unit: nil, on_error: :mask)
|
|
719
723
|
refuse_if_frozen
|
|
720
724
|
unless CAST_ON_ERROR.include?(on_error)
|
|
721
725
|
raise ArgumentError,
|
|
722
726
|
"on_error: must be :mask, :warn or :raise (got #{on_error.inspect})"
|
|
723
727
|
end
|
|
724
|
-
key = column_key(name, "
|
|
728
|
+
key = column_key(name, "parse_time")
|
|
725
729
|
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
726
730
|
unless string_column?(col)
|
|
727
731
|
raise ArgumentError,
|
|
728
|
-
"
|
|
732
|
+
"parse_time needs a string column (object / CAString / " \
|
|
729
733
|
"CAConstString / CAFixlenString); #{key.inspect} is #{col.data_type}"
|
|
730
734
|
end
|
|
731
735
|
@columns[key] = col.__send__(:carry_attrs, parse_time_column(key, col, format, unit, on_error))
|
|
732
736
|
self
|
|
733
737
|
end
|
|
734
738
|
|
|
735
|
-
# The strptime format
|
|
739
|
+
# The strptime format parse_time(name, :infer) reads the column with,
|
|
736
740
|
# or nil when the text is written year first and needs none. The first
|
|
737
741
|
# present cell gives the candidates and each later cell drops those it
|
|
738
742
|
# does not fit, until one is left. CAFrame::UnreadableColumn when none fits
|
|
@@ -753,120 +757,130 @@ class CAFrame
|
|
|
753
757
|
|
|
754
758
|
# A new frame with an integer column read as time serial counts (memo
|
|
755
759
|
# §11.2); this frame keeps the integer column. This is the "serial -> time" mode: each value is a count of
|
|
756
|
-
# +unit+ resolution since +
|
|
757
|
-
# +
|
|
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
|
|
758
762
|
# measured from another origin — a netCDF "hours since 1990-01-01" axis, an
|
|
759
|
-
# Excel serial date (
|
|
763
|
+
# Excel serial date (origin "1899-12-30", unit :D) — converts directly.
|
|
760
764
|
#
|
|
761
765
|
# A float column is accepted only when every value is whole (no fractional
|
|
762
766
|
# part); a fractional serial has sub-unit precision that a finer +unit+
|
|
763
767
|
# should carry, so it raises CAFrame::UnreadableColumn rather than
|
|
764
768
|
# silently truncate. Make it the index with +set_index+ afterward.
|
|
765
769
|
#
|
|
766
|
-
# A +CATime::Grid+ carries the same (unit,
|
|
767
|
-
# netCDF +units+ attribute goes straight in
|
|
768
|
-
#
|
|
769
|
-
#
|
|
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
|
|
770
775
|
# time-of-day, while a grid resolves the finer storage that holds it.
|
|
771
776
|
#
|
|
772
|
-
# df.to_time("time", unit: :h,
|
|
773
|
-
# df.to_time("time", CATime::Grid.parse("hours since 1990-01-01"))
|
|
774
|
-
# 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"))
|
|
775
780
|
def to_time(...)
|
|
776
781
|
return dup.__send__(:to_time_in_place, ...)
|
|
777
782
|
end
|
|
778
783
|
|
|
779
|
-
private def to_time_in_place(name,
|
|
784
|
+
private def to_time_in_place(name, unit: :s, origin: nil)
|
|
780
785
|
refuse_if_frozen
|
|
781
786
|
key = column_key(name, "to_time")
|
|
782
787
|
col = @columns.fetch(key) { refuse_missing_column(key) }
|
|
783
788
|
raw = integer_serial_column(col, key)
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
@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))
|
|
787
792
|
return self
|
|
788
793
|
end
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
"the positional argument must be a CATime::Grid (got #{grid.class})"
|
|
792
|
-
end
|
|
793
|
-
if epoch
|
|
794
|
-
# An Integer epoch is an instant in Unix seconds, whatever the unit.
|
|
795
|
-
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]
|
|
796
796
|
end
|
|
797
797
|
@columns[key] = col.__send__(:carry_attrs, raw.time(unit: unit))
|
|
798
798
|
self
|
|
799
799
|
end
|
|
800
800
|
|
|
801
|
-
# Fill masked cells of a column
|
|
802
|
-
#
|
|
803
|
-
#
|
|
804
|
-
#
|
|
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.
|
|
805
808
|
#
|
|
806
|
-
#
|
|
807
|
-
#
|
|
808
|
-
#
|
|
809
|
-
#
|
|
810
|
-
#
|
|
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
|
|
811
814
|
#
|
|
812
|
-
# :
|
|
815
|
+
# :forward / :backward work for any writable column data_type (numeric, time,
|
|
813
816
|
# object, fixlen, …); :linear needs a numeric or time (CATime /
|
|
814
817
|
# CATimedelta) column. A categorical column's codes are read-only
|
|
815
818
|
# (memo §13.4), so any fill on it raises — rebind a filled copy instead:
|
|
816
819
|
# `df.append(name, df[name].strip_mask(method: :forward))`.
|
|
817
820
|
#
|
|
818
|
-
# Several columns, or every one, are filled the same way: the
|
|
819
|
-
#
|
|
820
|
-
#
|
|
821
|
-
# fills several columns, each its own
|
|
822
|
-
#
|
|
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:
|
|
823
827
|
#
|
|
824
|
-
#
|
|
825
|
-
#
|
|
826
|
-
#
|
|
827
|
-
#
|
|
828
|
-
|
|
829
|
-
|
|
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)
|
|
830
835
|
return self if plan.nil?
|
|
831
836
|
if plan.is_a?(Hash)
|
|
832
|
-
return fill_by_map(plan, "
|
|
837
|
+
return fill_by_map(plan, "unmask") { |frame, key, how| frame.__send__(:unmask_column, key, how) }
|
|
833
838
|
end
|
|
834
|
-
|
|
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)
|
|
835
848
|
refuse_if_frozen
|
|
836
|
-
key = column_key(name, "
|
|
849
|
+
key = column_key(name, "unmask")
|
|
837
850
|
refuse_missing_column(key) unless @columns.key?(key)
|
|
838
851
|
col = @columns[key]
|
|
839
852
|
# An N-D column is filled down the rows: each position on its trailing
|
|
840
853
|
# axes is a series of its own.
|
|
841
854
|
along = col.ndim > 1 ? { axis: 0 } : {}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
raise ArgumentError,
|
|
848
|
-
"fill: unknown method #{method_or_value.inspect} " \
|
|
849
|
-
"(: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
|
|
850
860
|
else
|
|
851
|
-
fill_constant(key, col,
|
|
861
|
+
fill_constant(key, col, how)
|
|
852
862
|
end
|
|
853
863
|
self
|
|
854
864
|
end
|
|
855
865
|
|
|
856
866
|
# A constant fill, in place, naming the column when the value does not go
|
|
857
867
|
# into it, as strip_mask names it.
|
|
858
|
-
private def fill_constant
|
|
868
|
+
private def fill_constant(key, col, value)
|
|
859
869
|
if col.read_only?
|
|
860
|
-
raise "
|
|
870
|
+
raise "unmask: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) can not modify read-only array"
|
|
861
871
|
end
|
|
862
872
|
col.unmask(value)
|
|
863
873
|
rescue ArgumentError, TypeError, RangeError, FloatDomainError => e
|
|
864
|
-
|
|
865
|
-
|
|
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}")
|
|
866
877
|
end
|
|
867
878
|
|
|
879
|
+
FILL_METHOD_NAMES = %i[forward backward linear].freeze
|
|
880
|
+
private_constant :FILL_METHOD_NAMES
|
|
881
|
+
|
|
868
882
|
# A frame with the masked cells filled, this frame left as it is: the
|
|
869
|
-
# sibling of +
|
|
883
|
+
# sibling of +unmask+ that returns a new frame, as CArray#strip_mask is of
|
|
870
884
|
# CArray#unmask. The value or the method is given as CArray#strip_mask
|
|
871
885
|
# takes it, for every column or, with a Hash, column by column:
|
|
872
886
|
#
|
|
@@ -877,8 +891,8 @@ class CAFrame
|
|
|
877
891
|
#
|
|
878
892
|
# A positional Symbol is a value, as in CArray#strip_mask. The methods are
|
|
879
893
|
# :forward and :backward (carry the last or the next present value down the
|
|
880
|
-
# rows; :
|
|
881
|
-
# 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
|
|
882
896
|
# the row position when it has none. An N-D column is filled down the rows.
|
|
883
897
|
# A cell a method cannot reach (before the first present value, say) stays
|
|
884
898
|
# masked.
|
|
@@ -896,7 +910,7 @@ class CAFrame
|
|
|
896
910
|
# @raise [KeyError] for a name that is no column.
|
|
897
911
|
# @raise [ArgumentError] for neither or both of a value and method:, or an
|
|
898
912
|
# unknown method.
|
|
899
|
-
def strip_mask
|
|
913
|
+
def strip_mask(fill = STRIP_MASK_UNSET, method: nil, **named)
|
|
900
914
|
# df.strip_mask("x" => 0.0) arrives as keywords: the String keys are
|
|
901
915
|
# column names, never an option.
|
|
902
916
|
unless named.empty?
|
|
@@ -940,17 +954,17 @@ class CAFrame
|
|
|
940
954
|
Ractor.make_shareable(STRIP_MASK_UNSET)
|
|
941
955
|
private_constant :STRIP_MASK_UNSET
|
|
942
956
|
|
|
943
|
-
STRIP_MASK_METHODS = %i[forward backward
|
|
957
|
+
STRIP_MASK_METHODS = %i[forward backward linear].freeze
|
|
944
958
|
private_constant :STRIP_MASK_METHODS
|
|
945
959
|
|
|
946
|
-
private def strip_mask_by_value
|
|
960
|
+
private def strip_mask_by_value(key, col, value)
|
|
947
961
|
return col.strip_mask(value)
|
|
948
962
|
rescue ArgumentError, TypeError, RangeError, FloatDomainError => e
|
|
949
963
|
raise e.exception("strip_mask: column #{key.inspect} (#{CArray::Inspector.type_label(col)}) " \
|
|
950
964
|
"cannot take #{value.inspect}: #{e.message}")
|
|
951
965
|
end
|
|
952
966
|
|
|
953
|
-
private def strip_mask_by_method
|
|
967
|
+
private def strip_mask_by_method(key, col, method)
|
|
954
968
|
if method == :linear && @index
|
|
955
969
|
out = col.copy
|
|
956
970
|
fill_linear_column(key, out, "strip_mask")
|
|
@@ -963,8 +977,8 @@ class CAFrame
|
|
|
963
977
|
end
|
|
964
978
|
end
|
|
965
979
|
|
|
966
|
-
private def linear_call
|
|
967
|
-
return "
|
|
980
|
+
private def linear_call(verb, key)
|
|
981
|
+
return "unmask(#{key.inspect}, method: :linear)" if verb == "unmask"
|
|
968
982
|
return "strip_mask(method: :linear) of column #{key.inspect}"
|
|
969
983
|
end
|
|
970
984
|
|
|
@@ -974,45 +988,60 @@ class CAFrame
|
|
|
974
988
|
# written only when every one has passed: a refusal leaves them all as
|
|
975
989
|
# they were. A read-only column is given to the block as it is, to refuse
|
|
976
990
|
# as a single fill does before anything is written.
|
|
977
|
-
# What
|
|
978
|
-
# name => how for several (the columns named before the
|
|
979
|
-
# or, with none named, every column with a masked cell,
|
|
980
|
-
# out; nil when there is none).
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
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
|
|
987
1003
|
end
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
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)
|
|
991
1016
|
raise ArgumentError,
|
|
992
|
-
"
|
|
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}; " \
|
|
993
1023
|
"a String alone is a column name"
|
|
994
1024
|
end
|
|
995
|
-
|
|
1025
|
+
end
|
|
1026
|
+
if names.empty?
|
|
996
1027
|
map = @columns.select { |key, col| !key_names.include?(key) && col.any_masked? }
|
|
997
1028
|
.keys.to_h { |key| [key, how] }
|
|
998
1029
|
return map.empty? ? nil : map
|
|
999
1030
|
end
|
|
1000
1031
|
return [names.first, how] if names.size == 1 && names.first.is_a?(String)
|
|
1001
|
-
return names_selected(names, "
|
|
1032
|
+
return names_selected(names, "unmask").to_h { |key| [key, how] }
|
|
1002
1033
|
end
|
|
1003
1034
|
|
|
1004
|
-
private def refuse_unknown_fill_method
|
|
1005
|
-
return
|
|
1006
|
-
raise ArgumentError,
|
|
1007
|
-
"#{verb}: unknown method #{how.inspect} (:ffill | :bfill | :linear, or a constant fill value)"
|
|
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)"
|
|
1008
1038
|
end
|
|
1009
1039
|
|
|
1010
|
-
private def fill_by_map
|
|
1040
|
+
private def fill_by_map(map, verb)
|
|
1011
1041
|
raise ArgumentError, "#{verb}: the Hash names no column" if map.empty?
|
|
1012
1042
|
refuse_if_frozen
|
|
1013
1043
|
keys = column_keys(map.keys, verb)
|
|
1014
1044
|
keys.each { |key| refuse_missing_column(key) unless @columns.key?(key) }
|
|
1015
|
-
map.each_value { |how| refuse_unknown_fill_method(how, verb) }
|
|
1016
1045
|
pairs = keys.zip(map.values)
|
|
1017
1046
|
pairs.each { |key, how| yield self, key, how if @columns[key].read_only? }
|
|
1018
1047
|
filled = pairs.map do |key, how|
|
|
@@ -1033,7 +1062,7 @@ class CAFrame
|
|
|
1033
1062
|
col[target] = filled[target] if target.any
|
|
1034
1063
|
end
|
|
1035
1064
|
|
|
1036
|
-
private def fill_linear_column(key, col, verb = "
|
|
1065
|
+
private def fill_linear_column(key, col, verb = "unmask")
|
|
1037
1066
|
# A Face column interpolates through its own linear_fetch (a time
|
|
1038
1067
|
# column's keeps the Face and its unit), as the core gap fill does.
|
|
1039
1068
|
unless col.__send__(:interpolable?)
|
|
@@ -1052,7 +1081,7 @@ class CAFrame
|
|
|
1052
1081
|
"#{linear_call(verb, key)}: the index #{@axis_name.inspect} is " \
|
|
1053
1082
|
"#{@index.data_type_name}, not numeric or time, so it cannot be the x of " \
|
|
1054
1083
|
"the interpolation; to interpolate by row position, use " \
|
|
1055
|
-
"df[#{key.inspect}].#{verb
|
|
1084
|
+
"df[#{key.inspect}].#{verb}(method: :linear#{along})"
|
|
1056
1085
|
end
|
|
1057
1086
|
# With the index as x, an N-D column is filled one trailing position at a
|
|
1058
1087
|
# time; each position is a 1-D view, so the fill writes through to it.
|
|
@@ -1213,13 +1242,13 @@ class CAFrame
|
|
|
1213
1242
|
"fixed-length ones (#{distinct.map(&:to_s).join(', ')}); " \
|
|
1214
1243
|
"they have no common unit"
|
|
1215
1244
|
end
|
|
1216
|
-
unit = distinct.reduce { |a, b|
|
|
1245
|
+
unit = distinct.reduce { |a, b| CATime::UnitAlgebra.finer(a, b) } || CATime::Resolution.new(1, :s)
|
|
1217
1246
|
ticks = CArray.int64(cells.elements)
|
|
1218
1247
|
ticks[nil] = UNDEF
|
|
1219
1248
|
distinct.each do |u|
|
|
1220
1249
|
addrs = units.each_index.select { |i| units[i] == u }
|
|
1221
1250
|
begin
|
|
1222
|
-
scaled =
|
|
1251
|
+
scaled = CATime::UnitAlgebra.convert_scale!(CA_INT64(addrs.map { |i| values[i] }), u, unit)
|
|
1223
1252
|
rescue RangeError
|
|
1224
1253
|
raise RangeError, "cast: column #{key.inspect} holds durations too long for their finest unit, #{unit}"
|
|
1225
1254
|
end
|
|
@@ -1239,10 +1268,10 @@ class CAFrame
|
|
|
1239
1268
|
case format
|
|
1240
1269
|
when nil then read_time_text(key, col, unit, on_error)
|
|
1241
1270
|
when String
|
|
1242
|
-
parse_time_by_format(key, col, format, unit ||
|
|
1271
|
+
parse_time_by_format(key, col, format, unit || CATime::Literal.format_unit(format), on_error)
|
|
1243
1272
|
when :infer then parse_time_inferred(key, col, unit)
|
|
1244
1273
|
when :mixed
|
|
1245
|
-
unit ||=
|
|
1274
|
+
unit ||= CATime::Literal.unit_shown(time_text_of(col), nil)
|
|
1246
1275
|
parse_time_by_format(key, col, nil, unit, on_error)
|
|
1247
1276
|
else
|
|
1248
1277
|
raise ArgumentError,
|
|
@@ -1251,7 +1280,7 @@ class CAFrame
|
|
|
1251
1280
|
end
|
|
1252
1281
|
rescue RangeError => e
|
|
1253
1282
|
raise unless e.message.include?("does not fit int64 ticks")
|
|
1254
|
-
raise RangeError, "column #{key.inspect}: #{e.message};
|
|
1283
|
+
raise RangeError, "column #{key.inspect}: #{e.message}; parse_time takes " \
|
|
1255
1284
|
"unit: to read it in a coarser unit (:us spans 292,000 years)"
|
|
1256
1285
|
end
|
|
1257
1286
|
|
|
@@ -1273,10 +1302,10 @@ class CAFrame
|
|
|
1273
1302
|
digits > 6 ? :ns : digits > 3 ? :us : :ms
|
|
1274
1303
|
end
|
|
1275
1304
|
res = CATime::Resolution.parse(unit)
|
|
1276
|
-
unless
|
|
1305
|
+
unless CATime::Literal.tick_kind(res)
|
|
1277
1306
|
return parse_time_by_format(key, col, f.format, unit, :raise)
|
|
1278
1307
|
end
|
|
1279
|
-
|
|
1308
|
+
CATime::Literal.ticks_from_fields(fields, col, res, f.format, :raise).time(unit: res)
|
|
1280
1309
|
end
|
|
1281
1310
|
|
|
1282
1311
|
# [format, fields, address of the first cell not in it] for :infer, or nil
|
|
@@ -1297,7 +1326,7 @@ class CAFrame
|
|
|
1297
1326
|
return nil # year first, only too far out for its unit
|
|
1298
1327
|
end
|
|
1299
1328
|
return nil if unreadable.empty?
|
|
1300
|
-
|
|
1329
|
+
CATime::Literal.infer_time_format(text, first)
|
|
1301
1330
|
end
|
|
1302
1331
|
|
|
1303
1332
|
# The column as an object array of its text. A CAString already holds one,
|
|
@@ -1341,10 +1370,10 @@ class CAFrame
|
|
|
1341
1370
|
private def parse_time_by_format(key, col, format, unit, on_error)
|
|
1342
1371
|
res = CATime::Resolution.parse(unit)
|
|
1343
1372
|
text = time_text_of(col)
|
|
1344
|
-
fields = format &&
|
|
1373
|
+
fields = format && CATime::Literal.tick_kind(res) &&
|
|
1345
1374
|
text.__strptime_fields__(format, false)
|
|
1346
1375
|
if fields
|
|
1347
|
-
parsed =
|
|
1376
|
+
parsed = CATime::Literal.ticks_from_fields(fields, text, res, format, :mask)
|
|
1348
1377
|
.time(unit: res)
|
|
1349
1378
|
return parsed if on_error == :mask
|
|
1350
1379
|
cells = text.flatten.to_a
|
|
@@ -1439,7 +1468,8 @@ class CAFrame
|
|
|
1439
1468
|
promoted = CArray.promote_list(cols)
|
|
1440
1469
|
rescue ArgumentError, TypeError, CArray::DataTypeError => e
|
|
1441
1470
|
raise ArgumentError,
|
|
1442
|
-
"#{e.message
|
|
1471
|
+
"promote: #{e.message.delete_prefix("promote_list: ")} -- " \
|
|
1472
|
+
"promote(:object) brings every column to its " \
|
|
1443
1473
|
"surface values, which any column set can share; a numeric target " \
|
|
1444
1474
|
"works too when every Face column declares #to_numeric"
|
|
1445
1475
|
end
|