如何使用Python导入EIA数据

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

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"

  1. pip install EIA_python
  2. import pandas as pd
  3. import eia
  4. def retrieve_data():
  5. # Create EIA API using your specific API key
  6. api_key = "abcxyz"
  7. api = eia.API(api_key)
  8. # Retrieve Data By Series ID
  9. series_ID='PET.RWTC.D'
  10. series_search = api.data_by_series(series=series_ID)
  11. df = pd.DataFrame(series_search)
  12. df.index.names = ['Date']
  13. df.columns=[ "Price"]
  14. df.index = df.index.str.replace('^([\d]{4})\s([\d]{2})([\d]{2})\s[\d]{2}', r'--',regex=True)
  15. df.index = pd.to_datetime(df.index)
  16. return df
  17. data = retrieve_data()
  18. data.to_csv('OK_WTI_Spot_Price_FOB.csv',index=True)

following are the errors I'm getting

  1. Cell In[5], line 1
  2. ----> 1 data = retrieve_data()
  3. 2 data.to_csv('OK_WTI_Spot_Price_FOB.csv',index=True)
  4. 3 data
  5. Cell In[4], line 8, in retrieve_data()
  6. 6 # Retrieve Data By Series ID
  7. 7 series_ID='PET.RWTC.D'
  8. ----> 8 series_search = api.data_by_series(series=series_ID)
  9. 9 df = pd.DataFrame(series_search)
  10. 10 df.index.names = ['Date']
  11. File ~\AppData\Local\anaconda3\lib\site-packages\eia\api.py:424, in API.data_by_series(self, series)
  12. 420 raise InvalidSeries(error_msg)
  13. 422 else:
  14. 423 lst_dates = [x[0][0:4] + " " + x[0][4:] + " " + x[0][6:8]
  15. --> 424 for x in search.json()['series'][0]['data']]
  16. 425 lst_values = [x[1] for x in
  17. 426 search.json()['series'][0]['data']]
  18. 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:

确定