FredAsDataframe
Up to date remote economic data access for ruby, using Polars dataframes.
This package will fetch economic and financial information from the Federal Reserve Economic Database, and return the results as a Polars Dataframe. You will need an API key that can be fetched from the FRED website at https://fredaccount.stlouisfed.org/apikeys .
Requirements
- Ruby >= 3.3
- polars-df ~> 0.27.1
Installation
Add this line to your application's Gemfile:
gem 'fred_as_dataframe'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install fred_as_dataframe
Configuration
Some data sources will require the specification of an API key. These keys should be provided as part of a configuration file, e.g., config/fred_as_dataframe.rb
FredAsDataframe::Client.configure do |config|
config.api_key = '1234567890ABCDEF'
end
Alternatively, you can read the API key from a file:
FredAsDataframe::Client.configure do |config|
config.api_key = File.read(File.join('', 'home', 'user', '.fred_api_key.txt')).strip
end
You can also pass the API key directly when creating a client instance:
client = FredAsDataframe::Client.new('UNRATE', api_key: '1234567890ABCDEF')
Usage
Consider the following transcript.
3.3.0 :001 > a = FredAsDataframe::Client.new('UNRATE')
=> #<FredAsDataframe::Client:0x0000000105f1dbb8 @api_key="1234567890ABCDEF", @tag="UNRATE">
3.3.0 :002 > b = a.fetch
=>
shape: (919, 2)
...
3.3.0 :003 > b
=>
shape: (919, 2)
┌────────────┬────────┐
│ Timestamps ┆ UNRATE │
│ --- ┆ --- │
│ date ┆ f64 │
╞════════════╪════════╡
│ 1948-01-01 ┆ 3.4 │
│ 1948-02-01 ┆ 3.8 │
│ 1948-03-01 ┆ 4.0 │
│ 1948-04-01 ┆ 3.9 │
│ 1948-05-01 ┆ 3.5 │
│ … ┆ … │
│ 2024-03-01 ┆ 3.8 │
│ 2024-04-01 ┆ 3.9 │
│ 2024-05-01 ┆ 4.0 │
│ 2024-06-01 ┆ 4.1 │
│ 2024-07-01 ┆ 4.3 │
└────────────┴────────┘
3.3.0 :004 > a = FredAsDataframe::Client.new('AAA')
=> #<FredAsDataframe::Client:0x0000000106077d38 @api_key="1234567890ABCDEF", @tag="AAA">
3.3.0 :005 > b = a.fetch
=>
shape: (1_267, 2)
...
3.3.0 :006 > b
=>
shape: (1_267, 2)
┌────────────┬──────┐
│ Timestamps ┆ AAA │
│ --- ┆ --- │
│ date ┆ f64 │
╞════════════╪══════╡
│ 1919-01-01 ┆ 5.35 │
│ 1919-02-01 ┆ 5.35 │
│ 1919-03-01 ┆ 5.39 │
│ 1919-04-01 ┆ 5.44 │
│ 1919-05-01 ┆ 5.39 │
│ … ┆ … │
│ 2024-03-01 ┆ 5.01 │
│ 2024-04-01 ┆ 5.28 │
│ 2024-05-01 ┆ 5.25 │
│ 2024-06-01 ┆ 5.13 │
│ 2024-07-01 ┆ 5.12 │
└────────────┴──────┘
Testing
This gem uses RSpec for testing. To run the test suite:
bundle install
bundle exec rake
Or run RSpec directly:
bundle exec rspec
The test suite uses VCR and WebMock to record and replay HTTP interactions with the FRED API, ensuring tests run quickly and don't require a live API connection or API key.
Running Tests with Coverage
Tests are configured with fixtures that mock FRED API responses, so you don't need a FRED API key to run the test suite.
API Documentation
Client#new(series_id, options = {})
Creates a new FRED data client for the specified series.
series_id- FRED series identifier (e.g., 'UNRATE', 'AAA', 'SP500')options- Optional hash with:api_keyto override configured API key
Client#fetch(start: nil, fin: nil)
Fetches data from FRED and returns a Polars DataFrame.
start- Optional start date (String or Date) to filter data from this date forwardfin- Optional end date (String or Date) to filter data up to this date
Returns a Polars DataFrame with timestamp and value columns.
Contributing
Others are welcome to contribute to the project.
The following conventions are intended for this project.
- Different sources are intended to reside in different classes.
- API keys (if needed) should be able to be set in the single configuration file.
- Series should be able to be identified via a single unique string, provided in the constructor.
- When fetched, the dataset may be filtered based on optional (hash) arguments.
- Output should be provided in a consistent DataFrame format (currently Polars::DataFrame).
Bug reports and pull requests are welcome on GitHub at https://github.com/bmck/fred_as_dataframe.
License
The gem is available as open source under the terms of the MIT License.