1

I hope that I can clearly explain what I'm trying to do here. I'm able to retrieve the series date and values through the Bureau of Labor Statistics(BLS) API data. https://www.bls.gov/

I want to now get the 1-year percent change for each value. I'm grateful for any help on this. XXXXX is where I put my registration ID, so I can access more than three years of data.

Here's the developer's page: https://www.bls.gov/developers/api_signature_v2.htm#parameters

base_url = 'https://api.bls.gov/publicAPI/v2/timeseries/data/'
series = {'id': 'CUSR0000SA0',
          'name': 'Consumer Price Index - All Urban Consumers'}
data_url = '{}{}/?registrationkey=XXXXXXXXXXXXXXXXXX&startyear=2010&endyear=2022'.format(base_url, series['id'])

import requests
r = requests.get(data_url).json()
print('Status: ' + r['status'])

r = r['Results']['series'][0]['data']
print(r[0])

import pandas as pd 
##M13 is annual year, which I'm skipping since I only want months 1-12
dates = ['{}{}'.format(i['period'], i['year']) for i in r if i['period'] < 'M13']
index = pd.to_datetime(dates)
data = {series['id']: [float(i['value']) for i in r if i['period'] < 'M13']}

df = pd.DataFrame(index=index, data=data).iloc[::-1]

I'm not sure how to write up retrieving calculations in my code. I tried "calculations: {[i['calculations'][0] for i in r if i['period'] < 'M13']}" but that is not right.

Michelle
  • 25
  • 3
  • 1
    Your question needs a minimal reproducible example consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for best practices related to Pandas questions. Suggest, you provide a sample of the data returned from the site as a tables, csv, or other simple structure and then using this data explain what you are trying to get as a result. – itprorh66 Jan 02 '23 at 19:57

0 Answers0