daru_lite 0.3.3 → 0.3.5

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 (150) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +60 -0
  3. data/README.md +1 -1
  4. data/lib/daru_lite/category.rb +1 -1
  5. data/lib/daru_lite/data_frame/duplicatable.rb +7 -1
  6. data/lib/daru_lite/data_frame/fetchable.rb +1 -1
  7. data/lib/daru_lite/data_frame/iterable.rb +5 -2
  8. data/lib/daru_lite/data_frame/sortable.rb +13 -5
  9. data/lib/daru_lite/dataframe.rb +52 -11
  10. data/lib/daru_lite/date_time/index.rb +2 -2
  11. data/lib/daru_lite/index/categorical_index.rb +62 -0
  12. data/lib/daru_lite/index/multi_index.rb +24 -6
  13. data/lib/daru_lite/maths/statistics/dataframe.rb +9 -5
  14. data/lib/daru_lite/maths/statistics/vector.rb +1 -1
  15. data/lib/daru_lite/version.rb +1 -1
  16. data/lib/daru_lite.rb +1 -0
  17. metadata +54 -284
  18. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  19. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  20. data/.github/dependabot.yml +0 -11
  21. data/.github/workflows/ci.yml +0 -33
  22. data/.gitignore +0 -10
  23. data/.rspec +0 -2
  24. data/.rubocop.yml +0 -27
  25. data/.rubocop_todo.yml +0 -139
  26. data/CONTRIBUTING.md +0 -47
  27. data/Gemfile +0 -2
  28. data/Rakefile +0 -69
  29. data/ReleasePolicy.md +0 -20
  30. data/benchmarks/TradeoffData.csv +0 -65
  31. data/benchmarks/csv_reading.rb +0 -22
  32. data/benchmarks/dataframe_creation.rb +0 -39
  33. data/benchmarks/db_loading.rb +0 -33
  34. data/benchmarks/duplicating.rb +0 -45
  35. data/benchmarks/group_by.rb +0 -32
  36. data/benchmarks/joining.rb +0 -52
  37. data/benchmarks/row_access.rb +0 -41
  38. data/benchmarks/row_assign.rb +0 -36
  39. data/benchmarks/sorting.rb +0 -51
  40. data/benchmarks/statistics.rb +0 -28
  41. data/benchmarks/vector_access.rb +0 -31
  42. data/benchmarks/vector_assign.rb +0 -42
  43. data/benchmarks/where_clause.rb +0 -48
  44. data/benchmarks/where_vs_filter.rb +0 -28
  45. data/daru_lite.gemspec +0 -54
  46. data/daru_lite_test-shm +0 -0
  47. data/daru_lite_test-wal +0 -0
  48. data/images/README.md +0 -5
  49. data/images/con0.png +0 -0
  50. data/images/con1.png +0 -0
  51. data/images/init0.png +0 -0
  52. data/images/init1.png +0 -0
  53. data/images/man0.png +0 -0
  54. data/images/man1.png +0 -0
  55. data/images/man2.png +0 -0
  56. data/images/man3.png +0 -0
  57. data/images/man4.png +0 -0
  58. data/images/man5.png +0 -0
  59. data/images/man6.png +0 -0
  60. data/profile/_base.rb +0 -23
  61. data/profile/df_to_a.rb +0 -10
  62. data/profile/filter.rb +0 -13
  63. data/profile/joining.rb +0 -13
  64. data/profile/sorting.rb +0 -12
  65. data/profile/vector_each_with_index.rb +0 -9
  66. data/profile/vector_new.rb +0 -9
  67. data/spec/accessors/array_wrapper_spec.rb +0 -3
  68. data/spec/category_spec.rb +0 -1741
  69. data/spec/core/group_by_spec.rb +0 -655
  70. data/spec/core/merge_spec.rb +0 -179
  71. data/spec/core/query_spec.rb +0 -347
  72. data/spec/daru_lite_spec.rb +0 -22
  73. data/spec/data_frame/aggregatable_example.rb +0 -65
  74. data/spec/data_frame/buildable_example.rb +0 -109
  75. data/spec/data_frame/calculatable_example.rb +0 -135
  76. data/spec/data_frame/convertible_example.rb +0 -180
  77. data/spec/data_frame/duplicatable_example.rb +0 -111
  78. data/spec/data_frame/fetchable_example.rb +0 -481
  79. data/spec/data_frame/filterable_example.rb +0 -456
  80. data/spec/data_frame/indexable_example.rb +0 -221
  81. data/spec/data_frame/iterable_example.rb +0 -465
  82. data/spec/data_frame/joinable_example.rb +0 -131
  83. data/spec/data_frame/missable_example.rb +0 -47
  84. data/spec/data_frame/pivotable_example.rb +0 -297
  85. data/spec/data_frame/queryable_example.rb +0 -92
  86. data/spec/data_frame/setable_example.rb +0 -482
  87. data/spec/data_frame/sortable_example.rb +0 -350
  88. data/spec/dataframe_spec.rb +0 -1306
  89. data/spec/date_time/data_spec.rb +0 -197
  90. data/spec/date_time/date_time_index_helper_spec.rb +0 -72
  91. data/spec/date_time/index_spec.rb +0 -588
  92. data/spec/date_time/offsets_spec.rb +0 -465
  93. data/spec/extensions/which_dsl_spec.rb +0 -38
  94. data/spec/fixtures/bank2.dat +0 -200
  95. data/spec/fixtures/boolean_converter_test.csv +0 -5
  96. data/spec/fixtures/countries.json +0 -7794
  97. data/spec/fixtures/duplicates.csv +0 -32
  98. data/spec/fixtures/eciresults.html +0 -394
  99. data/spec/fixtures/empties.dat +0 -2
  100. data/spec/fixtures/empty_rows_test.csv +0 -17
  101. data/spec/fixtures/macau.html +0 -3691
  102. data/spec/fixtures/macd_data.csv +0 -150
  103. data/spec/fixtures/matrix_test.csv +0 -100
  104. data/spec/fixtures/moneycontrol.html +0 -6812
  105. data/spec/fixtures/music_data.tsv +0 -2501
  106. data/spec/fixtures/repeated_fields.csv +0 -7
  107. data/spec/fixtures/sales-funnel.csv +0 -18
  108. data/spec/fixtures/scientific_notation.csv +0 -4
  109. data/spec/fixtures/string_converter_test.csv +0 -5
  110. data/spec/fixtures/strings.dat +0 -2
  111. data/spec/fixtures/test_xls.xls +0 -0
  112. data/spec/fixtures/test_xls_2.xls +0 -0
  113. data/spec/fixtures/url_test.txt~ +0 -0
  114. data/spec/fixtures/valid_markup.html +0 -62
  115. data/spec/fixtures/wiki_climate.html +0 -1243
  116. data/spec/fixtures/wiki_table_info.html +0 -631
  117. data/spec/formatters/table_formatter_spec.rb +0 -137
  118. data/spec/helpers_spec.rb +0 -8
  119. data/spec/index/categorical_index_spec.rb +0 -221
  120. data/spec/index/index_spec.rb +0 -438
  121. data/spec/index/multi_index_spec.rb +0 -713
  122. data/spec/io/io_spec.rb +0 -357
  123. data/spec/io/sql_data_source_spec.rb +0 -48
  124. data/spec/iruby/dataframe_spec.rb +0 -170
  125. data/spec/iruby/helpers_spec.rb +0 -49
  126. data/spec/iruby/multi_index_spec.rb +0 -37
  127. data/spec/iruby/vector_spec.rb +0 -105
  128. data/spec/maths/arithmetic/dataframe_spec.rb +0 -148
  129. data/spec/maths/arithmetic/vector_spec.rb +0 -165
  130. data/spec/maths/statistics/dataframe_spec.rb +0 -178
  131. data/spec/maths/statistics/vector_spec.rb +0 -756
  132. data/spec/monkeys_spec.rb +0 -42
  133. data/spec/shared/vector_display_spec.rb +0 -213
  134. data/spec/spec_helper.rb +0 -87
  135. data/spec/support/database_helper.rb +0 -29
  136. data/spec/support/matchers.rb +0 -5
  137. data/spec/vector/aggregatable_example.rb +0 -27
  138. data/spec/vector/calculatable_example.rb +0 -82
  139. data/spec/vector/convertible_example.rb +0 -126
  140. data/spec/vector/duplicatable_example.rb +0 -48
  141. data/spec/vector/fetchable_example.rb +0 -463
  142. data/spec/vector/filterable_example.rb +0 -165
  143. data/spec/vector/indexable_example.rb +0 -201
  144. data/spec/vector/iterable_example.rb +0 -111
  145. data/spec/vector/joinable_example.rb +0 -25
  146. data/spec/vector/missable_example.rb +0 -88
  147. data/spec/vector/queryable_example.rb +0 -91
  148. data/spec/vector/setable_example.rb +0 -300
  149. data/spec/vector/sortable_example.rb +0 -242
  150. data/spec/vector_spec.rb +0 -599
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c158868ddcdfad0c071d5ad8b583605e86cdd3a005714df2071d0fc122e88278
4
- data.tar.gz: 56d1feb6d2b95f6a4c897589c56c32e8890fe72c9eb2e72d6b71a91ffc9e95ae
3
+ metadata.gz: de64da6dbfe4a00b302051ee625c0d093a622e96df55b4199481d19c75c2f552
4
+ data.tar.gz: 908e11700ff1e8c2df635ea99747edbd2f8550d82962e6bc9fa79223d21e6462
5
5
  SHA512:
