pandas 如何使用python导入EIA数据

pn9klfpd  于 2023-06-20  发布在  Python
关注(0)|答案(1)|浏览(130)

我试图从EIA API中提取一些数据,但出现错误。
EIA数据URL“https://www.eia.gov/dnav/pet/hist/rbrteD.htm”

pip install EIA_python

import pandas as pd
import eia

def retrieve_data():
    # Create EIA API using your specific API key
    api_key = "abcxyz"
    api = eia.API(api_key)

    # Retrieve Data By Series ID 
    series_ID='PET.RWTC.D'
    series_search = api.data_by_series(series=series_ID)
    df = pd.DataFrame(series_search)    
    df.index.names = ['Date']
    df.columns=[ "Price"]
    df.index = df.index.str.replace('^([\d]{4})\s([\d]{2})([\d]{2})\s[\d]{2}', r'\1-\2-\3',regex=True)
    df.index = pd.to_datetime(df.index)
    return df

data = retrieve_data()
data.to_csv('OK_WTI_Spot_Price_FOB.csv',index=True)

以下是我得到的错误

Cell In[5], line 1
----> 1 data = retrieve_data()
      2 data.to_csv('OK_WTI_Spot_Price_FOB.csv',index=True)
      3 data

Cell In[4], line 8, in retrieve_data()
      6 # Retrieve Data By Series ID 
      7 series_ID='PET.RWTC.D'
----> 8 series_search = api.data_by_series(series=series_ID)
      9 df = pd.DataFrame(series_search)    
     10 df.index.names = ['Date']

File ~\AppData\Local\anaconda3\lib\site-packages\eia\api.py:424, in API.data_by_series(self, series)
    420         raise InvalidSeries(error_msg)
    422 else:
    423     lst_dates = [x[0][0:4] + " " + x[0][4:] + " " + x[0][6:8]
--> 424                  for x in search.json()['series'][0]['data']]
    425     lst_values = [x[1] for x in
    426                   search.json()['series'][0]['data']]
    427     dates_values_dict = dict(zip(lst_dates, lst_values))
lmyy7pcs

lmyy7pcs1#

方法data_by_series命中已弃用的API,并返回以下响应:
{“error”:“EIA于2023年3月13日停用APIv 1。请使用以/v2开头的APIv 2调用,或使用www.example.com进行向后兼容性调用api.eia.gov/seriesid/V1_SERIES_ID。完整文档可在www.example.com上获得https://www.eia.gov/opendata“,“code”:404}

相关问题