1. 원유를 조금이라도 분석해본 사람이면 EIA 데이터의 유효성을 안다.
많은 데이터를 API 통해 python으로 불러와보자.
2. EIA API 등록
여기로 가서
오른쪽을 클릭해서 등록. 이메일 주소를 입력하고 등록하면 이메일로 자신만의 API KEY가 온다.
API 개별 코드는 API Query Browse 에서 검색
3. WTI 일간 데이터를 불러와보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import pandas as pd
import eia
def retrieve_time_series(api, series_ID):
"""
Return the time series dataframe, based on API and unique Series ID
api: API that we're connected to
series_ID: string. Name of the series that we want to pull from the EIA API
"""
#Retrieve Data By Series ID
series_search = api.data_by_series(series=series_ID)
##Create a pandas dataframe from the retrieved time series
df = pd.DataFrame(series_search)
return df
"""
Execution in main block
"""
#Create EIA API using your specific API key
api_key = '이메일로 받은 개별 API KEY'
api = eia.API(api_key)
#Pull the oil WTI price data
series_ID='PET.RWTC.D'
price_df=retrieve_time_series(api, series_ID)
price_df
|
1
2
3
4
5
6
7
8
9
10
11
|
price_df.reset_index(level=0, inplace=True)
#Rename the columns for easier analysis
price_df.rename(columns={'index':'Date',
price_df.columns[1]:'WTI_Price'},
inplace=True)
#Format the 'Date' column
price_df['Date']=price_df['Date'].astype(str).str[:-3]
#Convert the Date column into a date object
price_df['Date']=pd.to_datetime(price_df['Date'], format='%Y %m%d')
#Subset to only include data going back to 2014
price_df=price_df[(price_df['Date']>='2014-01-01')]
|
셀을 분리하는 이유는 price_df 데이터프레임 형태를 확인해야 지저분한 날짜를 데이터로 바꿀 수 있기 때문.