6
- metadata.gz: 5ac43007818f83995648bdbc65c5debc0d8999c9623271f0d6ca7c70bccc99904b74ec48fd99860a38ad6d02f99493fecdffb6560cbf92fd6a6682eb1190c0a5
7
- data.tar.gz: 3f980b1dd60846f9aba374663e34fbd0118396079e1366f7e0046c288087adbf0f7f606853dec915dd9b42109f30e2f25033ea277533a711f5be8ab2948e58f3
6
+ metadata.gz: eff5b0aac5a75ae43482fc40c046f43e95fb58704dad16795dd878e29b0667d4f2918db835c7934199c89460d8196e12ad781aa679d0d74e61558c24a2fa23fc
7
+ data.tar.gz: 860fcdb0a648c535b21a963d06453bc4ce761886ad5fa505587f3e17eae9639dfcc2043be1f3231708daeb86b5b256443541c904225c3d119c1340dac5774334
data/History.md CHANGED
@@ -1,3 +1,63 @@
1
+ # 0.3.5
2
+ * Declared `csv` and `matrix` as runtime dependencies
3
+ * Fixed `Vector#standardize`, `Vector#to_json` and `DataFrame#to_json` raising `NoMethodError` when ActiveSupport is not loaded
4
+ * Required Ruby 3.4 or later
5
+ * Stopped packaging specs, fixtures, benchmarks and images in the gem
6
+ * CI now runs on Ruby 3.4 and 4.0
7
+
8
+ # 0.3.4 (2026-07-20)
9
+ * Fixed latently-broken `CategoricalIndex` methods relying on `@relation_hash`
10
+ * Fixed DataFrame operations on a `MultiIndex` with duplicate tuples
11
+
12
+ # 0.3.3 (2026-07-01)
13
+ * Fixed `keep_vector_if` `TypeError` on a `MultiIndex` with duplicate tuples, preserving the vectors' index type
14
+
15
+ # 0.3.2 (2026-06-12)
16
+ * Fixed `NoMethodError` on single-key lookup with a `CategoricalIndex`
17
+
18
+ # 0.3.1 (2026-04-17)
19
+ * Raise `IndexError` on duplicate (row, column) pairs when using `crosstab_by_assignation`
20
+
21
+ # 0.3.0 (2026-04-14)
22
+ * Preserve `nil` rows when using `crosstab_by_assignation`
23
+
24
+ # 0.2.4 (2026-04-10)
25
+ * Updated rubocop and fixed offenses
26
+
27
+ # 0.2.3 (2026-03-27)
28
+ * Fixed regression when accessing a `MultiIndex` vector with an array among the arguments
29
+
30
+ # 0.2.2 (2026-03-27)
31
+ * Fixed regression when accessing a `MultiIndex` with an array among the arguments
32
+
33
+ # 0.2.1 (2026-03-26)
34
+ * Fixed `DataFrame#union` and row subsets via `#[]` when using a `MultiIndex`
35
+ * Dropped Code Climate
36
+
37
+ # 0.2.0 (2025-04-21)
38
+ * Updated dependencies
39
+ * [Breaking change] Dropped DBI
40
+ * [Security] Fixed polynomial ReDoS
41
+
42
+ # 0.1.3 (2024-09-09)
43
+ * Fixed `DataFrame#keep_row_if`, `Index#to_a` and `MultiIndex#delete_at`
44
+
45
+ # 0.1.2 (2024-09-05)
46
+ * Split the `DataFrame` and `Vector` classes into modules
47
+ * Fixed `rotate_vectors` with vectors of mixed classes
48
+ * Fixed `keep_row_if` for categorical indexes
49
+
50
+ # 0.1.1 (2024-02-26)
51
+ * First version of the daru Lite readme
52
+ * Fixed `inspect` when vector names are integers
53
+ * Fixed `order=` when vectors are of different classes
54
+
55
+ # 0.1.0 (2024-02-16)
56
+ * Renamed the gem to daru_lite
57
+ * Dropped gruff
58
+ * Added `DataFrame#rotate_vectors!` and `DataFrame#add_level_to_vectors`
59
+ * Added column to categorical index conversion
60
+
1
61
  # 0.0.1
