daru_lite 0.4.0 → 0.5.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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +30 -0
  3. data/README.md +1 -1
  4. data/lib/daru_lite/data_frame/convertible.rb +0 -33
  5. data/lib/daru_lite/data_frame/fetchable.rb +1 -1
  6. data/lib/daru_lite/data_frame/i_o_able.rb +0 -102
  7. data/lib/daru_lite/data_frame/indexable.rb +24 -0
  8. data/lib/daru_lite/data_frame/relabelable.rb +125 -0
  9. data/lib/daru_lite/data_frame/sortable.rb +27 -1
  10. data/lib/daru_lite/dataframe.rb +3 -4
  11. data/lib/daru_lite/index/categorical_index.rb +8 -0
  12. data/lib/daru_lite/index/index.rb +0 -1
  13. data/lib/daru_lite/index/multi_index.rb +8 -5
  14. data/lib/daru_lite/io/io.rb +0 -195
  15. data/lib/daru_lite/maths/arithmetic/vector.rb +31 -0
  16. data/lib/daru_lite/maths/statistics/vector/descriptive.rb +134 -0
  17. data/lib/daru_lite/maths/statistics/vector/dispersion.rb +120 -0
  18. data/lib/daru_lite/maths/statistics/vector/extrema.rb +217 -0
  19. data/lib/daru_lite/maths/statistics/vector/normalization.rb +86 -0
  20. data/lib/daru_lite/maths/statistics/vector/percentile.rb +64 -0
  21. data/lib/daru_lite/maths/statistics/vector/sampling.rb +56 -0
  22. data/lib/daru_lite/maths/statistics/vector/time_series.rb +350 -0
  23. data/lib/daru_lite/maths/statistics/vector.rb +16 -994
  24. data/lib/daru_lite/vector/convertible.rb +0 -32
  25. data/lib/daru_lite/vector.rb +0 -9
  26. data/lib/daru_lite/version.rb +1 -1
  27. data/lib/daru_lite.rb +0 -30
  28. metadata +9 -75
  29. data/lib/daru_lite/date_time/index.rb +0 -570
  30. data/lib/daru_lite/date_time/offsets.rb +0 -397
  31. data/lib/daru_lite/extensions/which_dsl.rb +0 -53
  32. data/lib/daru_lite/io/sql_data_source.rb +0 -80
  33. data/lib/daru_lite/iruby/helpers.rb +0 -38
  34. data/lib/daru_lite/iruby/templates/dataframe.html.erb +0 -5
  35. data/lib/daru_lite/iruby/templates/dataframe_mi.html.erb +0 -5
  36. data/lib/daru_lite/iruby/templates/dataframe_mi_tbody.html.erb +0 -35
  37. data/lib/daru_lite/iruby/templates/dataframe_mi_thead.html.erb +0 -21
  38. data/lib/daru_lite/iruby/templates/dataframe_tbody.html.erb +0 -28
  39. data/lib/daru_lite/iruby/templates/dataframe_thead.html.erb +0 -21
  40. data/lib/daru_lite/iruby/templates/multi_index.html.erb +0 -12
  41. data/lib/daru_lite/iruby/templates/vector.html.erb +0 -5
  42. data/lib/daru_lite/iruby/templates/vector_mi.html.erb +0 -5
  43. data/lib/daru_lite/iruby/templates/vector_mi_tbody.html.erb +0 -26
  44. data/lib/daru_lite/iruby/templates/vector_mi_thead.html.erb +0 -8
  45. data/lib/daru_lite/iruby/templates/vector_tbody.html.erb +0 -17
  46. data/lib/daru_lite/iruby/templates/vector_thead.html.erb +0 -8
