Skip to content

Commit

Permalink
refactor: error handling for older pandas versions / minor improvements
Browse files Browse the repository at this point in the history
bump version up to 0.2.5
  • Loading branch information
philsv committed May 2, 2023
1 parent 9ddc805 commit 1c99d35
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
56 changes: 30 additions & 26 deletions myeia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import pandas as pd
import requests
from dotenv import load_dotenv
from pandas.errors import SettingWithCopyWarning

try:
from pandas.errors import SettingWithCopyWarning
except ImportError as e:
raise ImportError("Please upgrade your version of pandas to 1.5.3 or higher.") from e

warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)

load_dotenv()


Expand All @@ -32,30 +37,23 @@ def get_series(
Args:
series_id (str): The series ID. For example, "NG.RNGC1.W".
new_name (str): A name you want to give the value column.
Returns:
pd.DataFrame: A DataFrame with the date and value columns.
"""
headers = {"Accept": "*/*"}
url = f"{self.url}seriesid/{series_id}?api_key={self.token}"
response = requests.get(url, headers=headers)
response.raise_for_status()
json_response = response.json()
base_df = self.get_json_response(url, headers)

base_df = pd.DataFrame(json_response["response"]["data"])

if "series-description" in base_df.columns:
series_description = base_df["series-description"][0]
if not new_name:
series_description = base_df["series-description"][0] if "series-description" in base_df.columns else series_id
else:
series_description = series_id

if new_name != "":
series_description = new_name

df = base_df[["period", "value"]]

df.rename(columns={df.columns[0]: "Date", df.columns[1]: series_description}, inplace=True)
df["Date"] = pd.to_datetime(df["Date"])
df.set_index("Date", inplace=True)
return df
return self.format_time_series_data(df)

def get_series_via_route(
self,
Expand All @@ -74,6 +72,7 @@ def get_series_via_route(
frequency (str): The frequency of the series. For example, "daily".
facet (str): The facet of the series. For example, "series", "seriesId".
new_name (str): A name you want to give the value column.
Returns:
pd.DataFrame: A DataFrame with the date and value columns.
"""
Expand All @@ -84,30 +83,35 @@ def get_series_via_route(
sort = "&sort[0][column]=period&sort[0][direction]=desc"
url = f"{self.url}{api_route}{series}{sort}"

response = requests.get(url, headers=headers)
response.raise_for_status()
json_response = response.json()

base_df = pd.DataFrame(json_response["response"]["data"])
base_df = self.get_json_response(url, headers)

if facet == "series":
df = base_df[["period", "value", "series-description", "series"]]

elif facet == "seriesId":
df = base_df[["period", "value", "seriesDescription", "seriesId"]]

df.reset_index(drop=True, inplace=True)

name = df[df.columns[2]][0]

if new_name != "":
name = new_name

name = new_name if new_name != "" else df[df.columns[2]][0]
df.rename(columns={df.columns[1]: name}, inplace=True)

df = df.iloc[:, :2]

df.rename(columns={df.columns[0]: "Date"}, inplace=True)
return self.format_time_series_data(df)

def format_time_series_data(self, df):
"""
Helper function to format time series data.
"""
df["Date"] = pd.to_datetime(df["Date"])
df.set_index("Date", inplace=True)
return df

def get_json_response(self, url: str, headers: dict):
"""
Helper function to get JSON response from API.
"""
response = requests.get(url, headers=headers)
response.raise_for_status()
json_response = response.json()
return pd.DataFrame(json_response["response"]["data"])
2 changes: 1 addition & 1 deletion myeia/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.4"
__version__ = "0.2.5"

0 comments on commit 1c99d35

Please sign in to comment.