示例#1
0
 def test_fred_series_search(self):
     fred.key('123')
     fred.search('money stock')
     expected = 'http://api.stlouisfed.org/fred/series/search'
     params = {
         'api_key': '123',
         'search_text': 'money stock',
         'file_type': 'json'
     }
     self.get.assert_called_with(expected, params=params)
示例#2
0
def searchTitle(*args):

    if not args: searchKey = raw_input("Enter search key: ")
    else: searchKey = args[0]
    response = fred.search(searchKey)
    if response['seriess'] == []:
        print 'No results found for: ', searchKey
        return []
    index = 1
    l = ['']
    i = 0
    if not args: print '0) ALL'
    while i < len(response['seriess']) and index < 51:
        if not args: sys.stdout.write(str(index)), sys.stdout.write(') ')
        more = True  #necessary?
        if i < len(response['seriess']):
            try:
                l.append((response['seriess'][i]['title'],
                          response['seriess'][i]['id']))
                if args: break
                print response['seriess'][i]['title']
                i += 1
                index += 1
            except:
                i += 1
        if args: break

    return l
示例#3
0
def get_series_notes(series_term: str) -> pd.DataFrame:
    """Get Series notes. [Source: FRED]
    Parameters
    ----------
    series_term : str
        Search for this series term
    Returns
    ----------
    pd.DataFrame
        DataFrame of matched series
    """

    fred.key(cfg.API_FRED_KEY)
    d_series = fred.search(series_term)

    df_fred = pd.DataFrame()

    if "error_message" in d_series:
        if "api_key" in d_series["error_message"]:
            console.print("[red]Invalid API Key[/red]\n")
        else:
            console.print(d_series["error_message"])
    else:

        if "seriess" in d_series:
            if d_series["seriess"]:
                df_fred = pd.DataFrame(d_series["seriess"])
                df_fred["notes"] = df_fred["notes"].fillna(
                    "No description provided.")
            else:
                console.print("No matches found. \n")
        else:
            console.print("No matches found. \n")

    return df_fred
示例#4
0
def get_series_ids(series_term: str, num: int) -> Tuple[List[str], List[str]]:
    """Get Series IDs. [Source: FRED]
    Parameters
    ----------
    series_term : str
        Search for this series term
    num : int
        Maximum number of series IDs to output
    Returns
    ----------
    List[str]
        List of series IDs
    List[str]
        List of series Titles
    """
    fred.key(cfg.API_FRED_KEY)
    d_series = fred.search(series_term)

    if "seriess" not in d_series:
        return [], []

    if not d_series["seriess"]:
        return [], []

    df_series = pd.DataFrame(d_series["seriess"])
    df_series = df_series.sort_values(by=["popularity"],
                                      ascending=False).head(num)
    return df_series["id"].values, df_series["title"].values
示例#5
0
    def set_dropdown_options(input_value):
        ##Search FRED and gets the top 100 results
        search = {}
        if input_value != '':
            search = fred2.search(input_value)['seriess'][:100]
        #Get series IDs and information of top 100 results
        search_titles = [e['title'] for e in search]
        search_frequency = [e['frequency'] for e in search]
        search_units = [e['units'] for e in search]
        search_seasonal_adjustment = [e['seasonal_adjustment'] for e in search]
        search_IDs = [e['id'] for e in search]
        search_observation_start = [e['observation_start'] for e in search]
        search_observation_end = [e['observation_end'] for e in search]

        #Return list of options
        out_dict = [{
            'label':
            '{}, {}, {}, {}, {}, {} to {}'.format(title, frequency, units,
                                                  seasonal_adjustment, id,
                                                  observation_start,
                                                  observation_end),
            'value':
            id
        } for title, frequency, units, seasonal_adjustment, id,
                    observation_start, observation_end in zip(
                        search_titles, search_frequency, search_units,
                        search_seasonal_adjustment, search_IDs,
                        search_observation_start, search_observation_end)]

        return out_dict
示例#6
0
def search_fred(search_string, page=1):
    results_per_page = 15

    api_call_result = fred.search(search_string)
    total_pages = math.ceil(api_call_result['count'] / results_per_page)

    upper_result_bound = page * results_per_page - 1
    lower_result_bound = upper_result_bound - results_per_page + 1

    search_results = api_call_result['seriess']
    page_results = search_results[lower_result_bound:upper_result_bound]

    page_data = _unpack_search_data(page_results)

    metadata = {
        'data': page_data,
        'total_pages': total_pages,
        'current_page': page
    }

    return metadata
示例#7
0
def get_series_notes(series_term: str) -> pd.DataFrame:
    """Get Series notes. [Source: FRED]
    Parameters
    ----------
    series_term : str
        Search for this series term
    Returns
    ----------
    pd.DataFrame
        DataFrame of matched series
    """

    fred.key(cfg.API_FRED_KEY)
    d_series = fred.search(series_term)

    if "seriess" not in d_series:
        return pd.DataFrame()
    if not d_series["seriess"]:
        return pd.DataFrame()
    df_fred = pd.DataFrame(d_series["seriess"])
    df_fred["notes"] = df_fred["notes"].fillna("No description provided.")
    return df_fred
示例#8
0
def get_series_ids(series_term: str, num: int) -> Tuple[List[str], List[str]]:
    """Get Series IDs. [Source: FRED]
    Parameters
    ----------
    series_term : str
        Search for this series term
    num : int
        Maximum number of series IDs to output
    Returns
    ----------
    List[str]
        List of series IDs
    List[str]
        List of series Titles
    """
    fred.key(cfg.API_FRED_KEY)
    d_series = fred.search(series_term)

    # Cover invalid api and empty search terms
    if "error_message" in d_series:
        if "api_key" in d_series["error_message"]:
            console.print("[red]Invalid API Key[/red]\n")
        else:
            console.print(d_series["error_message"])
        return [], []

    if "seriess" not in d_series:
        return [], []

    if not d_series["seriess"]:
        return [], []

    df_series = pd.DataFrame(d_series["seriess"])
    df_series = df_series.sort_values(by=["popularity"],
                                      ascending=False).head(num)

    return df_series["id"].values, df_series["title"].values
示例#9
0
def get_series_notes(series_term: str, num: int) -> str:
    """Get Series notes. [Source: FRED]

    Parameters
    ----------
    series_term : str
        Search for this series term
    num : int
        Maximum number of series notes to display

    Returns
    ----------
    notes : str
        Series notes output
    """
    fred.key(cfg.API_FRED_KEY)
    d_series = fred.search(series_term)
    if "seriess" not in d_series:
        return "No Series found using this term!\n"

    df_fred = pd.DataFrame(d_series["seriess"])
    if df_fred.empty:
        return "No Series found using this term!\n"

    df_fred = df_fred.sort_values(by=["popularity"], ascending=False).head(num)

    notes = ""
    for _, series in df_fred.iterrows():
        if series["notes"]:
            notes += series["id"] + "\n"
            notes += "-" * len(series["id"]) + "\n"
            notes += series["notes"] + "\n\n"

    if not notes:
        return "Series notes not found!\n"

    return notes
示例#10
0
文件: test.py 项目: dmitri123/fred
 def test_fred_series_search(self):
     fred.key('123')
     fred.search('money stock')
     expected = 'https://api.stlouisfed.org/fred/series/search'
     params = {'api_key': '123', 'search_text': 'money stock', 'file_type': 'json'}
     self.get.assert_called_with(expected, params=params)