2
62
  * Forked from Daru
3
63
  * Fixed incompatibilities with Ruby 3.1
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Simple, straightforward DataFrames for Ruby
4
4
 
5
- [![Build Status](https://github.com/pollandroll/daru_lite/actions/workflows/build.yml/badge.svg)](https://github.com/pollandroll/daru_lite/actions)
5
+ [![Build Status](https://github.com/pollandroll/daru_lite/actions/workflows/ci.yml/badge.svg)](https://github.com/pollandroll/daru_lite/actions)
6
6
  [![Gem Version](https://img.shields.io/gem/v/daru_lite.svg)](https://rubygems.org/gems/daru_lite)
7
7
 
8
8
  ## Introduction
@@ -766,7 +766,7 @@ module DaruLite
766
766
  return if ordered?
767
767
 
768
768
  raise ArgumentError, "Can not apply #{operation} when vector is unordered. " \
769
- 'To make the categorical data ordered, use #ordered = true' \
769
+ 'To make the categorical data ordered, use #ordered = true'
770
770
  end
771
771
 
772
772
  def dummy_coding(full)
@@ -10,7 +10,13 @@ module DaruLite
10
10
  # * +vectors_to_dup+ - An Array specifying the names of Vectors to
11
11
  # be duplicated. Will duplicate the entire DataFrame if not specified.
12
12
  def dup(vectors_to_dup = nil)
13
- vectors_to_dup ||= @vectors.to_a
13
+ # No argument: copy by position so a MultiIndex with duplicate tuples (which cannot be
14
+ # re-looked-up unambiguously by name) is preserved intact.
15
+ if vectors_to_dup.nil?
16
+ return DaruLite::DataFrame.new(
17
+ @data.map(&:dup), order: @vectors.dup, index: @index.dup, name: @name, clone: true
18
+ )
19
+ end
14
20
 
15
21
  src = vectors_to_dup.map { |vec| @data[@vectors.pos(vec)].dup }
16
22
  new_order = DaruLite::Index.new(vectors_to_dup)
@@ -226,7 +226,7 @@ module DaruLite
226
226
 
227
227
  return @data[pos] if pos.is_a?(Integer)
228
228
 
229
- new_vectors = pos.map { |tuple| @data[@vectors[tuple]] }
229
+ new_vectors = @vectors.positions_for(names.flatten).map { |position| @data[position] }
230
230
 
231
231
  pos = pos.drop_left_level(names.size) if names.size < @vectors.width
232
232
 
@@ -25,8 +25,11 @@ module DaruLite
25
25
  def each_vector_with_index
26
26
  return to_enum(:each_vector_with_index) unless block_given?
27
27
 
28
- @vectors.each do |vector|
29
- yield @data[@vectors[vector]], vector
28
+ # Snapshot the data/name pairing up front so the block may safely mutate the DataFrame
29
+ # (e.g. add vectors) mid-iteration, and pair by position so duplicate MultiIndex tuples
30
+ # don't trigger an ambiguous name lookup.
31
+ @data.zip(@vectors.to_a).each do |vector, vector_index| # rubocop:disable Style/ExplicitBlockArgument -- preserve 2-arg yield semantics
32
+ yield vector, vector_index
30
33
  end
31
34
 
32
35
  self
@@ -158,18 +158,26 @@ module DaruLite
158
158
 
159
159
  private
160
160
 
161
+ # Resolve a sort-key name to a single integer position. A MultiIndex with duplicate tuples
162
+ # maps a name to several positions; sorting by such a key uses the first match.
163
+ def sort_vector_position(name)
164
+ @vectors.is_a?(MultiIndex) ? @vectors.positions_for(name).first : @vectors[name]
165
+ end
166
+
161
167
  def convert_categorical_vectors(names)
162
168
  names.filter_map do |n|
163
- next unless self[n].category?
169
+ position = sort_vector_position(n)
170
+ vector = @data[position]
171
+ next unless vector.category?
164
172
 
165
- old = [n, self[n]]
166
- self[n] = DaruLite::Vector.new(self[n].to_ints)
173
+ old = [position, vector]
174
+ @data[position] = DaruLite::Vector.new(vector.to_ints)
167
175
  old
168
176
  end
169
177
  end
170
178
 
171
179
  def restore_categorical_vectors(old)
172
- old.each { |name, vector| self[name] = vector }
180
+ old.each { |position, vector| @data[position] = vector }
173
181
  end
174
182
 
175
183
  def sort_build_row(vector_locs, by_blocks, ascending, handle_nils, r1, r2) # rubocop:disable Metrics/ParameterLists
@@ -223,7 +231,7 @@ module DaruLite
223
231
  handle_nils = sort_coerce_boolean opts, :handle_nils, false, vector_order.size
224
232
 
225
233
  by_blocks = vector_order.map { |v| (opts[:by] || {})[v] }
226
- vector_locs = vector_order.map { |v| @vectors[v] }
234
+ vector_locs = vector_order.map { |v| sort_vector_position(v) }
227
235
 
228
236
  lambda do |index1, index2|
229
237
  # Build left and right array to compare two rows
@@ -267,8 +267,12 @@ module DaruLite
267
267
  def delete_vector(vector)
268
268
  raise IndexError, "Vector #{vector} does not exist." unless @vectors.include?(vector)
269
269
 
270
- @data.delete_at @vectors[vector]
271
- @vectors = DaruLite::Index.new @vectors.to_a - [vector]
270
+ # Delete the first matching column by position: a MultiIndex with duplicate tuples resolves a
271
+ # name to several positions, and removing every duplicate name would desync @vectors from @data.
272
+ # Reuse the index's own #delete_at, which preserves the index type.
273
+ position = @vectors.is_a?(MultiIndex) ? @vectors.positions_for(vector).first : @vectors[vector]
274
+ @data.delete_at(position)
275
+ @vectors = @vectors.delete_at(position)
272
276
 
273
277
  self
274
278
  end
@@ -469,7 +473,9 @@ module DaruLite
469
473
  @size == other.size &&
470
474
  @index == other.index &&
471
475
  @vectors == other.vectors &&
472
- @vectors.to_a.all? { |v| self[v] == other[v] }
476
+ # Compare columns by position: name-based access is ambiguous (and recurses infinitely)
477
+ # when the vectors are a MultiIndex with duplicate tuples.
478
+ @data == other.data
473
479
  end
474
480
 
475
481
  # Converts the specified non category type vectors to category type vectors
@@ -589,7 +595,7 @@ module DaruLite
589
595
  end
590
596
 
591
597
  if pos.is_a?(DaruLite::Index)
592
- assign_multiple_vectors pos, v
598
+ assign_multiple_vectors name, pos, v
593
599
  elsif pos == name &&
594
600
  (@vectors.include?(name) || (pos.is_a?(Integer) && pos < @data.size))
595
601
 
@@ -599,10 +605,14 @@ module DaruLite
599
605
  end
600
606
  end
601
607
 
602
- def assign_multiple_vectors(pos, v)
603
- pos.each do |p|
604
- @data[@vectors[p]] = v
605
- end
608
+ # Assign +v+ to every column matching +name+ (a partial key, or a full MultiIndex tuple that is
609
+ # duplicated across several positions). For a MultiIndex, resolve to positions directly: a
610
+ # duplicated tuple re-looked-up by name would return a MultiIndex instead of an integer.
611
+ def assign_multiple_vectors(name, pos, v)
612
+ positions = @vectors.is_a?(MultiIndex) ? @vectors.positions_for(name.flatten) : pos.map { |p| @vectors[p] }
613
+ # Give all but the first match an independent copy so duplicate columns don't share one
614
+ # Vector instance (a later cell mutation would otherwise leak across both).
615
+ positions.each_with_index { |position, i| @data[position] = i.zero? ? v : v.dup }
606
616
  end
607
617
 
608
618
  def assign_or_add_vector_rough(name, v)
@@ -764,11 +774,42 @@ module DaruLite
764
774
  end
765
775
 
766
776
  def initialize_from_array_of_vectors(source, vectors, index, opts)
777
+ # Build @data positionally rather than round-tripping through a name-keyed hash: duplicate
778
+ # MultiIndex tuples would collapse into a single hash entry and corrupt the columns.
779
+ @vectors = Index.coerce(vectors)
780
+
781
+ first_index = source.first.index
782
+ vectors_have_same_index = source.all? { |vector| first_index == vector.index }
783
+
767
784
  clone = opts[:clone] != false
768
- hsh = vectors.each_with_index.to_h do |name, idx|
769
- [name, source[idx]]
785
+ clone = true unless index || vectors_have_same_index
786
+
787
+ @index = deduce_index_from_vectors(index, source, first_index, vectors_have_same_index)
788
+
789
+ @data =
790
+ if !clone
791
+ source.dup
792
+ elsif vectors_have_same_index
793
+ source.map(&:dup)
794
+ else
795
+ source.map.with_index { |vector, position| reindex_vector(vector, @vectors.to_a[position]) }
796
+ end
797
+ end
798
+
799
+ def deduce_index_from_vectors(index, source, first_index, vectors_have_same_index)
800
+ if index
801
+ Index.coerce(index)
802
+ elsif vectors_have_same_index
803
+ first_index.dup
804
+ else
805
+ DaruLite::Index.new(source.map { |vector| vector.index.to_a }.flatten.uniq.sort)
806
+ end
807
+ end
808
+
809
+ def reindex_vector(vector, name)
810
+ DaruLite::Vector.new([], name: name, index: @index).tap do |new_vector|
811
+ @index.each { |idx| new_vector[idx] = vector.index.include?(idx) ? vector[idx] : nil }
770
812
  end
771
- initialize(hsh, index: index, order: vectors, name: @name, clone: clone)
772
813
  end
773
814
 
774
815
  def initialize_from_array_of_hashes(source, vectors, index, _opts)
@@ -260,8 +260,8 @@ module DaruLite
260
260
  @offset =
261
261
  case opts[:freq]
262
262
  when :infer then Helper.infer_offset(data)
263
- when nil then nil
264
- else Helper.offset_from_frequency(opts[:freq])
263
+ when nil then nil
264
+ else Helper.offset_from_frequency(opts[:freq])
265
265
  end
266
266
 
267
267
  @frequency = @offset&.freq_string
@@ -1,5 +1,9 @@
1
1
  module DaruLite
2
2
  class CategoricalIndex < Index
3
+ UNSUPPORTED_RANGE_MSG =
4
+ 'CategoricalIndex does not support label-range slicing: ' \
5
+ 'categories are unordered and may repeat'.freeze
6
+
3
7
  # Create a categorical index object.
4
8
  # @param indexes [Array<object>] array of indexes
5
9
  # @return [DaruLite::CategoricalIndex] categorical index
@@ -51,6 +55,10 @@ module DaruLite
51
55
  # x.pos :a, 1
52
56
  # # => [0, 1, 2, 3]
53
57
  def pos(*indexes)
58
+ if indexes.size == 1 && indexes.first.is_a?(Range) && !include?(indexes.first)
59
+ return pos_from_range(indexes.first)
60
+ end
61
+
54
62
  positions = indexes.map do |index|
55
63
  if include? index
56
64
  @cat_hash[index]
@@ -81,6 +89,35 @@ module DaruLite
81
89
  nil
82
90
  end
83
91
 
92
+ # Returns the category located at a positional value.
93
+ # Mirrors Index#key but reads the categorical structure instead of @keys,
94
+ # which CategoricalIndex never populates.
95
+ # @param value [Integer] positional value
96
+ # @return [object, nil] category at the position, or nil if out of range
97
+ # @example
98
+ # idx = DaruLite::CategoricalIndex.new [:a, :b, :a]
99
+ # idx.key 1 # => :b
100
+ # idx.key 99 # => nil
101
+ def key(value)
102
+ return nil unless value.is_a?(Numeric)
103
+
104
+ to_a[value]
105
+ end
106
+
107
+ # Label-range slicing is not supported: categories are unordered and may
108
+ # repeat, so a range of positions between two labels is ambiguous.
109
+ # @raise [ArgumentError] always
110
+ def slice(*)
111
+ raise ArgumentError, UNSUPPORTED_RANGE_MSG
112
+ end
113
+
114
+ # Label-range slicing is not supported: categories are unordered and may
115
+ # repeat, so a range of positions between two labels is ambiguous.
116
+ # @raise [ArgumentError] always
117
+ def subset_slice(*)
118
+ raise ArgumentError, UNSUPPORTED_RANGE_MSG
119
+ end
120
+
84
121
  # Returns index value from position
85
122
  # @param pos [Integer] the position to look for
86
123
  # @return [object] category corresponding to position
@@ -206,6 +243,31 @@ module DaruLite
206
243
 
207
244
  private
208
245
 
246
+ # Single-key position lookup, resolved against the categorical structure.
247
+ # Overrides Index#numeric_pos, which reads the never-populated @relation_hash.
248
+ # @raise [IndexError] when the key is neither a valid category nor position
249
+ def numeric_pos(key)
250
+ pos(key)
251
+ end
252
+
253
+ # Label-range slicing is not supported (see #slice / #subset_slice).
254
+ # @raise [ArgumentError] always
255
+ def preprocess_range(_rng)
256
+ raise ArgumentError, UNSUPPORTED_RANGE_MSG
257
+ end
258
+
259
+ # A positional (integer) range resolves to the positions it spans, mirroring
260
+ # pandas iloc / Vector#head / #tail. A non-integer label range is rejected
261
+ # because categories are unordered and may repeat (like pandas .loc on an
262
+ # unordered CategoricalIndex).
263
+ # @raise [ArgumentError] when the range bounds are not integers
264
+ def pos_from_range(rng)
265
+ raise ArgumentError, UNSUPPORTED_RANGE_MSG unless rng.begin.is_a?(Integer) && rng.end.is_a?(Integer)
266
+
267
+ # size.times.to_a[rng] clamps to valid positions; nil (fully out of range) -> []
268
+ size.times.to_a[rng] || []
269
+ end
270
+
209
271
  def int_from_cat(cat)
210
272
  @cat_hash.keys.index cat
211
273
  end
@@ -227,22 +227,40 @@ module DaruLite
227
227
  collection.one? ? collection.first : collection
228
228
  end
229
229
 
230
- def retrieve_from_tuple(key)
230
+ # Return every integer position whose tuple matches the (possibly partial) key.
231
+ # Unlike #retrieve_from_tuple, the return type is always an Array of positions,
232
+ # even when a full tuple is duplicated across several positions.
233
+ #
234
+ # @param key [Array] a full or left-anchored partial tuple
235
+ # @return [Array<Integer>] matching positions, in ascending order
236
+ def positions_for(key)
231
237
  chosen = []
232
238
 
233
239
  key.each_with_index do |k, depth|
234
- level_index = @levels[depth][k]
240
+ # An over-long key runs past the defined levels; guard explicitly rather than letting
241
+ # nil[k] raise NoMethodError.
242
+ level = @levels[depth]
243
+ raise IndexError, "Specified index #{key.inspect} do not exist" if level.nil?
244
+
245
+ level_index = level[k]
235
246
  raise IndexError, "Specified index #{key.inspect} do not exist" if level_index.nil?
236
247
 
237
- label = @labels[depth]
238
- chosen = find_all_indexes label, level_index, chosen
248
+ chosen = find_all_indexes(@labels[depth], level_index, chosen)
239
249
  end
240
250
 
251
+ chosen
252
+ end
253
+
254
+ def retrieve_from_tuple(key)
255
+ chosen = positions_for(key)
256
+
257
+ # Every level component existed individually but no tuple matches the full combination
258
+ # (e.g. [:a, :two, :foo] when that triple was never indexed together).
259
+ raise IndexError, "Specified index #{key.inspect} do not exist" if chosen.empty?
260
+
241
261
  return chosen[0] if chosen.size == 1 && key.size == @levels.size
242
262
 
243
263
  multi_index_from_multiple_selections(chosen)
244
- rescue NoMethodError
245
- raise IndexError, "Specified index #{key.inspect} do not exist"
246
264
  end
247
265
 
248
266
  def multi_index_from_multiple_selections(chosen)
@@ -166,7 +166,9 @@ module DaruLite
166
166
  private
167
167
 
168
168
  def apply_method_to_numerics(method, *)
169
- numerics = @vectors.to_a.map { |n| [n, @data[@vectors[n]]] }.select { |_n, v| v.numeric? }
169
+ numerics = @data.each_with_index
170
+ .map { |v, position| [@vectors.to_a[position], v] }
171
+ .select { |_n, v| v.numeric? }
170
172
  computed = numerics.map { |_n, v| v.send(method, *) }
171
173
 
172
174
  DaruLite::DataFrame.new(computed, index: @index, order: numerics.map(&:first), clone: false)
@@ -187,10 +189,12 @@ module DaruLite
187
189
  end
188
190
 
189
191
  def compute_stats(method)
190
- DaruLite::Vector.new(
191
- numeric_vectors.to_h { [it, self[it].send(method)] },
192
- name: method
193
- )
192
+ # Iterate by position (via each_vector_with_index) instead of `self[name]`: a MultiIndex
193
+ # with duplicate tuples would make the name lookup ambiguous and collapse the result.
194
+ stats = each_vector_with_index.select { |vector, _name| vector.numeric? }
195
+ .map { |vector, name| [name, vector.send(method)] }
196
+
197
+ DaruLite::Vector.new(stats.map(&:last), index: stats.map(&:first), name: method)
194
198
  end
195
199
  alias sds std
196
200
  alias variance variance_sample
@@ -554,7 +554,7 @@ module DaruLite
554
554
  def standardize(use_population = false)
555
555
  m ||= mean
556
556
  sd = use_population ? sdp : sds
557
- return DaruLite::Vector.new([nil] * size) if m.nil? || sd.to_d == BigDecimal('0.0')
557
+ return DaruLite::Vector.new([nil] * size) if m.nil? || sd.nil? || sd.zero?
558
558
 
559
559
  vector_standardized_compute m, sd
560
560
  end
@@ -1,3 +1,3 @@
1
1
  module DaruLite
2
- VERSION = '0.3.3'.freeze
2
+ VERSION = '0.3.5'.freeze
3
3
  end
data/lib/daru_lite.rb CHANGED
@@ -73,6 +73,7 @@ require 'matrix'
73
73
  require 'forwardable'
74
74
  require 'erb'
75
75
  require 'date'
76
+ require 'json'
76
77
 
77
78
  require 'daru_lite/version'
78
79