def blsseries(keyid, api, start, end): df = bls.get_series(keyid, start, end, api) df.to_excel('bls.xlsx') df = pd.read_excel('bls.xlsx') df.columns = ['date', 'value'] return df
def makeLineChart(series, startYear, endYear, changeValue, yaxisAltName, title): BLSid = 'INSERT YOUR OWN PUBLIC DATA API ACCT NUMBER' df = bls.get_series(series, startYear, endYear, BLSid) df = df.reset_index() df.columns = ['Date', yaxisAltName] df['Date'] = df['Date'].astype(str) df[str(changeValue) + ' Month Change'] = df[yaxisAltName] - df[yaxisAltName].shift( changeValue) def createLineFigure(df): if changeValue == 0: fig = px.line(df, x='Date', y=yaxisAltName, title=title) else: chart_df = df[['Date', str(changeValue) + ' Month Change']].dropna() chart_df['Date'] = df['Date'].astype(str) fig = px.line(chart_df, x='Date', y=str(changeValue) + ' Month Change', title=title) fig.show() createLineFigure(df)
def test_monthly_value_multiple(): assert (bls.get_series(['LNS14000000', 'LNS14000001'], startyear=1948, endyear=1948).xs('1948-01').equals( pd.Series({ 'LNS14000000': 3.4, 'LNS14000001': 3.4 })))
def test_monthly_value_multiple(): assert ( bls.get_series( ['LNS14000000', 'LNS14000001'], startyear=1948, endyear=1948 ) .xs('1948-01') .equals( pd.Series({ 'LNS14000000': 3.4, 'LNS14000001': 3.4 }) ) )
def pull_unemployment(args, key, annual_avg=True): try: data = pd.DataFrame( bls.get_series(COOK_AREA_CODE, startyear=args.startyear, endyear=args.endyear, key=key)).reset_index() except Exception as e: print("API call failed") print(e) if annual_avg: data['year'] = data['date'].dt.year return data.groupby('year').mean().rename( columns={COOK_AREA_CODE: "Cook_U3_ann_avg"}) else: return data.rename(columns={COOK_AREA_CODE: "Cook_U3"})
def test_quarterly_value(): assert bls.get_series('CIU2020000000000A', startyear=2001, endyear=2001)['2001-Q1'] == 3.8
def test_monthly_value(): assert bls.get_series('LNS14000000', startyear=1948, endyear=1948)['1948-01'] == 3.4
def test_error_no_data(): with pytest.raises(ValueError): bls.get_series('LNS14000000', startyear=1900, endyear=1900)
def test_known_value(): series = bls.get_series('LNS14000000', startyear=1948) assert series.iloc[0].round(2) == 3.40
def test_key_end_twenty_years(): series = bls.get_series('LNS14000000', endyear=2018) years = series.index.year assert (years.min(), years.max()) == (1999, 2018)
def test_annual_value(): assert bls.get_series( 'TUU10100AA01000007', startyear=2009, endyear=2009 )['2009'] == 148720
def test_quarterly_value(): assert bls.get_series( 'CIU2020000000000A', startyear=2001, endyear=2001 )['2001-Q1'] == 3.8
def test_monthly_value(): assert bls.get_series( 'LNS14000000', startyear=1948, endyear=1948 )['1948-01'] == 3.4
def collect_data(self): load_env() if isfunction(self.provider): if self.code: self.data = self.provider(self.code) else: self.data = self.provider() elif self.provider == 'fred': fred = Fred(api_key=os.environ['TOKEN_FRED']) self.data = fred.get_series(self.code, observation_start=self.start_dt, observation_end=self.end_dt) elif self.provider == 'eod_hist': url = 'https://eodhistoricaldata.com/api/eod/{0}'.format(self.code) params = {'api_token': os.environ['TOKEN_EODHIST']} expire_after = td(days=1).total_seconds() session = requests_cache.CachedSession(cache_name='cache', backend='sqlite', expire_after=expire_after) r = session.get(url, params=params) if r.status_code != requests.codes.ok: session = requests.Session() r = session.get(url, params=params) if r.status_code == requests.codes.ok: df = pd.read_csv(StringIO(r.text), skipfooter=1, parse_dates=[0], index_col=0, engine='python') self.data = df['Close'] else: raise Exception(r.status_code, r.reason, url) elif self.provider == 'schiller': url = 'http://www.econ.yale.edu/~shiller/data/ie_data_with_TRCAPE.xls' webpage = requests.get(url, stream=True) self.data = pd.read_excel(io.BytesIO(webpage.content), 'Data', header=7, skipfooter=1) self.data.index = self.data['Date'].apply( lambda x: dt.strptime(str(x).format(x, '4.2f'), '%Y.%m')) self.data = self.data[self.code] print(self.data.tail(5)) elif self.provider == 'quandl': self.data = quandl.get(self.code, authtoken=os.environ['TOKEN_QUANDL'], collapse="quarterly", start_date=self.start_dt, end_date=self.end_dt)['Value'] elif self.provider == 'bls': self.data = bls.get_series( [self.code], startyear=dt.strptime(self.start_dt, '%Y-%m-%d').year, endyear=dt.strptime(self.end_dt, '%Y-%m-%d').year, key=os.environ['TOKEN_BLS']) elif self.provider == 'worldbank': self.data = wbdata.get_data( self.code, country='US', data_date=(dt.strptime(self.start_dt, '%Y-%m-%d'), dt.strptime(self.end_dt, '%Y-%m-%d')), convert_date=True, pandas=True, keep_levels=False) print(self.data.tail(5)) print("Collected data for [{0}]".format(self.code))
## retrieve our Bureau of Labor Statistics Public Data API key from OS environment value ## key comes from https://data.bls.gov/registrationEngine/ ## api may or may not work without our API key ## print(os.environ) ## debug - check if BRS_API_KEY is in environ try: os.environ["BLS_API_KEY"] except KeyError: msg = "Please set the environment variable BLS_API_KEY with our key from the DOL" print(msg) infoLogger.info(msg) sys.exit(1) BLS_API_KEY = os.environ.get('BLS_API_KEY') ## call the API from the BLS python package for the desired year - a year of data values cpi_information = bls.get_series('CUUR0000SA0', blsYear, blsYear, BLS_API_KEY) cpi_information.head() inf=pandas.DataFrame(cpi_information) msg = 'The raw data from the DOL API for the year parameters is:' print (msg) infoLogger.info(msg) print (inf) infoLogger.info(inf) ## filter only the CPI for the desired month try: var = inf.loc[str(blsYear)+'-'+str(blsMonth),:].item() except KeyError: msg = 'The month requested ' + str(blsMonth) + '-' + str(blsYear) + ' is not available yet, it may take a few more days, often after the 10th day.' print(msg)
def test_annual_value(): assert bls.get_series('TUU10100AA01000007', startyear=2009, endyear=2009)['2009'] == 148720
def test_key_till_thisyear(): series = bls.get_series('LNS14000000', startyear=1948) years = series.index.year assert (years.min(), years.max()) == (1948, datetime.date.today().year)
def test_error_no_key_too_many_years(nokey): with pytest.raises(ValueError): bls.get_series('LNS14000000', startyear=1948, endyear=2018)
def makeHorzBarChart(series1, series2, series3, series4, series5, series6, series7, series8, series9, series10, series11, series12, year, month, changeValue, title): BLSid = 'INSERT YOUR OWN PUBLIC DATA API ACCT NUMBER' #Creating DF if changeValue == 1: s1_1 = bls.get_series(series1, year, year, BLSid)[str(year) + '-' + str(month)] s1_2 = bls.get_series(series1, year, year, BLSid)[str(year) + '-' + str(month - 1)] s1 = s1_1 - s1_2 s2_1 = bls.get_series(series2, year, year, BLSid)[str(year) + '-' + str(month)] s2_2 = bls.get_series(series2, year, year, BLSid)[str(year) + '-' + str(month - 1)] s2 = s2_1 - s2_2 s3_1 = bls.get_series(series3, year, year, BLSid)[str(year) + '-' + str(month)] s3_2 = bls.get_series(series3, year, year, BLSid)[str(year) + '-' + str(month - 1)] s3 = s3_1 - s3_2 s4_1 = bls.get_series(series4, year, year, BLSid)[str(year) + '-' + str(month)] s4_2 = bls.get_series(series4, year, year, BLSid)[str(year) + '-' + str(month - 1)] s4 = s4_1 - s4_2 s5_1 = bls.get_series(series5, year, year, BLSid)[str(year) + '-' + str(month)] s5_2 = bls.get_series(series5, year, year, BLSid)[str(year) + '-' + str(month - 1)] s5 = s5_1 - s5_2 s6_1 = bls.get_series(series6, year, year, BLSid)[str(year) + '-' + str(month)] s6_2 = bls.get_series(series6, year, year, BLSid)[str(year) + '-' + str(month - 1)] s6 = s6_1 - s6_2 s7_1 = bls.get_series(series7, year, year, BLSid)[str(year) + '-' + str(month)] s7_2 = bls.get_series(series7, year, year, BLSid)[str(year) + '-' + str(month - 1)] s7 = s7_1 - s7_2 s8_1 = bls.get_series(series8, year, year, BLSid)[str(year) + '-' + str(month)] s8_2 = bls.get_series(series8, year, year, BLSid)[str(year) + '-' + str(month - 1)] s8 = s8_1 - s8_2 s9_1 = bls.get_series(series9, year, year, BLSid)[str(year) + '-' + str(month)] s9_2 = bls.get_series(series9, year, year, BLSid)[str(year) + '-' + str(month - 1)] s9 = s9_1 - s9_2 s10_1 = bls.get_series(series10, year, year, BLSid)[str(year) + '-' + str(month)] s10_2 = bls.get_series(series10, year, year, BLSid)[str(year) + '-' + str(month - 1)] s10 = s10_1 - s10_2 s11_1 = bls.get_series(series11, year, year, BLSid)[str(year) + '-' + str(month)] s11_2 = bls.get_series(series11, year, year, BLSid)[str(year) + '-' + str(month - 1)] s11 = s11_1 - s11_2 s12_1 = bls.get_series(series12, year, year, BLSid)[str(year) + '-' + str(month)] s12_2 = bls.get_series(series12, year, year, BLSid)[str(year) + '-' + str(month - 1)] s12 = s12_1 - s12_2 else: s1_1 = bls.get_series(series1, year, year, BLSid)[str(year) + '-' + str(month)] s1_2 = bls.get_series(series1, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s1 = s1_1 - s1_2 s2_1 = bls.get_series(series2, year, year, BLSid)[str(year) + '-' + str(month)] s2_2 = bls.get_series(series2, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s2 = s2_1 - s2_2 s3_1 = bls.get_series(series3, year, year, BLSid)[str(year) + '-' + str(month)] s3_2 = bls.get_series(series3, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s3 = s3_1 - s3_2 s4_1 = bls.get_series(series4, year, year, BLSid)[str(year) + '-' + str(month)] s4_2 = bls.get_series(series4, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s4 = s4_1 - s4_2 s5_1 = bls.get_series(series5, year, year, BLSid)[str(year) + '-' + str(month)] s5_2 = bls.get_series(series5, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s5 = s5_1 - s5_2 s6_1 = bls.get_series(series6, year, year, BLSid)[str(year) + '-' + str(month)] s6_2 = bls.get_series(series6, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s6 = s6_1 - s6_2 s7_1 = bls.get_series(series7, year, year, BLSid)[str(year) + '-' + str(month)] s7_2 = bls.get_series(series7, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s7 = s7_1 - s7_2 s8_1 = bls.get_series(series8, year, year, BLSid)[str(year) + '-' + str(month)] s8_2 = bls.get_series(series8, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s8 = s8_1 - s8_2 s9_1 = bls.get_series(series9, year, year, BLSid)[str(year) + '-' + str(month)] s9_2 = bls.get_series(series9, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s9 = s9_1 - s9_2 s10_1 = bls.get_series(series10, year, year, BLSid)[str(year) + '-' + str(month)] s10_2 = bls.get_series(series10, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s10 = s10_1 - s10_2 s11_1 = bls.get_series(series11, year, year, BLSid)[str(year) + '-' + str(month)] s11_2 = bls.get_series(series11, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s11 = s11_1 - s11_2 s12_1 = bls.get_series(series12, year, year, BLSid)[str(year) + '-' + str(month)] s12_2 = bls.get_series(series12, year - 1, year - 1, BLSid)[str(year - 1) + '-' + str(month)] s12 = s12_1 - s12_2 df = { 'Series': [ series1, series2, series3, series4, series5, series6, series7, series8, series9, series10, series11, series12 ], 'MoYChange': [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12] } fig = px.bar(df, x='MoYChange', y='Series', orientation='h', title=title) fig.show()