Class: NyFrbAsDataframe::Hhdc
- Inherits:
-
Object
- Object
- NyFrbAsDataframe::Hhdc
- Includes:
- HTTParty
- Defined in:
- lib/ny_frb_as_dataframe/hhdc.rb
Instance Attribute Summary collapse
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #fetch(start: nil, fin: nil) ⇒ Object
-
#initialize(series = nil) ⇒ Hhdc
constructor
A new instance of Hhdc.
Constructor Details
#initialize(series = nil) ⇒ Hhdc
Returns a new instance of Hhdc.
17 18 19 |
# File 'lib/ny_frb_as_dataframe/hhdc.rb', line 17 def initialize(series = nil) @tag = series end |
Instance Attribute Details
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
15 16 17 |
# File 'lib/ny_frb_as_dataframe/hhdc.rb', line 15 def tag @tag end |
Instance Method Details
#fetch(start: nil, fin: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ny_frb_as_dataframe/hhdc.rb', line 21 def fetch(start: nil, fin: nil) df = nil SimpleXlsxReader.configuration.auto_slurp = true Tempfile.open(['foo', '.xlsx'], '/tmp', binmode: true) do |f| qtr = Date.today.year * 4 + (Date.today.month.div(3)+1) dta = nil resp = nil loop do u = "https://www.newyorkfed.org/medialibrary/interactives/householdcredit/data/xls/hhd_c_report_#{qtr.div(4)}q#{qtr.modulo(4)+1}.xlsx" dta = self.class.get(u) resp = dta.parsed_response break if (dta.response.code.to_i == 200 && dta.parsed_response.encoding.to_s == "ASCII-8BIT") qtr -= 1 end f.write(resp) f.rewind xlsx_file = SimpleXlsxReader.open(f.path) col_nm_hsh = { 'Total Debt Balance and Its Composition' => {'Mortgage' => 'Mort Debt Balance ($T)', 'HE Revolving' => 'HELOC Debt Balance ($T)', 'Auto Loan' => 'Car Loan Debt Balance ($T)', 'Credit Card' => 'CC Debt Balance ($T)', 'Student Loan' => 'Student Debt Balance ($T)', 'Other' => 'Other Debt Balance ($T)', 'Total' => 'Total Debt Balance ($T)'}, 'Number of Accounts by Loan Type' => {'Auto Loan' => 'Num Car Loan Accts (M)', 'Credit Card' => 'Num CC Accts (M)', 'Mortgage' => 'Num Mort Accts (M)', 'HE Revolving' => 'Num HELOC Accts (M)'}, 'Total Number of New and Closed Accounts and Consumer Credit Inquiries' => {'inquiry within 6 mo' => 'Num Credit Inquiries and New Accts', 'closed within 12 mo' => 'Num New Credit Accts Closed in 12 mo', 'open within 12 mo' => 'Num New Credit Accts Open after 12 mo'}, 'Mortgage Origination Volume by Riskscore' => {'<620' => 'New Mort w Riskscore <620 ($B)','620-659' => 'New Mort w Riskscore 620-659 ($B)','660-719' => 'New Mort w Riskscore 660-719 ($B)','720-759' => 'New Mort w Riskscore 720-759 ($B)','760+' => 'New Mort w Riskscore 760+ ($B)','TOTAL' => 'New Mort ($B)'}, 'Distribution of Riskscore of Mortgage Originations' => {'Median' => 'Median Riskscore for New Mort', '25th percentile' => 'Bottom 25%ile Riskscore for New Mort', '10th percentile' => 'Bottom 10%ile Riskscore for New Mort'}, 'Auto Loan Origination Volume by Riskscore' => {'<620' => 'New Car Loans w Riskscore <620 ($B)','620-659' => 'New Car Loans w Riskscore 620-659 ($B)','660-719' => 'New Car Loans w Riskscore 660-719 ($B)','720-759' => 'New Car Loans w Riskscore 720-759 ($B)','760+' => 'New Car Loans w Riskscore 760+ ($B)','TOTAL' => 'All New Car Loans ($B)'}, 'Distribution of Riskscore of Auto Loan Originations' => {'Median' => 'Median Riskscore for New Car Loan', '25th percentile' => 'Bottom 25%ile Riskscore for New Car Loan', '10th percentile' => 'Bottom 10%ile Riskscore for New Car Loan'}, 'Credit Limit and Balance for Credit Cards and HE Revolving' => {'Credit Card Balance' => 'CC Balance ($T)', 'Credit Card Available Credit' => 'CC Avail Credit ($T)', 'Credit Card Limit' => 'CC Limit ($T)', 'HE Revolving Balance' => 'HELOC Balance ($T)', 'HE Revolving Available Credit' => 'HELOC Avail Credit ($T)', 'HE Revolving Limit' => 'HELOC Limit ($T)'}, 'Total Balance by Delinquency Status' => {'Current' => '% Balances Current', '30 days late' => '% Balances 30d late', '60 days late' => '% Balances 60d late', '90 days late' => '% Balances 90d late', '120+ days late' => '% Balances 120+d late', 'Severely Derogatory' => '% Balances Severely Impair'}, 'Percent of Balance 90+ Days Delinquent by Loan Type' => {'MORTGAGE' => '% Mort Bal 90d+ late', 'HELOC' => '% HELOC Bal 90d+ late', 'AUTO' => '% Car Loan Bal 90d+ late', 'CC' => '% CC Bal 90d+ late', 'STUDENT LOAN' => '% Student Loan Bal 90d+ late', 'OTHER' => '% Other Bal 90d+ late', 'ALL' => '% All Bal 90d+ late'}, 'New Delinquent* Balances by Loan Type' => {'AUTO' => '% Car Loan Bal 0d->30d+ Late', 'CC' => '% CC Bal 0d->30d+ Late', 'MORTGAGE' => '% Mort Bal 0d->30d+ Late', 'HELOC' => '% HELOC Bal 0d->30d+ Late', 'STUDENT LOAN' => '% Student Loan Bal 0d->30d+ Late', 'OTHER' => '% Other Bal 0d->30d+ Late', 'Total' => '% All Bal 0d->30d+ Late'}, 'New Seriously Delinquent* Balances by Loan Type' => {'AUTO' => '% Car Loan Bal ->90d+ Late', 'CC' => '% CC Bal ->90d+ Late', 'MORTGAGE' => '% Mort Bal ->90d+ Late', 'HELOC' => '% HELOC Bal ->90d+ Late', 'STUDENT LOAN' => '% Student Loan Bal ->90d+ Late', 'OTHER' => '% Other Bal ->90d+ Late', 'ALL' => '% All Bal ->90d+ Late'}, 'Quarterly Transition Rates for Current Mortgage Accounts' => {'To 30-60 days late' => 'Qtrly % Mort 0->30-60d late', 'To 90+ days Late' => 'Qtrly % Mort ->90d+ Late'}, 'Quarterly Transition Rates for 30-60 Day Late Mortgage Accounts' => {'To current' => 'Qtrly % Mort % 30-60d->0d Late', 'To 90+ days Late' => 'Qtrly % Mort 30-60d->90d+ Late'}, 'Number of Consumers with New Foreclosures and Bankruptcies' => {'foreclosure' => 'Num Consumers w New Foreclosure', 'bankruptcy' => 'Num Consumers w New Bankruptcy'}, 'Third Party Collections' => {'proportion of consumers with collection' => '% Consumers w Collection', 'average collection amount per person with item' => 'Avg Collection Amt'}, 'Total Debt Balance by Age' => {'18-29' => 'Avg Total Debt Bal for 18-29 yo ($T)', '30-39' => 'Avg Total Debt Bal for 30-39 yo ($T)', '40-49' => 'Avg Total Debt Bal for 40-49 yo ($T)', '50-59' => 'Avg Total Debt Bal for 50-59 yo ($T)', '60-69' => 'Avg Total Debt Bal for 60-69 yo ($T)', '70+' => 'Avg Total Debt Bal for 70+ yo ($T)'}, 'Auto Loan Originations By Age' => {'18-29' => 'New Car Loans for 18-29 yo ($B)', '30-39' => 'New Car Loans for 30-39 yo ($B)', '40-49' => 'New Car Loans for 40-49 yo ($B)', '50-59' => 'New Car Loans for 50-59 yo ($B)', '60-69' => 'New Car Loans for 60-69 yo ($B)', '70+' => 'New Car Loans for 70+ yo ($B)'}, 'Total Mortgage Orginations by Age' => {'18-29' => 'New Mort for 18-29 yo ($B)', '30-39' => 'New Mort for 30-39 yo ($B)', '40-49' => 'New Mort for 40-49 yo ($B)', '50-59' => 'New Mort for 50-59 yo ($B)', '60-69' => 'New Mort for 60-69 yo ($B)', '70+' => 'New Mort for 70+ yo ($B)'}, 'Transition into Serious Delinquency (90+) by Age' => {'18-29' => 'Qtrly % ->90d+ late for 18-29 yo', '30-39' => 'Qtrly % ->90d+ late for 30-39 yo', '40-49' => 'Qtrly % ->90d+ late for 40-49 yo', '50-59' => 'Qtrly % ->90d+ late for 50-59 yo', '60-69' => 'Qtrly % ->90d+ late for 60-69 yo', '70+' => 'Qtrly % ->90d+ late for 70+ yo', 'all' => 'Qtrly % ->90d+ late'}, 'Transition into Serious Delinquency (90+) for Mortgages by Age' => {'18-29' => 'Qtrly % Mort ->90d+ late for 18-29 yo', '30-39' => 'Qtrly % Mort ->90d+ late for 30-39 yo', '40-49' => 'Qtrly % Mort ->90d+ late for 40-49 yo', '50-59' => 'Qtrly % Mort ->90d+ late for 50-59 yo', '60-69' => 'Qtrly % Mort ->90d+ late for 60-69 yo', '70+' => 'Qtrly % Mort ->90d+ late for 70+ yo', 'all' => 'Qtrly % Mort ->90d+ late'}, 'Transition into Serious Delinquency (90+) for Auto Loans by Age' => {'18-29' => 'Qtrly % Car Loans ->90d+ late for 18-29 yo', '30-39' => 'Qtrly % Car Loans ->90d+ late for 30-39 yo', '40-49' => 'Qtrly % Car Loans ->90d+ late for 40-49 yo', '50-59' => 'Qtrly % Car Loans ->90d+ late for 50-59 yo', '60-69' => 'Qtrly % Car Loans ->90d+ late for 60-69 yo', '70+' => 'Qtrly % Car Loans ->90d+ late for 70+ yo', 'all' => 'Qtrly % Car Loans ->90d+ late'}, 'Transition into Serious Delinquency (90+) for Credit Cards by Age' => {'18-29' => 'Qtrly % CC ->90d+ late for 18-29 yo', '30-39' => 'Qtrly % CC ->90d+ late for 30-39 yo', '40-49' => 'Qtrly % CC ->90d+ late for 40-49 yo', '50-59' => 'Qtrly % CC ->90d+ late for 50-59 yo', '60-69' => 'Qtrly % CC ->90d+ late for 60-69 yo', '70+' => 'Qtrly % CC ->90d+ late for 70+ yo', 'all' => 'Qtrly % CC ->90d+ late'}, 'Transition into Serious Delinquency (90+) for Student Loans by Age' => {'18-29' => 'Qtrly % Student Loan ->90d+ late for 18-29 yo', '30-39' => 'Qtrly % Student Loan ->90d+ late for 30-39 yo', '40-49' => 'Qtrly % Student Loan ->90d+ late for 40-49 yo', '50-59' => 'Qtrly % Student Loan ->90d+ late for 50-59 yo', 'all' => 'Qtrly % Student Loan ->90d+ late'}, 'New Foreclosures By Age' => {'18-29' => 'New Foreclosures for 18-29 yo (000)', '30-39' => 'New Foreclosures for 30-39 yo (000)', '40-49' => 'New Foreclosures for 40-49 yo (000)', '50-59' => 'New Foreclosures for 50-59 yo (000)', '60-69' => 'New Foreclosures for 60-69 yo (000)', '70+' => 'New Foreclosures for 70+ yo (000)'}, 'New Bankruptcy By Age' => {'18-29' => 'New Bankruptcies for 18-29 yo (000)', '30-39' => 'New Bankruptcies for 30-39 yo (000)', '40-49' => 'New Bankruptcies for 40-49 yo (000)', '50-59' => 'New Bankruptcies for 50-59 yo (000)', '60-69' => 'New Bankruptcies for 60-69 yo (000)', '70+' => 'New Bankruptcies for 70+ yo (000)'} } # Polars::Config.set_tbl_cols(-1) Polars::Config.set_tbl_rows(-1) done = false xlsx_file.sheets.each do |sht| # Begin per-sheet analysis nm = sht.name begin _ = sht.rows pg_nm_text = sht.rows pg_nm_text = pg_nm_text.first rescue begin pg_nm_text = sht.rows.first rescue done = true end end break if done == true pg_nm_text = pg_nm_text.first next unless col_nm_hsh.keys.include?(pg_nm_text) header_found = false csv_txt = [] sht.rows.each do |row| # Begin per-line analysis r = row if header_found == false if r.length > (r - col_nm_hsh[pg_nm_text].keys).length header_found = true 0.upto(r.length-1).to_a.each do |ndex| if ndex.zero? r[ndex] = 'Timestamps_' else col_nm_hsh[pg_nm_text].keys.each do |k| r[ndex] = col_nm_hsh[pg_nm_text][k] if r[ndex] == k end end end end next if header_found == false end break if r.compact.length.zero? # puts "#{__FILE__}:#{__LINE__} r = >#{r.inspect}<" if /(?<yr>\d{2}):Q(?<qtr>\d)/ =~ r[0].to_s yr = yr.to_i + 2000 yr -= 100 if yr > Date.today.year dte = Date.new(yr, qtr.to_i * 3 - 2, 1) r[0] = dte elsif (r[0].to_date rescue false) r[0] = r[0].to_date.beginning_of_quarter end next if r.map{|rr| (rr.nil? ? '' : rr.to_s).length }.sum.zero? rr = r.map{|c| c.nil? ? '' : c} rr = rr.reverse.drop_while(&:nil?).reverse next if rr.compact.length.zero? csv_txt << rr.join(',') end # End per-line analysis csv_dta = CSV.parse(csv_txt.join("\n"), headers: true, converters: [:numeric]) csv_dta.delete(nil) keys = csv_dta.headers.compact.intersection(['Timestamps_'] + col_nm_hsh[pg_nm_text].values) vals = csv_dta.to_a[1..-1].map{|r| (r + [''] * keys.length)[0..keys.length-1] }.transpose tmp_df = {}; (0..(keys.length-1)).to_a.each{|i| tmp_df[keys[i]] = vals[i]} tmp_df = Polars::DataFrame.new(tmp_df) tmp_df = tmp_df.filter(Polars.col('Timestamps_') <= fin.try(:to_date)) unless fin.nil? tmp_df = tmp_df.filter(Polars.col('Timestamps_') >= start.try(:to_date)) unless start.nil? tmp_df.sort!('Timestamps_') if df.nil? df = tmp_df.dup else df = df.join(tmp_df, on: 'Timestamps_', how: 'full') = df['Timestamps_'].to_a = df['Timestamps__right'].to_a .length.times.each {|ndex| [ndex] ||= [ndex] } df.replace('Timestamps_', Polars::Series.new()) df = df.drop(['Timestamps__right']) end df = df.sort('Timestamps_') end # End per-sheet analysis s = Polars::Series.new('Timestamps', df['Timestamps_'].to_a) df = df.insert_column(0,s).drop(['Timestamps_']) end df = df.drop(df.columns - ['Timestamps', @tag]) unless @tag.nil? || !df.columns.include?(@tag) df end |