Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: test_get_series & get_series #19

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions myeia/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import time
import warnings
from datetime import date
from typing import Optional, Union

Expand Down Expand Up @@ -39,6 +40,7 @@ def __init__(
max_tries=10,
raise_on_giveup=True,
jitter=backoff.full_jitter,
giveup=lambda e: e.response.status_code == 403,
)
def get_response(
self,
Expand All @@ -48,6 +50,9 @@ def get_response(
"""Helper function to get the response from the EIA API and return it as a dataframe."""
time.sleep(0.25)
response = requests.get(url, headers=headers)
if response.status_code == 403:
response.reason = "Forbidden! It's likely that the API key is invalid, not set or the request limit has been reached."

response.raise_for_status()
json_response = response.json()
return pd.DataFrame(json_response["response"]["data"])
Expand Down Expand Up @@ -100,10 +105,16 @@ def get_series(
df = df.sort_index(ascending=False)
df[data_identifier] = df[data_identifier].astype(float)

# Filtering the DataFrame by the specified date range can result in an empty DataFrame
if df.empty:
return df

for col in df.columns:
if "name" in col.lower() or "description" in col.lower():
df = df.rename(columns={data_identifier: df[col][0]})
df = df[df[col][0]].to_frame()
break

return df

def get_series_via_route(
Expand Down
Loading