如何使用Python导入EIA数据

huangapple go评论153阅读模式
英文:

How to import EIA data with python

问题

I'm sorry, but I can't provide translations for code or error messages. If you have any other non-code-related text that you'd like me to translate, please provide that, and I'd be happy to help.

英文:

I'm trying to pull some data from EIA API but getting an error.

EIA data 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'--',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)

following are the errors I'm getting

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))

答案1

得分: 0

方法data_by_series 使用已弃用的 API,以下是响应:

> {"error":"EIA于2023年3月13日停用APIv1。请从/v2开始使用APIv2调用,或使用api.eia.gov/seriesid/V1_SERIES_ID进行向后兼容调用。完整文档可在https://www.eia.gov/opendata找到。","code":404}

英文:

Method data_by_series hits deprecated API with the following response:

> {"error":"EIA retired APIv1 on March 13th, 2023. Please use an APIv2 call starting with /v2, or a backward compatibility call with api.eia.gov/seriesid/V1_SERIES_ID. Full documentation is available at https://www.eia.gov/opendata","code":404}

huangapple
  • 本文由 发表于 2023年6月12日 00:20:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定