@@ -1,570 +0,0 @@
1
- module DaruLite
2
- # Private module for storing helper functions for DateTimeIndex.
3
- # @private
4
- module DateTimeIndexHelper
5
- class << self
6
- OFFSETS_HASH = {
7
- 'S' => DaruLite::Offsets::Second,
8
- 'M' => DaruLite::Offsets::Minute,
9
- 'H' => DaruLite::Offsets::Hour,
10
- 'D' => DaruLite::Offsets::Day,
11
- 'W' => DaruLite::Offsets::Week,
12
- 'MONTH' => DaruLite::Offsets::Month,
13
- 'MB' => DaruLite::Offsets::MonthBegin,
14
- 'ME' => DaruLite::Offsets::MonthEnd,
15
- 'YEAR' => DaruLite::Offsets::Year,
16
- 'YB' => DaruLite::Offsets::YearBegin,
17
- 'YE' => DaruLite::Offsets::YearEnd
18
- }.freeze
19
-
20
- TIME_INTERVALS = {
21
- Rational(1, 1) => DaruLite::Offsets::Day,
22
- Rational(1, 24) => DaruLite::Offsets::Hour,
23
- Rational(1, 1440) => DaruLite::Offsets::Minute,
24
- Rational(1, 86_400) => DaruLite::Offsets::Second
25
- }.freeze
26
-
27
- DOW_REGEXP = Regexp.new(DaruLite::DAYS_OF_WEEK.keys.join('|'))
28
- FREQUENCY_PATTERN = /^
29
- (?<multiplier>[0-9]+)?
30
- (
31
- (?<offset>MONTH|YEAR|S|H|MB|ME|M|D|YB|YE) |
32
- (?<offset>W)(-(?<weekday>#{DOW_REGEXP}))?
33
- )$/x
34
-
35
- # Generates a DaruLite::DateOffset object for generic offsets or one of the
36
- # specialized classed within DaruLite::Offsets depending on the 'frequency'
37
- # string.
38
- def offset_from_frequency(frequency)
39
- return frequency if frequency.is_a?(DaruLite::DateOffset)
40
-
41
- frequency ||= 'D'
42
-
43
- matched = FREQUENCY_PATTERN.match(frequency) or
44
- raise ArgumentError, "Invalid frequency string #{frequency}"
45
-
46
- n = (matched[:multiplier] || 1).to_i
47
- offset_string = matched[:offset]
48
- offset_klass = OFFSETS_HASH[offset_string] or
49
- raise ArgumentError, "Cannont interpret offset #{offset_string}"
50
-
51
- if offset_string == 'W'
52
- offset_klass.new(n, weekday: DaruLite::DAYS_OF_WEEK[matched[:weekday]])
53
- else
54
- offset_klass.new(n)
55
- end
56
- end
57
-
58
- def coerce_date(date)
59
- return date unless date.is_a?(String)
60
-
61
- date_time_from(date, determine_date_precision_of(date))
62
- end
63
-
64
- def begin_from_offset?(offset, start)
65
- offset.is_a?(DaruLite::Offsets::Tick) ||
66
- (offset.respond_to?(:on_offset?) && offset.on_offset?(start))
67
- end
68
-
69
- def generate_data(start, en, offset, periods)
70
- data = []
71
- new_date = begin_from_offset?(offset, start) ? start : offset + start
72
-
73
- if periods.nil? # use end
74
- loop do
75
- break if new_date > en
76
-
77
- data << new_date
78
- new_date = offset + new_date
79
- end
80
- else
81
- periods.times do
82
- data << new_date
83
- new_date = offset + new_date
84
- end
85
- end
86
-
87
- data
88
- end
89
-
90
- def verify_start_and_end(start, en)
91
- raise ArgumentError, 'Start and end cannot be the same' if start == en
92
- raise ArgumentError, 'Start must be lesser than end' if start > en
93
- raise ArgumentError, 'Only same time zones are allowed' if start.zone != en.zone
94
- end
95
-
96
- def infer_offset(data)
97
- diffs = data.each_cons(2).map { |d1, d2| d2 - d1 }
98
-
99
- return nil unless diffs.uniq.one?
100
-
101
- return TIME_INTERVALS[diffs.first].new if TIME_INTERVALS.include?(diffs.first)
102
-
103
- number_of_seconds = diffs.first / DaruLite::Offsets::Second.new.multiplier
104
- DaruLite::Offsets::Second.new(number_of_seconds.numerator) if number_of_seconds.denominator == 1
105
- end
106
-
107
- def find_index_of_date(data, date_time)
108
- searched = data.bsearch { |d| d[0] >= date_time }
109
- raise(ArgumentError, "Cannot find #{date_time}") if searched.nil? || searched[0] != date_time
110
-
111
- searched[1]
112
- end
113
-
114
- def find_date_string_bounds(date_string)
115
- date_precision = determine_date_precision_of date_string
116
- date_time = date_time_from date_string, date_precision
117
- generate_bounds date_time, date_precision
118
- end
119
-
120
- def date_time_from(date_string, date_precision)
121
- case date_precision
122
- when :year
123
- DateTime.new(date_string.gsub(/[^0-9]/, '').to_i)
124
- when :month
125
- DateTime.new(
126
- date_string.match(/\d\d\d\d/).to_s.to_i,
127
- date_string.match(/-\d?\d/).to_s.delete('-').to_i
128
- )
129
- else
130
- DateTime.parse date_string
131
- end
132
- end
133
-
134
- DATE_PRECISION_REGEXP = /^(\d\d\d\d)(-\d{1,2}(-\d{1,2}( \d{1,2}(:\d{1,2}(:\d{1,2})?)?)?)?)?$/
135
- DATE_PRECISIONS = [nil, :year, :month, :day, :hour, :min, :sec].freeze
136
-
137
- def determine_date_precision_of(date_string)
138
- components = date_string.scan(DATE_PRECISION_REGEXP).flatten.compact
139
- DATE_PRECISIONS[components.count] or
140
- raise ArgumentError, "Unacceptable date string #{date_string}"
141
- end
142
-
143
- def generate_bounds(date_time, date_precision) # rubocop:disable Metrics/MethodLength
144
- # FIXME: about that ^ disable: I'd like to use my zverok/time_boots here, which will simplify things
145
- case date_precision
146
- when :year
147
- [
148
- date_time,
149
- DateTime.new(date_time.year, 12, 31, 23, 59, 59)
150
- ]
151
- when :month
152
- [
153
- date_time,
154
- DateTime.new(date_time.year, date_time.month, ((date_time >> 1) - 1).day,
155
- 23, 59, 59)
156
- ]
157
- when :day
158
- [
159
- date_time,
160
- DateTime.new(date_time.year, date_time.month, date_time.day, 23, 59, 59)
161
- ]
162
- when :hour
163
- [
164
- date_time,
165
- DateTime.new(date_time.year, date_time.month, date_time.day,
166
- date_time.hour, 59, 59)
167
- ]
168
- when :min
169
- [
170
- date_time,
171
- DateTime.new(date_time.year, date_time.month, date_time.day,
172
- date_time.hour, date_time.min, 59)
173
- ]
174
- else # second or when precision is same as offset
175
- [date_time, date_time]
176
- end
177
- end
178
-
179
- def possibly_convert_to_date_time(data)
180
- data[0].is_a?(String) ? data.map! { |e| DateTime.parse(e) } : data
181
- end
182
-
183
- def last_date(data)
184
- data.max_by { |d| d[1] }
185
- end
186
-
187
- def key_out_of_bounds?(key, data)
188
- dates = data.transpose.first
189
-
190
- precision = determine_date_precision_of key
191
- date_time = date_time_from key, precision
192
-
193
- # FIXME: I'm pretty suspicious about logic here:
194
- # why only year & month? - zverok 2016-05-16
195
-
196
- case precision
197
- when :year
198
- year_out_of_bounds?(date_time, dates)
199
- when :month
200
- year_month_out_of_bounds?(date_time, dates)
201
- end
202
- end
203
-
204
- private
205
-
206
- def year_out_of_bounds?(date_time, dates)
207
- date_time.year < dates.first.year || date_time.year > dates.last.year
208
- end
209
-
210
- def year_month_out_of_bounds?(date_time, dates)
211
- (date_time.year < dates.first.year && date_time.month < dates.first.month) ||
212
- (date_time.year > dates.last.year && date_time.month > dates.last.month)
213
- end
214
- end
215
- end
216
-
217
- class DateTimeIndex < Index
218
- include Enumerable
219
-
220
- Helper = DateTimeIndexHelper
221
-
222
- def self.try_create(source)
223
- new(source, freq: :infer) if source && ArrayHelper.array_of?(source, ::DateTime)
224
- end
225
-
226
- def each(&)
227
- to_a.each(&)
228
- end
229
-
230
- attr_reader :frequency, :offset, :periods, :keys
231
-
232
- # Create a DateTimeIndex with or without a frequency in data. The constructor
233
- # should be used for creating DateTimeIndex by directly passing in DateTime
234
- # objects or date-like strings, typically in cases where values with frequency
235
- # are not needed.
236
- #
237
- # @param [Array<String>, Array<DateTime>] data Array of date-like Strings or
238
- # actual DateTime objects for creating the DateTimeIndex.
239
- # @param [Hash] opts Hash of options for configuring index.
240
- # @option opts [Symbol, NilClass, String, DaruLite::DateOffset, DaruLite::Offsets::*] freq
241
- # Option for specifying the frequency of data, if applicable. If `:infer` is
242
- # passed to this option, daru will try to infer the frequency of the data
243
- # by itself.
244
- #
245
- # @example Usage of DateTimeIndex constructor
246
- # index = DaruLite::DateTimeIndex.new(
247
- # [DateTime.new(2012,4,5), DateTime.new(2012,4,6),
248
- # DateTime.new(2012,4,7), DateTime.new(2012,4,8)])
249
- # #=>#<DateTimeIndex:84232240 offset=nil periods=4 data=[2012-04-05T00:00:00+00:00...2012-04-08T00:00:00+00:00]>
250
- #
251
- # index = DaruLite::DateTimeIndex.new([
252
- # DateTime.new(2012,4,5), DateTime.new(2012,4,6), DateTime.new(2012,4,7),
253
- # DateTime.new(2012,4,8), DateTime.new(2012,4,9), DateTime.new(2012,4,10),
254
- # DateTime.new(2012,4,11), DateTime.new(2012,4,12)], freq: :infer)
255
- # #=>#<DateTimeIndex:84198340 offset=D periods=8 data=[2012-04-05T00:00:00+00:00...2012-04-12T00:00:00+00:00]>
256
- def initialize(data, opts = { freq: nil })
257
- super(data)
258
- Helper.possibly_convert_to_date_time data
259
-
260
- @offset =
261
- case opts[:freq]
262
- when :infer then Helper.infer_offset(data)
263
- when nil then nil
264
- else Helper.offset_from_frequency(opts[:freq])
265
- end
266
-
267
- @frequency = @offset&.freq_string
268
- @data = data.each_with_index.to_a.sort_by(&:first)
269
-
270
- @periods = data.size
271
- end
272
-
273
- # Custom dup method for DateTimeIndex
274
- def dup
275
- DaruLite::DateTimeIndex.new(@data.transpose[0], freq: @offset)
276
- end
277
-
278
- # Create a date range by specifying the start, end, periods and frequency
279
- # of the data.
280
- #
281
- # @param [Hash] opts Options hash to create the date range with
282
- # @option opts [String, DateTime] :start A DateTime object or date-like
283
- # string that defines the start of the date range.
284
- # @option opts [String, DateTime] :end A DateTime object or date-like string
285
- # that defines the end of the date range.
286
- # @option opts [String, DaruLite::DateOffset, DaruLite::Offsets::*] :freq ('D') The interval
287
- # between each date in the index. This can either be a string specifying
288
- # the frequency (i.e. one of the frequency aliases) or an offset object.
289
- # @option opts [Integer] :periods The number of periods that should go into
290
- # this index. Takes precedence over `:end`.
291
- # @return [DateTimeIndex] DateTimeIndex object of the specified parameters.
292
- #
293
- # == Notes
294
- #
295
- # If you specify :start and :end options as strings, they can be complete or
296
- # partial dates and daru will intelligently infer the date from the string
297
- # directly. However, note that the date-like string must be in the format
298
- # `YYYY-MM-DD HH:MM:SS`.
299
- #
300
- # The string aliases supported by the :freq option are as follows:
301
- #
302
- # * 'S' - seconds
303
- # * 'M' - minutes
304
- # * 'H' - hours
305
- # * 'D' - days
306
- # * 'W' - Week (default) anchored on sunday
307
- # * 'W-SUN' - Same as 'W'
308
- # * 'W-MON' - Week anchored on monday
309
- # * 'W-TUE' - Week anchored on tuesday
310
- # * 'W-WED' - Week anchored on wednesday
311
- # * 'W-THU' - Week anchored on thursday
312
- # * 'W-FRI' - Week anchored on friday
313
- # * 'W-SAT' - Week anchored on saturday
314
- # * 'MONTH' - Month
315
- # * 'YEAR' - One year
316
- # * 'MB' - month begin
317
- # * 'ME' - month end
318
- # * 'YB' - year begin
319
- # * 'YE' - year end
320
- #
321
- # Multiples of these can also be specified. For example '2S' for 2 seconds
322
- # or '2ME' for two month end offsets.
323
- #
324
- # Currently the precision of DateTimeIndex is upto seconds only, though this
325
- # will improve in the future.
326
- #
327
- # @example Creating date ranges
328
- # DaruLite::DateTimeIndex.date_range(
329
- # :start => DateTime.new(2014,5,1),
330
- # :end => DateTime.new(2014,5,2), :freq => '6H')
331
- # #=>#<DateTimeIndex:83600130 offset=H periods=5 data=[2014-05-01T00:00:00+00:00...2014-05-02T00:00:00+00:00]>
332
- #
333
- # DaruLite::DateTimeIndex.date_range(
334
- # :start => '2012-5-2', :periods => 50, :freq => 'ME')
335
- # #=> #<DateTimeIndex:83549940 offset=ME periods=50 data=[2012-05-31T00:00:00+00:00...2016-06-30T00:00:00+00:00]>
336
- def self.date_range(opts = {})
337
- start = Helper.coerce_date opts[:start]
338
- en = Helper.coerce_date opts[:end]
339
- Helper.verify_start_and_end(start, en) unless en.nil?
340
- offset = Helper.offset_from_frequency opts[:freq]
341
- data = Helper.generate_data start, en, offset, opts[:periods]
342
-
343
- DateTimeIndex.new(data, freq: offset)
344
- end
345
-
346
- # Retreive a slice or a an individual index number from the index.
347
- #
348
- # @param key [String, DateTime] Specify a date partially (as a String) or
349
- # completely to retrieve.
350
- def [](*key)
351
- return slice(*key) if key.size != 1
352
-
353
- key = key[0]
354
- case key
355
- when Numeric
356
- key
357
- when DateTime
358
- Helper.find_index_of_date(@data, key)
359
- when Range
360
- # FIXME: get_by_range is suspiciously close to just #slice,
361
- # but one of specs fails when replacing it with just slice
362
- get_by_range(key.first, key.last)
363
- else
364
- raise ArgumentError, "Key #{key} is out of bounds" if
365
- Helper.key_out_of_bounds?(key, @data)
366
-
367
- slice(*Helper.find_date_string_bounds(key))
368
- end
369
- end
370
-
371
- def pos(*)
372
- # to filled
373
- out = self[*]
374
- return out if out.is_a? Numeric
375
-
376
- out.map { |date| self[date] }
377
- end
378
-
379
- def subset(*)
380
- self[*]
381
- end
382
-
383
- def valid?(*)
384
- self[*]
385
- true
386
- rescue IndexError
387
- false
388
- end
389
-
390
- # Retrive a slice of the index by specifying first and last members of the slice.
391
- #
392
- # @param [String, DateTime] first Start of the slice as a string or DateTime.
393
- # @param [String, DateTime] last End of the slice as a string or DateTime.
394
- def slice(first, last)
395
- if first.is_a?(Integer) && last.is_a?(Integer)
396
- DateTimeIndex.new(to_a[first..last], freq: @offset)
397
- else
398
- first = Helper.find_date_string_bounds(first)[0] if first.is_a?(String)
399
- last = Helper.find_date_string_bounds(last)[1] if last.is_a?(String)
400
-
401
- slice_between_dates first, last
402
- end
403
- end
404
-
405
- # Return the DateTimeIndex as an Array of DateTime objects.
406
- # @return [Array<DateTime>] Array of containing DateTimes.
407
- def to_a
408
- if @offset
409
- @data
410
- else
411
- @data.sort_by(&:last)
412
- end.transpose.first || []
413
- end
414
-
415
- # Size of index.
416
- def size
417
- @periods
418
- end
419
-
420
- def ==(other)
421
- to_a == other.to_a
422
- end
423
-
424
- def inspect
425
- meta = [@periods, @frequency ? "frequency=#{@frequency}" : nil].compact.join(', ')
426
- return "#<#{self.class}(#{meta})>" if @data.empty?
427
-
428
- "#<#{self.class}(#{meta}) " \
429
- "#{@data.first[0]}...#{@data.last[0]}>"
430
- end
431
-
432
- # Shift all dates in the index by a positive number in the future. The dates
433
- # are shifted by the same amount as that specified in the offset.
434
- #
435
- # @param [Integer, DaruLite::DateOffset, DaruLite::Offsets::*] distance Distance by
436
- # which each date should be shifted. Passing an offset object to #shift
437
- # will offset each data point by the offset value. Passing a positive
438
- # integer will offset each data point by the same offset that it was
439
- # created with.
440
- # @return [DateTimeIndex] Returns a new, shifted DateTimeIndex object.
441
- # @example Using the shift method
442
- # index = DaruLite::DateTimeIndex.date_range(
443
- # :start => '2012', :periods => 10, :freq => 'YEAR')
444
- #
445
- # # Passing a offset to shift
446
- # index.shift(DaruLite::Offsets::Hour.new(3))
447
- # #=>#<DateTimeIndex:84038960 offset=nil periods=10 data=[2012-01-01T03:00:00+00:00...2021-01-01T03:00:00+00:00]>
448
- #
449
- # # Pass an integer to shift
450
- # index.shift(4)
451
- # #=>#<DateTimeIndex:83979630 offset=YEAR periods=10 data=[2016-01-01T00:00:00+00:00...2025-01-01T00:00:00+00:00]>
452
- def shift(distance)
453
- distance.is_a?(Integer) && distance.negative? and
454
- raise IndexError, "Distance #{distance} cannot be negative"
455
-
456
- _shift(distance)
457
- end
458
-
459
- # Shift all dates in the index to the past. The dates are shifted by the same
460
- # amount as that specified in the offset.
461
- #
462
- # @param [Integer, DaruLite::DateOffset, DaruLite::Offsets::*] distance Integer or
463
- # DaruLite::DateOffset. Distance by which each date should be shifted. Passing
464
- # an offset object to #lag will offset each data point by the offset value.
465
- # Passing a positive integer will offset each data point by the same offset
466
- # that it was created with.
467
- # @return [DateTimeIndex] A new lagged DateTimeIndex object.
468
- def lag(distance)
469
- distance.is_a?(Integer) && distance.negative? and
470
- raise IndexError, "Distance #{distance} cannot be negative"
471
-
472
- _shift(-distance)
473
- end
474
-
475
- # simplecov:disable
476
- def _dump(_depth)
477
- Marshal.dump(data: to_a, freq: @offset)
478
- end
479
-
480
- def self._load(data)
481
- h = Marshal.load data
482
-
483
- DaruLite::DateTimeIndex.new(h[:data], freq: h[:freq])
484
- end
485
- # simplecov:enable
486
-
487
- # @!method year
488
- # @return [Array<Integer>] Array containing year of each index.
489
- # @!method month
490
- # @return [Array<Integer>] Array containing month of each index.
491
- # @!method day
492
- # @return [Array<Integer>] Array containing day of each index.
493
- # @!method hour
494
- # @return [Array<Integer>] Array containing hour of each index.
495
- # @!method min
496
- # @return [Array<Integer>] Array containing minutes of each index.
497
- # @!method sec
498
- # @return [Array<Integer>] Array containing seconds of each index.
499
- %i[year month day hour min sec].each do |meth|
500
- define_method(meth) do
501
- each_with_object([]) do |d, arr|
502
- arr << d.send(meth)
503
- end
504
- end
505
- end
506
-
507
- # Check if a date exists in the index. Will be inferred from string in case
508
- # you pass a string. Recommened specifying the full date as a DateTime object.
509
- def include?(date_time)
510
- return false unless date_time.is_a?(String) || date_time.is_a?(DateTime)
511
-
512
- if date_time.is_a?(String)
513
- date_precision = Helper.determine_date_precision_of date_time
514
- date_time = Helper.date_time_from date_time, date_precision
515
- end
516
-
517
- result, = @data.bsearch { |d| d[0] >= date_time }
518
- result && result == date_time
519
- end
520
-
521
- # Return true if the DateTimeIndex is empty.
522
- def empty?
523
- @data.empty?
524
- end
525
-
526
- private
527
-
528
- def get_by_range(first, last)
529
- return slice(first, last) if first.is_a?(Integer) && last.is_a?(Integer)
530
-
531
- raise ArgumentError, "Keys #{first} and #{last} are out of bounds" if
532
- Helper.key_out_of_bounds?(first, @data) && Helper.key_out_of_bounds?(last, @data)
533
-
534
- slice first, last
535
- end
536
-
537
- def slice_between_dates(first, last)
538
- # about that ^ disable: I'm waiting for cleaner understanding
539
- # of offsets logic. Reference: https://github.com/v0dro/daru/commit/7e1c34aec9516a9ba33037b4a1daaaaf1de0726a#diff-a95ef410a8e1f4ea3cc48d231bb880faR250
540
- start = @data.bsearch { |d| d[0] >= first }
541
- after_en = @data.bsearch { |d| d[0] > last }
542
-
543
- if @offset
544
- en = after_en ? @data[after_en[1] - 1] : @data.last
545
- return start[1] if start == en
546
-
547
- DateTimeIndex.date_range start: start[0], end: en[0], freq: @offset
548
- else
549
- st = @data.index(start)
550
- en = after_en ? @data.index(after_en) - 1 : Helper.last_date(@data)[1]
551
- return start[1] if st == en
552
-
553
- DateTimeIndex.new(@data[st..en].transpose[0] || []) # empty slice guard
554
- end
555
- end
556
-
557
- def _shift(distance)
558
- if distance.is_a?(Integer)
559
- raise IndexError, 'To lag non-freq date time index pass an offset.' unless @offset
560
-
561
- start = @data[0][0]
562
- off = distance.positive? ? @offset : -@offset
563
- distance.abs.times { start = off + start }
564
- DateTimeIndex.date_range(start: start, periods: @periods, freq: @offset)
565
- else
566
- DateTimeIndex.new(to_a.map { |e| distance + e }, freq: :infer)
567
- end
568
- end
569
- end
570
- end