我正在使用美国劳工统计局 (BLS) API 检索 CUUR0000SA0 系列的消费者价格指数 (CPI) 数据。我的目标是获取2020年4月至2024年4月的CPI数据。但是,API响应仅包含2022年1月开始的数据,我无法检索该日期之前的数据。
这是我用来发出 API 请求的代码:
import requests
import json
import pandas as pd
# Constants
CPI_URL = "https://api.bls.gov/publicAPI/v2/timeseries/data/"
API_KEY = "<<MY API KEY>>"
# Function to get CPI data from the API
def get_cpi_data(start_year, end_year):
headers = {"Content-Type": "application/json"}
data = json.dumps({
"seriesid": ["CUUR0000SA0"],
"startyear": str(start_year),
"endyear": str(end_year),
"registrationkey": API_KEY
})
response = requests.post(CPI_URL, headers=headers, data=data)
response_json = response.json()
# Print the API response for debugging purposes
print(json.dumps(response_json, indent=4))
return response_json
# Test the function with a range of years
start_year = 2020
end_year = 2024
try:
cpi_data = get_cpi_data(start_year, end_year)
except ValueError as e:
print(e)
API 响应包含以下数据(此处被截断以节省空间):
{
"status": "REQUEST_SUCCEEDED",
"responseTime": 155,
"message": [],
"Results": {
"series": [
{
"seriesID": "CUUR0000SA0",
"data": [
{
"year": "2024",
"period": "M04",
"periodName": "April",
"latest": "true",
"value": "313.548",
"footnotes": [
{}
]
},
...
{
"year": "2022",
"period": "M01",
"periodName": "January",
"value": "281.148",
"footnotes": [
{}
]
}
]
}
]
}
问题:
为什么我的 API 请求中没有返回 2022 年 1 月之前几个月(包括 2020 年 4 月)的数据? API 请求中是否需要任何特定限制或额外参数来检索所需数据?
一些来源
BLS API 信息在这里:https://www.bls.gov/developers/api_signature_v2.htm
该系列的文本文件可以追溯到几十年前(但在 2023 年 3 月之后不再提供):https://download.bls.gov/pub/time.series/cu/cu.data.1.AllItems
基于 BLS API 文档和 Twitter 的帮助 (https://x.com/ernietedeschi/status/1792358211271479510),我修改了方法以循环多年,以确保获得所需的所有数据点。这是有效的更新代码:
import requests
import json
import pandas as pd
# Constants
CPI_URL = "https://api.bls.gov/publicAPI/v2/timeseries/data/"
API_KEY = ""
# Function to get CPI data for a specific year
def get_cpi_data_for_year(year):
headers = {"Content-Type": "application/json"}
data = json.dumps({
"seriesid": ["CUUR0000SA0"],
"startyear": str(year),
"endyear": str(year),
"registrationkey": API_KEY
})
response = requests.post(CPI_URL, headers=headers, data=data)
response_json = response.json()
# Print the API response for debugging purposes
print(json.dumps(response_json, indent=4))
return response_json
# Function to get CPI data for a range of years
def get_cpi_data_range(start_year, end_year):
all_data = []
for year in range(start_year, end_year + 1):
response_json = get_cpi_data_for_year(year)
if response_json['status'] == 'REQUEST_SUCCEEDED':
series_data = response_json['Results']['series'][0]['data']
for item in series_data:
all_data.append({
"year": item['year'],
"period": item['period'],
"value": item['value']
})
# Convert to DataFrame
df = pd.DataFrame(all_data)
return df
# Get data for the range 2016 to 2024
start_year = 2016
end_year = 2024
try:
cpi_data_df = get_cpi_data_range(start_year, end_year)
print(cpi_data_df)
except ValueError as e:
print(e)
通过按年份拆分查询并合并结果,我成功检索了从 2016 年到 2024 年整个范围的数据。这种方法解决了在单个请求中检索多个年份的限制。