Processing Financial Data with FRED API: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[Financial_Data_Science#Processing_Financial_Data_with_FRED_API|Financial Data Science]]
* [[FRED API]]
* [[Pandas#Code_Examples|Pandas Code Examples]]


=Overview=
=Overview=
This article describes the sequence of steps required to process financial data obtained from FRED API.
This article describes the sequence of steps required to process financial data obtained from FRED API.
=Procedure=
Import the package and establish a connection to the FRED backend, providing the API Key obtained as described [[FRED_API#Get_the_API_Key|here]].
<syntaxhighlight lang='py'>
from fredapi import Fred
fred = Fred(api_key='...')
</syntaxhighlight>
Search for a dataset using full text search. The result is a [[Pandas_DataFrame|DataFrame]].
<syntaxhighlight lang='py'>
df = fred.search("S&P", limit=1000, order_by=None, sort_order=None, filter=None)
</syntaxhighlight>
The exploration yields a series with the ID "SP500".
Retrieve the [[Pandas_Series|Series]]:
<syntaxhighlight lang='py'>
s = fred.get_series(series_id="SP500")
</syntaxhighlight>
The series is already time-indexed:
<font size=-2>
2013-10-07    1676.12
2013-10-08    1655.45
2013-10-09    1656.40
2013-10-10    1692.56
2013-10-11    1703.20
              ... 
2023-10-02    4288.39
2023-10-03    4229.45
2023-10-04    4263.75
2023-10-05    4258.19
2023-10-06    4308.50
Length: 2610, dtype: float64
</font>
<syntaxhighlight lang='py'>
s.index
</syntaxhighlight>
<font size=-2>
DatetimeIndex(['2023-10-01', '2023-10-02', '2023-10-03', '2023-10-04',
              '2023-10-05', '2023-10-06', '2023-10-07', '2023-10-08',
              '2023-10-09'],
              dtype='datetime64[ns]', name='date', freq=None)
</font>

Latest revision as of 19:41, 8 October 2023

Internal

Overview

This article describes the sequence of steps required to process financial data obtained from FRED API.

Procedure

Import the package and establish a connection to the FRED backend, providing the API Key obtained as described here.

from fredapi import Fred
fred = Fred(api_key='...')

Search for a dataset using full text search. The result is a DataFrame.

df = fred.search("S&P", limit=1000, order_by=None, sort_order=None, filter=None)

The exploration yields a series with the ID "SP500".

Retrieve the Series:

s = fred.get_series(series_id="SP500")

The series is already time-indexed:

2013-10-07    1676.12
2013-10-08    1655.45
2013-10-09    1656.40
2013-10-10    1692.56
2013-10-11    1703.20
              ...   
2023-10-02    4288.39
2023-10-03    4229.45
2023-10-04    4263.75
2023-10-05    4258.19
2023-10-06    4308.50
Length: 2610, dtype: float64

s.index

DatetimeIndex(['2023-10-01', '2023-10-02', '2023-10-03', '2023-10-04',
              '2023-10-05', '2023-10-06', '2023-10-07', '2023-10-08',
              '2023-10-09'],
             dtype='datetime64[ns]', name='date', freq=None)