Class: SahmAsDataframe::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sahm_as_dataframe/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



9
10
11
# File 'lib/sahm_as_dataframe/client.rb', line 9

def initialize
  @unrate_reader = FredAsDataframe::Client.new('UNRATE')
end

Instance Attribute Details

#unrate_readerObject (readonly)

Returns the value of attribute unrate_reader.



7
8
9
# File 'lib/sahm_as_dataframe/client.rb', line 7

def unrate_reader
  @unrate_reader
end

Instance Method Details

#fetch(start: nil, fin: nil, interval: '1d') ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sahm_as_dataframe/client.rb', line 13

def fetch(start: nil, fin: nil, interval: '1d')
  unrate = @unrate_reader.fetch(start: start, fin: fin, interval: interval)
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} unrate = #{unrate.inspect}"}
  unrate['row_num'] = 1.upto(unrate.length).to_a
  unrate = unrate.set_sorted('row_num')

  # Rails.logger.info { "#{__FILE__}:#{__LINE__} unrate = #{unrate.inspect}"}
  unrate['3m rolling avg'] = unrate.rolling(index_column: "row_num", period: "3i").agg(
    [
      Polars.mean("UNRATE").alias("3m rolling avg")
    ]
  )['3m rolling avg']
  unrate['12m rolling_min'] = unrate.rolling(index_column: "row_num", period: "12i").agg(
    [
      Polars.min("UNRATE").alias("12m rolling_min")
    ]
  )['12m rolling_min']
  unrate['SAHM indicator'] = unrate['3m rolling avg'] - unrate['12m rolling_min']

  unrate = unrate.drop('3m rolling avg', 'row_num', '12m rolling_min')
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} unrate = #{unrate.inspect}"}
  # unrate = unrate.drop_nulls
  unrate['SAHM recession'] = (unrate['SAHM indicator'] >= 0.5)
  # Rails.logger.info { "#{__FILE__}:#{__LINE__} unrate = #{unrate.inspect}"}
  unrate
end