daru_lite 0.1

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 (149) hide show
  1. checksums.yaml +7 -0
  2. data/.github/ISSUE_TEMPLATE.md +18 -0
  3. data/.github/workflows/ci.yml +33 -0
  4. data/.gitignore +10 -0
  5. data/.rspec +2 -0
  6. data/.rubocop.yml +27 -0
  7. data/.rubocop_todo.yml +137 -0
  8. data/CONTRIBUTING.md +47 -0
  9. data/Gemfile +2 -0
  10. data/History.md +4 -0
  11. data/LICENSE +24 -0
  12. data/README.md +218 -0
  13. data/Rakefile +69 -0
  14. data/ReleasePolicy.md +20 -0
  15. data/benchmarks/TradeoffData.csv +65 -0
  16. data/benchmarks/csv_reading.rb +22 -0
  17. data/benchmarks/dataframe_creation.rb +39 -0
  18. data/benchmarks/db_loading.rb +34 -0
  19. data/benchmarks/duplicating.rb +45 -0
  20. data/benchmarks/group_by.rb +32 -0
  21. data/benchmarks/joining.rb +52 -0
  22. data/benchmarks/row_access.rb +41 -0
  23. data/benchmarks/row_assign.rb +36 -0
  24. data/benchmarks/sorting.rb +51 -0
  25. data/benchmarks/statistics.rb +28 -0
  26. data/benchmarks/vector_access.rb +31 -0
  27. data/benchmarks/vector_assign.rb +42 -0
  28. data/benchmarks/where_clause.rb +48 -0
  29. data/benchmarks/where_vs_filter.rb +28 -0
  30. data/daru_lite.gemspec +55 -0
  31. data/images/README.md +5 -0
  32. data/images/con0.png +0 -0
  33. data/images/con1.png +0 -0
  34. data/images/init0.png +0 -0
  35. data/images/init1.png +0 -0
  36. data/images/man0.png +0 -0
  37. data/images/man1.png +0 -0
  38. data/images/man2.png +0 -0
  39. data/images/man3.png +0 -0
  40. data/images/man4.png +0 -0
  41. data/images/man5.png +0 -0
  42. data/images/man6.png +0 -0
  43. data/lib/daru_lite/accessors/array_wrapper.rb +109 -0
  44. data/lib/daru_lite/accessors/dataframe_by_row.rb +25 -0
  45. data/lib/daru_lite/accessors/mdarray_wrapper.rb +7 -0
  46. data/lib/daru_lite/category.rb +929 -0
  47. data/lib/daru_lite/configuration.rb +34 -0
  48. data/lib/daru_lite/core/group_by.rb +403 -0
  49. data/lib/daru_lite/core/merge.rb +270 -0
  50. data/lib/daru_lite/core/query.rb +109 -0
  51. data/lib/daru_lite/dataframe.rb +3080 -0
  52. data/lib/daru_lite/date_time/index.rb +569 -0
  53. data/lib/daru_lite/date_time/offsets.rb +397 -0
  54. data/lib/daru_lite/exceptions.rb +2 -0
  55. data/lib/daru_lite/extensions/which_dsl.rb +53 -0
  56. data/lib/daru_lite/formatters/table.rb +52 -0
  57. data/lib/daru_lite/helpers/array.rb +53 -0
  58. data/lib/daru_lite/index/categorical_index.rb +201 -0
  59. data/lib/daru_lite/index/index.rb +374 -0
  60. data/lib/daru_lite/index/multi_index.rb +374 -0
  61. data/lib/daru_lite/io/csv/converters.rb +21 -0
  62. data/lib/daru_lite/io/io.rb +294 -0
  63. data/lib/daru_lite/io/sql_data_source.rb +97 -0
  64. data/lib/daru_lite/iruby/helpers.rb +38 -0
  65. data/lib/daru_lite/iruby/templates/dataframe.html.erb +5 -0
  66. data/lib/daru_lite/iruby/templates/dataframe_mi.html.erb +5 -0
  67. data/lib/daru_lite/iruby/templates/dataframe_mi_tbody.html.erb +35 -0
  68. data/lib/daru_lite/iruby/templates/dataframe_mi_thead.html.erb +21 -0
  69. data/lib/daru_lite/iruby/templates/dataframe_tbody.html.erb +28 -0
  70. data/lib/daru_lite/iruby/templates/dataframe_thead.html.erb +21 -0
  71. data/lib/daru_lite/iruby/templates/multi_index.html.erb +12 -0
  72. data/lib/daru_lite/iruby/templates/vector.html.erb +5 -0
  73. data/lib/daru_lite/iruby/templates/vector_mi.html.erb +5 -0
  74. data/lib/daru_lite/iruby/templates/vector_mi_tbody.html.erb +26 -0
  75. data/lib/daru_lite/iruby/templates/vector_mi_thead.html.erb +8 -0
  76. data/lib/daru_lite/iruby/templates/vector_tbody.html.erb +17 -0
  77. data/lib/daru_lite/iruby/templates/vector_thead.html.erb +8 -0
  78. data/lib/daru_lite/maths/arithmetic/dataframe.rb +91 -0
  79. data/lib/daru_lite/maths/arithmetic/vector.rb +117 -0
  80. data/lib/daru_lite/maths/statistics/dataframe.rb +202 -0
  81. data/lib/daru_lite/maths/statistics/vector.rb +1019 -0
  82. data/lib/daru_lite/monkeys.rb +56 -0
  83. data/lib/daru_lite/vector.rb +1678 -0
  84. data/lib/daru_lite/version.rb +3 -0
  85. data/lib/daru_lite.rb +99 -0
  86. data/profile/_base.rb +23 -0
  87. data/profile/df_to_a.rb +10 -0
  88. data/profile/filter.rb +13 -0
  89. data/profile/joining.rb +13 -0
  90. data/profile/sorting.rb +12 -0
  91. data/profile/vector_each_with_index.rb +9 -0
  92. data/profile/vector_new.rb +9 -0
  93. data/spec/accessors/array_wrapper_spec.rb +3 -0
  94. data/spec/category_spec.rb +1741 -0
  95. data/spec/core/group_by_spec.rb +655 -0
  96. data/spec/core/merge_spec.rb +179 -0
  97. data/spec/core/query_spec.rb +347 -0
  98. data/spec/daru_lite_spec.rb +22 -0
  99. data/spec/dataframe_spec.rb +4330 -0
  100. data/spec/date_time/data_spec.rb +197 -0
  101. data/spec/date_time/date_time_index_helper_spec.rb +72 -0
  102. data/spec/date_time/index_spec.rb +588 -0
  103. data/spec/date_time/offsets_spec.rb +465 -0
  104. data/spec/extensions/which_dsl_spec.rb +38 -0
  105. data/spec/fixtures/bank2.dat +200 -0
  106. data/spec/fixtures/boolean_converter_test.csv +5 -0
  107. data/spec/fixtures/countries.json +7794 -0
  108. data/spec/fixtures/duplicates.csv +32 -0
  109. data/spec/fixtures/eciresults.html +394 -0
  110. data/spec/fixtures/empties.dat +2 -0
  111. data/spec/fixtures/empty_rows_test.csv +17 -0
  112. data/spec/fixtures/macau.html +3691 -0
  113. data/spec/fixtures/macd_data.csv +150 -0
  114. data/spec/fixtures/matrix_test.csv +100 -0
  115. data/spec/fixtures/moneycontrol.html +6812 -0
  116. data/spec/fixtures/music_data.tsv +2501 -0
  117. data/spec/fixtures/repeated_fields.csv +7 -0
  118. data/spec/fixtures/sales-funnel.csv +18 -0
  119. data/spec/fixtures/scientific_notation.csv +4 -0
  120. data/spec/fixtures/string_converter_test.csv +5 -0
  121. data/spec/fixtures/strings.dat +2 -0
  122. data/spec/fixtures/test_xls.xls +0 -0
  123. data/spec/fixtures/test_xls_2.xls +0 -0
  124. data/spec/fixtures/url_test.txt~ +0 -0
  125. data/spec/fixtures/valid_markup.html +62 -0
  126. data/spec/fixtures/wiki_climate.html +1243 -0
  127. data/spec/fixtures/wiki_table_info.html +631 -0
  128. data/spec/formatters/table_formatter_spec.rb +137 -0
  129. data/spec/helpers_spec.rb +8 -0
  130. data/spec/index/categorical_index_spec.rb +170 -0
  131. data/spec/index/index_spec.rb +417 -0
  132. data/spec/index/multi_index_spec.rb +680 -0
  133. data/spec/io/io_spec.rb +373 -0
  134. data/spec/io/sql_data_source_spec.rb +56 -0
  135. data/spec/iruby/dataframe_spec.rb +170 -0
  136. data/spec/iruby/helpers_spec.rb +49 -0
  137. data/spec/iruby/multi_index_spec.rb +37 -0
  138. data/spec/iruby/vector_spec.rb +105 -0
  139. data/spec/maths/arithmetic/dataframe_spec.rb +148 -0
  140. data/spec/maths/arithmetic/vector_spec.rb +165 -0
  141. data/spec/maths/statistics/dataframe_spec.rb +178 -0
  142. data/spec/maths/statistics/vector_spec.rb +756 -0
  143. data/spec/monkeys_spec.rb +42 -0
  144. data/spec/shared/vector_display_spec.rb +213 -0
  145. data/spec/spec_helper.rb +87 -0
  146. data/spec/support/database_helper.rb +30 -0
  147. data/spec/support/matchers.rb +5 -0
  148. data/spec/vector_spec.rb +2293 -0
  149. metadata +571 -0
