Skip to content

Commit 07989ca

Browse files
committed
feat: add simpler api call by series_id only
1 parent 41dd0f9 commit 07989ca

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ EIA_TOKEN=YOUR_TOKEN_HERE
4040
```
4141

4242
Lets look at an example of how to get the *EIA Natural Gas Futures*.
43+
You can use the simpler v1 API method where you only need to pass the `series_id` or you can use the newer v2 API method where you need to pass the `route`, `series`, and `frequency`.
4344

4445
```python
45-
df = eia.get_data(
46+
df = eia.get_series(series_id="NG.RNGC1.D")
47+
48+
df = eia.get_series_via_route(
4649
route="natural-gas/pri/fut",
4750
series="RNGC1",
4851
frequency="daily",
@@ -66,7 +69,9 @@ Date
6669
Lets look at another example the *Total OPEC Petroleum Supply* where the facet is available as `seriesId`. By Default it is set as `series` but we can define the facet as `seriesId`.
6770

6871
```python
69-
df = eia.get_data(
72+
df = eia.get_series(series_id="STEO.PAPR_OPEC.M")
73+
74+
df = eia.get_series_via_route(
7075
route="steo",
7176
series="PAPR_OPEC",
7277
frequency="monthly",

myeia/api.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,42 @@ class API:
2020
token: Optional[str] = os.getenv("EIA_TOKEN")
2121
url: str = "https://api.eia.gov/v2/"
2222

23-
def get_data(
23+
def get_series(
24+
self,
25+
series_id: str,
26+
new_name: str = "",
27+
) -> pd.DataFrame:
28+
"""
29+
Returns data for a given series in the simpler APIv1 format.
30+
31+
Args:
32+
series_id (str): The series ID. For example, "NG.RNGC1.W".
33+
Returns:
34+
pd.DataFrame: A DataFrame with the date and value columns.
35+
"""
36+
headers = {"Accept": "*/*"}
37+
url = f"{self.url}seriesid/{series_id}?api_key={self.token}"
38+
response = requests.get(url, headers=headers)
39+
response.raise_for_status()
40+
json_response = response.json()
41+
42+
base_df = pd.DataFrame(json_response["response"]["data"])
43+
44+
if "series-description" in base_df.columns:
45+
series_description = base_df["series-description"][0]
46+
else:
47+
series_description = series_id
48+
49+
if new_name != "":
50+
series_description = new_name
51+
52+
df = base_df[["period", "value"]]
53+
df.rename(columns={df.columns[0]: "Date", df.columns[1]: series_description}, inplace=True)
54+
df["Date"] = pd.to_datetime(df["Date"])
55+
df.set_index("Date", inplace=True)
56+
return df
57+
58+
def get_series_via_route(
2459
self,
2560
route: str,
2661
series: str,
@@ -29,19 +64,22 @@ def get_data(
2964
new_name: str = "",
3065
) -> pd.DataFrame:
3166
"""
32-
Returns data for a given series.
67+
Returns data for a given series in the newer APIv2 format.
68+
69+
Args:
70+
route (str): The route to the series. For example, "natural-gas/pri/fut".
71+
series (str): The series ID. For example, "RNGC1".
72+
frequency (str): The frequency of the series. For example, "daily".
73+
facet (str): The facet of the series. For example, "series", "seriesId".
74+
new_name (str): A name you want to give the series.
75+
Returns:
76+
pd.DataFrame: A DataFrame with the date and value columns.
3377
"""
34-
35-
headers = {
36-
"Accept": "*/*",
37-
}
78+
headers = {"Accept": "*/*"}
3879

3980
api_route = f"{route}/data/?api_key={self.token}&data[]=value&frequency={frequency}"
40-
4181
series = f"&facets[{facet}][]={series}"
42-
4382
sort = "&sort[0][column]=period&sort[0][direction]=desc"
44-
4583
url = f"{self.url}{api_route}{series}{sort}"
4684

4785
response = requests.get(url, headers=headers)

myeia/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.2"
1+
__version__ = "0.2.3"

tests/test_myeia.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33

44
from myeia.api import API
55

6+
eia = API()
67

7-
@pytest.mark.parametrize("route,series,frequency,facet", [("natural-gas/pri/fut", "RNGC1", "daily", "series"), ("petroleum/move/pipe", "MD0MP_R10-R20_1", "monthly", "series")])
8-
def test_get_data(route, series, frequency, facet):
9-
eia = API()
10-
df = eia.get_data(route, series, frequency, facet)
8+
9+
@pytest.mark.parametrize("series_id", ["NG.RNGC1.D", "PET.WCESTUS1.W", "PET.MD0MP_R10-R20_1.M", "INTL.29-12-HKG-BKWH.A"])
10+
def test_get_series(series_id):
11+
df = eia.get_series(series_id)
12+
assert isinstance(df, pd.DataFrame)
13+
14+
15+
@pytest.mark.parametrize(
16+
"route,series,frequency,facet",
17+
[("natural-gas/pri/fut", "RNGC1", "daily", "series"), ("petroleum/stoc/wstk", "WCESTUS1", "weekly", "series"), ("petroleum/move/pipe", "MD0MP_R10-R20_1", "monthly", "series")],
18+
)
19+
def test_get_series_via_route(route, series, frequency, facet):
20+
df = eia.get_series_via_route(route, series, frequency, facet)
1121
assert isinstance(df, pd.DataFrame)

0 commit comments

Comments
 (0)