Skip to content

Commit 3fe6082

Browse files
committed
Merge remote-tracking branch 'refs/remotes/mortada/master' into get_series_with_realtime
2 parents 45a1ac2 + fb61173 commit 3fe6082

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

fredapi/fred.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def __do_series_search(self, url):
337337
data = None
338338
return data, num_results_total
339339

340-
def __get_search_results(self, url, limit, order_by, sort_order):
340+
def __get_search_results(self, url, limit, order_by, sort_order, filter):
341341
"""
342342
helper function for getting search results up to specified limit on the number of results. The Fred HTTP API
343343
truncates to 1000 results per request, so this may issue multiple HTTP requests to obtain more available data.
@@ -352,6 +352,12 @@ def __get_search_results(self, url, limit, order_by, sort_order):
352352
else:
353353
raise ValueError('%s is not in the valid list of order_by options: %s' % (order_by, str(order_by_options)))
354354

355+
if filter is not None:
356+
if len(filter) == 2:
357+
url = url + '&filter_variable=%s&filter_value=%s' % (filter[0], filter[1])
358+
else:
359+
raise ValueError('Filter should be a 2 item tuple like (filter_variable, filter_value)')
360+
355361
sort_order_options = ['asc', 'desc']
356362
if sort_order is not None:
357363
if sort_order in sort_order_options:
@@ -375,7 +381,7 @@ def __get_search_results(self, url, limit, order_by, sort_order):
375381
data = data.append(next_data)
376382
return data.head(max_results_needed)
377383

378-
def search(self, text, limit=1000, order_by=None, sort_order=None):
384+
def search(self, text, limit=1000, order_by=None, sort_order=None, filter=None):
379385
"""
380386
Do a fulltext search for series in the Fred dataset. Returns information about matching series in a DataFrame.
381387
@@ -391,6 +397,9 @@ def search(self, text, limit=1000, order_by=None, sort_order=None):
391397
'popularity'
392398
sort_order : str, optional
393399
sort the results by ascending or descending order. Valid options are 'asc' or 'desc'
400+
filter : tuple, optional
401+
filters the results. Expects a tuple like (filter_variable, filter_value).
402+
Valid filter_variable values are 'frequency', 'units', and 'seasonal_adjustment'
394403
395404
Returns
396405
-------
@@ -399,10 +408,10 @@ def search(self, text, limit=1000, order_by=None, sort_order=None):
399408
"""
400409
url = "%s/series/search?search_text=%s&" % (self.root_url,
401410
quote_plus(text))
402-
info = self.__get_search_results(url, limit, order_by, sort_order)
411+
info = self.__get_search_results(url, limit, order_by, sort_order, filter)
403412
return info
404413

405-
def search_by_release(self, release_id, limit=0, order_by=None, sort_order=None):
414+
def search_by_release(self, release_id, limit=0, order_by=None, sort_order=None, filter=None):
406415
"""
407416
Search for series that belongs to a release id. Returns information about matching series in a DataFrame.
408417
@@ -418,19 +427,22 @@ def search_by_release(self, release_id, limit=0, order_by=None, sort_order=None)
418427
'popularity'
419428
sort_order : str, optional
420429
sort the results by ascending or descending order. Valid options are 'asc' or 'desc'
430+
filter : tuple, optional
431+
filters the results. Expects a tuple like (filter_variable, filter_value).
432+
Valid filter_variable values are 'frequency', 'units', and 'seasonal_adjustment'
421433
422434
Returns
423435
-------
424436
info : DataFrame
425437
a DataFrame containing information about the matching Fred series
426438
"""
427439
url = "%s/release/series?release_id=%d" % (self.root_url, release_id)
428-
info = self.__get_search_results(url, limit, order_by, sort_order)
440+
info = self.__get_search_results(url, limit, order_by, sort_order, filter)
429441
if info is None:
430442
raise ValueError('No series exists for release id: ' + str(release_id))
431443
return info
432444

433-
def search_by_category(self, category_id, limit=0, order_by=None, sort_order=None):
445+
def search_by_category(self, category_id, limit=0, order_by=None, sort_order=None, filter=None):
434446
"""
435447
Search for series that belongs to a category id. Returns information about matching series in a DataFrame.
436448
@@ -446,6 +458,9 @@ def search_by_category(self, category_id, limit=0, order_by=None, sort_order=Non
446458
'popularity'
447459
sort_order : str, optional
448460
sort the results by ascending or descending order. Valid options are 'asc' or 'desc'
461+
filter : tuple, optional
462+
filters the results. Expects a tuple like (filter_variable, filter_value).
463+
Valid filter_variable values are 'frequency', 'units', and 'seasonal_adjustment'
449464
450465
Returns
451466
-------
@@ -454,7 +469,7 @@ def search_by_category(self, category_id, limit=0, order_by=None, sort_order=Non
454469
"""
455470
url = "%s/category/series?category_id=%d&" % (self.root_url,
456471
category_id)
457-
info = self.__get_search_results(url, limit, order_by, sort_order)
472+
info = self.__get_search_results(url, limit, order_by, sort_order, filter)
458473
if info is None:
459474
raise ValueError('No series exists for category id: ' + str(category_id))
460475
return info

0 commit comments

Comments
 (0)