@@ -0,0 +1,465 @@
1
+ include DaruLite
2
+
3
+ describe DateOffset do
4
+ context "#initialize, #+, #-" do
5
+ it "creates a seconds offset" do
6
+ offset = DateOffset.new(secs: 5)
7
+ expect(offset + DateTime.new(2012,3,4,23,4,00)).to eq(
8
+ DateTime.new(2012,3,4,23,4,05))
9
+ expect(offset - DateTime.new(2012,4,2,22,4,23)).to eq(
10
+ DateTime.new(2012,4,2,22,4,18))
11
+ end
12
+
13
+ it "creates a minutes offset" do
14
+ offset = DateOffset.new(mins: 2)
15
+ expect(offset + DateTime.new(2013,4,5,12,45,44)).to eq(
16
+ DateTime.new(2013,4,5,12,47,44))
17
+ end
18
+
19
+ it "creates an hours offset" do
20
+ offset = DateOffset.new(hours: 3)
21
+ expect(offset + DateTime.new(2024,3,2)).to eq(
22
+ DateTime.new(2024,3,2,03,0,0))
23
+ end
24
+
25
+ it "creates a days offset" do
26
+ offset = DateOffset.new(days: 12)
27
+ expect(offset + DateTime.new(2012,5,4)).to eq(
28
+ DateTime.new(2012,5,16))
29
+ end
30
+
31
+ it "creates a weeks offset" do
32
+ offset = DateOffset.new(weeks: 2)
33
+ expect(offset + DateTime.new(2012,3,1)).to eq(
34
+ DateTime.new(2012,3,15))
35
+ end
36
+
37
+ it "creates a months offset" do
38
+ offset = DateOffset.new(months: 1)
39
+ expect(offset + DateTime.new(2012,3,1)).to eq(
40
+ DateTime.new(2012,4,1))
41
+ end
42
+
43
+ it "creates a years offset" do
44
+ offset = DateOffset.new(years: 2)
45
+ expect(offset + DateTime.new(2012,5,30)).to eq(
46
+ DateTime.new(2014,5,30))
47
+ end
48
+
49
+ it "supports 'n' option to apply same offset multiple times" do
50
+ offset = DateOffset.new(days: 3, n: 4)
51
+ expect(offset + DateTime.new(2012,3,1)).to eq(
52
+ DateTime.new(2012,3,13))
53
+ end
54
+ end
55
+ end
56
+
57
+ describe NegativeDateOffset do
58
+ context "#initialize, #+, #-" do
59
+ it "creates a seconds offset" do
60
+ offset = -DateOffset.new(secs: 5)
61
+ expect(offset + DateTime.new(2012,3,4,23,4,05)).to eq(
62
+ DateTime.new(2012,3,4,23,4,00))
63
+ expect(offset - DateTime.new(2012,4,2,22,4,18)).to eq(
64
+ DateTime.new(2012,4,2,22,4,23))
65
+ end
66
+
67
+ it "creates a minutes offset" do
68
+ offset = -DateOffset.new(mins: 2)
69
+ expect(offset + DateTime.new(2013,4,5,12,47,44)).to eq(
70
+ DateTime.new(2013,4,5,12,45,44))
71
+ end
72
+
73
+ it "creates an hours offset" do
74
+ offset = -DateOffset.new(hours: 3)
75
+ expect(offset + DateTime.new(2024,3,2,3)).to eq(
76
+ DateTime.new(2024,3,2,0))
77
+ end
78
+
79
+ it "creates a days offset" do
80
+ offset = -DateOffset.new(days: 12)
81
+ expect(offset + DateTime.new(2012,5,16)).to eq(
82
+ DateTime.new(2012,5,4))
83
+ end
84
+
85
+ it "creates a weeks offset" do
86
+ offset = -DateOffset.new(weeks: 2)
87
+ expect(offset + DateTime.new(2012,3,15)).to eq(
88
+ DateTime.new(2012,3,1))
89
+ end
90
+
91
+ it "creates a months offset" do
92
+ offset = -DateOffset.new(months: 1)
93
+ expect(offset + DateTime.new(2012,4,1)).to eq(
94
+ DateTime.new(2012,3,1))
95
+ end
96
+
97
+ it "creates a years offset" do
98
+ offset = -DateOffset.new(years: 2)
99
+ expect(offset + DateTime.new(2014,5,30)).to eq(
100
+ DateTime.new(2012,5,30))
101
+ end
102
+
103
+ it "supports 'n' option to apply same offset multiple times" do
104
+ offset = -DateOffset.new(days: 3, n: 4)
105
+ expect(offset + DateTime.new(2012,3,13)).to eq(
106
+ DateTime.new(2012,3,1))
107
+ end
108
+ end
109
+
110
+ context "#-@" do
111
+ it "creates a date offset" do
112
+ negative_offset = -DateOffset.new(secs: 5)
113
+ offset = -negative_offset
114
+
115
+ expect(offset + DateTime.new(2012,3,4,23,4,0)).to eq(
116
+ DateTime.new(2012,3,4,23,4,5))
117
+ expect(offset - DateTime.new(2012,4,2,22,4,23)).to eq(
118
+ DateTime.new(2012,4,2,22,4,18))
119
+ end
120
+ end
121
+ end
122
+
123
+ include DaruLite::Offsets
124
+ describe Offsets do
125
+ describe Second do
126
+ before do
127
+ @offset = Offsets::Second.new(5)
128
+ end
129
+
130
+ context "#initialize" do
131
+ it "creates a seconds offset" do
132
+ expect(@offset + DateTime.new(2012,3,4,23,4,00)).to eq(
133
+ DateTime.new(2012,3,4,23,4,05))
134
+ end
135
+ end
136
+
137
+ context "#-" do
138
+ it "reduces by seconds" do
139
+ expect(@offset - DateTime.new(2012,2,3,12,4,23)).to eq(
140
+ DateTime.new(2012,2,3,12,4,18))
141
+ end
142
+ end
143
+
144
+ context "#==" do
145
+ it 'equals the same offset' do
146
+ expect(Offsets::Second.new(5)).to eq(Offsets::Second.new(5))
147
+ end
148
+
149
+ it 'does equal a different offset' do
150
+ expect(Offsets::Second.new(5)).to_not eq(Offsets::Second.new(4))
151
+ end
152
+
153
+ it 'equals offets with the same period' do
154
+ expect(Offsets::Second.new(60)).to eq(Offsets::Minute.new())
155
+ end
156
+ end
157
+ end
158
+
159
+ describe Minute do
160
+ context "#initialize" do
161
+ it "creates a minutes offset" do
162
+ offset = Offsets::Minute.new(2)
163
+ expect(offset + DateTime.new(2013,4,5,12,45,44)).to eq(
164
+ DateTime.new(2013,4,5,12,47,44))
165
+ end
166
+ end
167
+
168
+ context "#-" do
169
+ it "reduces by minutes" do
170
+ end
171
+ end
172
+ end
173
+
174
+ describe Hour do
175
+ context "#initialize" do
176
+ it "creates an hours offset" do
177
+ offset = Offsets::Hour.new(3)
178
+ expect(offset + DateTime.new(2024,3,2)).to eq(
179
+ DateTime.new(2024,3,2,03,0,0))
180
+ end
181
+ end
182
+
183
+ context "#-" do
184
+ it "reduces by hours" do
185
+ end
186
+ end
187
+ end
188
+
189
+ describe Day do
190
+ context "#initialize" do
191
+ it "creates a days offset" do
192
+ offset = Offsets::Day.new(12)
193
+ expect(offset + DateTime.new(2012,5,4)).to eq(
194
+ DateTime.new(2012,5,16))
195
+ end
196
+ end
197
+
198
+ context "#-" do
199
+ it "reduces by days" do
200
+ end
201
+ end
202
+ end
203
+
204
+ describe Week do
205
+ DAYS_ADVANCE = {
206
+ sunday: DateTime.new(2015,7,12),
207
+ monday: DateTime.new(2015,7,13),
208
+ tuesday: DateTime.new(2015,7,14),
209
+ wednesday: DateTime.new(2015,7,15),
210
+ thursday: DateTime.new(2015,7,16),
211
+ friday: DateTime.new(2015,7,17),
212
+ saturday: DateTime.new(2015,7,11)
213
+ }
214
+
215
+ DAYS_ADVANCE.each.with_index do |day_date, i|
216
+ offset = Offsets::Week.new(weekday: i)
217
+
218
+ context "#initialize" do
219
+ date = DateTime.new(2015,7,10)
220
+
221
+ it "creates anchored Week offset for #{day_date[0]}" do
222
+ expect(offset + date).to eq(day_date[1])
223
+ end
224
+ end
225
+
226
+ context "#on_offset?" do
227
+ it "checks if given DateTime is on the offset itself? (#{day_date[0]})" do
228
+ expect(offset.on_offset?(DAYS_ADVANCE[day_date[0]])).to eq(true)
229
+ end
230
+ end
231
+ end
232
+
233
+ context "#-" do
234
+ DAYS_RETREAT = {
235
+ sunday: DateTime.new(2015,7,12),
236
+ monday: DateTime.new(2015,7,13),
237
+ tuesday: DateTime.new(2015,7,14),
238
+ wednesday: DateTime.new(2015,7,8),
239
+ thursday: DateTime.new(2015,7,9),
240
+ friday: DateTime.new(2015,7,10),
241
+ saturday: DateTime.new(2015,7,11)
242
+ }
243
+ date = DateTime.new(2015,7,15)
244
+
245
+ DAYS_RETREAT.each.with_index do |day_date, i|
246
+ it "decreases the date to nearest preceding #{day_date[0]}" do
247
+ offset = Offsets::Week.new(weekday: i)
248
+ expect(offset - date).to eq(day_date[1])
249
+ end
250
+ end
251
+ end
252
+ end
253
+
254
+ describe Month do
255
+ context "#initialize" do
256
+ it "creates a month offset" do
257
+ offset = Offsets::Month.new(3)
258
+ expect(offset + DateTime.new(2012,2,29)).to eq(
259
+ DateTime.new(2012,5,29))
260
+ end
261
+ end
262
+
263
+ context "#-" do
264
+ it "reduces date by a month" do
265
+ offset = Offsets::Month.new(2)
266
+ expect(offset - DateTime.new(2012,4,2)).to eq(
267
+ DateTime.new(2012,2,2))
268
+ end
269
+ end
270
+ end
271
+
272
+ describe MonthBegin do
273
+ before do
274
+ @offset = Offsets::MonthBegin.new
275
+ @n_offset = Offsets::MonthBegin.new(3)
276
+ end
277
+
278
+ context "#+" do
279
+ it "offsets to beginning of next month" do
280
+ expect(@offset + DateTime.new(2012,3,25)).to eq(
281
+ DateTime.new(2012,4,1))
282
+
283
+ expect(@n_offset + DateTime.new(2011,3,1,5)).to eq(
284
+ DateTime.new(2011,6,1,5))
285
+ end
286
+ end
287
+
288
+ context "#on_offset?" do
289
+ it "returns true if date is on the offset" do
290
+ expect(@offset.on_offset?(DateTime.new(2012,4,1))).to eq(true)
291
+ end
292
+
293
+ it "returns false if date is not on the offset" do
294
+ expect(@offset.on_offset?(DateTime.new(2012,4,30))).to eq(false)
295
+ end
296
+ end
297
+
298
+ context "#-" do
299
+ it "decreases to beginning of the current month if not on offset" do
300
+ expect(@offset - DateTime.new(2012,4,5)).to eq(
301
+ DateTime.new(2012,4,1))
302
+
303
+ expect(@n_offset - DateTime.new(2012,5,3)).to eq(
304
+ DateTime.new(2012,3,1))
305
+ end
306
+
307
+ it "decreases to beginning of the previous month if on offset" do
308
+ expect(@offset - DateTime.new(2012,5,1)).to eq(
309
+ DateTime.new(2012,4,1))
310
+
311
+ expect(@n_offset - DateTime.new(2012,6,1)).to eq(
312
+ DateTime.new(2012,3,1))
313
+ end
314
+ end
315
+ end
316
+
317
+ describe MonthEnd do
318
+ before do
319
+ @offset = Offsets::MonthEnd.new
320
+ @n_offset = Offsets::MonthEnd.new(2)
321
+ end
322
+
323
+ context "#+" do
324
+ it "increases date to end of next month if on offset" do
325
+ expect(@offset + DateTime.new(2012,2,29)).to eq(
326
+ DateTime.new(2012,3,31))
327
+
328
+ expect(@n_offset + DateTime.new(2012,2,29)).to eq(
329
+ DateTime.new(2012,4,30))
330
+ end
331
+
332
+ it "increases date to end of this month if not on offset" do
333
+ expect(@offset + DateTime.new(2012,4,4)).to eq(
334
+ DateTime.new(2012,4,30))
335
+
336
+ expect(@n_offset + DateTime.new(2012,5,2)).to eq(
337
+ DateTime.new(2012,6,30))
338
+ end
339
+ end
340
+
341
+ context "#-" do
342
+ it "decreases to end of the previous month" do
343
+ expect(@offset - DateTime.new(2012,2,29)).to eq(
344
+ DateTime.new(2012,1,31))
345
+
346
+ expect(@n_offset - DateTime.new(2015,3,3)).to eq(
347
+ DateTime.new(2015,1,31))
348
+ end
349
+ end
350
+
351
+ context "#on_offset?" do
352
+ it "returns true if date is on the offset" do
353
+ expect(@offset.on_offset?(DateTime.new(2012,4,30))).to eq(true)
354
+ end
355
+
356
+ it "returns false if date is not on the offset" do
357
+ expect(@offset.on_offset?(DateTime.new(2012,4,1))).to eq(false)
358
+ end
359
+ end
360
+ end
361
+
362
+ describe Year do
363
+ before do
364
+ @offset = Offsets::Year.new
365
+ end
366
+
367
+ context "#+" do
368
+ it "increaes date by a year" do
369
+ expect(@offset + DateTime.new(2012,5,2)).to eq(
370
+ DateTime.new(2013,5,2))
371
+ end
372
+ end
373
+
374
+ context "#-" do
375
+ it "decreases date by a year" do
376
+ expect(@offset - DateTime.new(2011,6,25)).to eq(
377
+ DateTime.new(2010,6,25))
378
+ end
379
+ end
380
+ end
381
+
382
+ describe YearBegin do
383
+ before do
384
+ @offset = Offsets::YearBegin.new
385
+ @n_offset = Offsets::YearBegin.new(2)
386
+ end
387
+
388
+ context "#+" do
389
+ it "offsets date to future" do
390
+ expect(@n_offset + DateTime.new(2012,3,25)).to eq(
391
+ DateTime.new(2014,1,1))
392
+ end
393
+ end
394
+
395
+ context "#on_offset?" do
396
+ it "checks if date is on the offset" do
397
+ expect(@offset.on_offset?(DateTime.new(2012,1,1))).to eq(
398
+ true)
399
+
400
+ expect(@offset.on_offset?(DateTime.new(2012,12,31))).to eq(
401
+ false)
402
+ end
403
+ end
404
+
405
+ context "#-" do
406
+ it "decreases to beginning of the year if not on offset" do
407
+ expect(@offset - DateTime.new(2012,4,2)).to eq(
408
+ DateTime.new(2012,1,1))
409
+
410
+ expect(@n_offset - DateTime.new(2012,5,5)).to eq(
411
+ DateTime.new(2011,1,1))
412
+ end
413
+
414
+ it "decreases to beginning of previous year if on offset" do
415
+ expect(@offset - DateTime.new(2012,1,1)).to eq(
416
+ DateTime.new(2011,1,1))
417
+
418
+ expect(@n_offset - DateTime.new(2013,1,1)).to eq(
419
+ DateTime.new(2011,1,1))
420
+ end
421
+ end
422
+ end
423
+
424
+ describe YearEnd do
425
+ before do
426
+ @offset = Offsets::YearEnd.new
427
+ @n_offset = Offsets::YearEnd.new(2)
428
+ end
429
+
430
+ context "#+" do
431
+ it "increases to end of same year if not on offset" do
432
+ expect(@offset + DateTime.new(2011,5,2,4,2)).to eq(
433
+ DateTime.new(2011,12,31,4,2))
434
+
435
+ expect(@n_offset + DateTime.new(2011,5,2,4,2)).to eq(
436
+ DateTime.new(2012,12,31,4,2))
437
+ end
438
+
439
+ it "increases to end of next year if on offset" do
440
+ expect(@offset + DateTime.new(2012,12,31,4)).to eq(
441
+ DateTime.new(2013,12,31,4))
442
+
443
+ expect(@n_offset + DateTime.new(2012,12,31,4)).to eq(
444
+ DateTime.new(2014,12,31,4))
445
+ end
446
+ end
447
+
448
+ context "#-" do
449
+ it "decreases to end of previous year" do
450
+ expect(@offset - DateTime.new(2012,2,3)).to eq(
451
+ DateTime.new(2011,12,31))
452
+
453
+ expect(@n_offset - DateTime.new(2011,5,6)).to eq(
454
+ DateTime.new(2010,12,31))
455
+ end
456
+ end
457
+
458
+ context "#on_offset?" do
459
+ it "reports whether on offset or not" do
460
+ expect(@offset.on_offset?(DateTime.new(2012,12,31))).to eq(true)
461
+ expect(@offset.on_offset?(DateTime.new(2012,1,1))).to eq(false)
462
+ end
463
+ end
464
+ end
465
+ end
@@ -0,0 +1,38 @@
1
+ require 'daru_lite/extensions/which_dsl'
2
+
3
+ describe "which DSL" do
4
+ before do
5
+ @df = DaruLite::DataFrame.new({
6
+ number: [1,2,3,4,5,6,Float::NAN],
7
+ sym: [:one, :two, :three, :four, :five, :six, :seven],
8
+ names: ['sameer', 'john', 'james', 'omisha', 'priyanka', 'shravan',nil]
9
+ })
10
+ end
11
+
12
+ it "accepts simple single eq statement" do
13
+ answer = DaruLite::DataFrame.new({
14
+ number: [4],
15
+ sym: [:four],
16
+ names: ['omisha']
17
+ }, index: DaruLite::Index.new([3])
18
+ )
19
+ expect( @df.which{ `number` == 4 } ).to eq(answer)
20
+ end
21
+
22
+ it "accepts somewhat complex comparison operator chaining" do
23
+ answer = DaruLite::DataFrame.new({
24
+ number: [3,4],
25
+ sym: [:three, :four],
26
+ names: ['james', 'omisha']
27
+ }, index: DaruLite::Index.new([2,3]))
28
+ expect(
29
+ @df.which{ (`names` == 'james') | (`sym` == :four) }
30
+ ).to eq(answer)
31
+ end
32
+
33
+ it "accepts vector methods" do
34
+ # expect( @df.which{ `number` == `number`.only_valid.max } ).to eq(@df.row_at(5)) # row_at(5) return a Vector object!?
35
+ expect( @df.which{ `number` <= `number`.only_valid.max } ).to eq(@df.row_at(0..5))
36
+ end
37
+
38
+ end