Skip to content

Commit

Permalink
fix: facet bug / remove new_name
Browse files Browse the repository at this point in the history
  • Loading branch information
philsv committed Sep 8, 2023
1 parent 24d079b commit 970d6e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# myeia

[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.3.4&x2=0)](https://badge.fury.io/py/myeia)
[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.3.5&x2=0)](https://badge.fury.io/py/myeia)
[![License: MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://github.com/philsv/myeia/blob/main/LICENSE)
[![Weekly Downloads](https://static.pepy.tech/personalized-badge/myeia?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week)](https://pepy.tech/project/myeia)
[![Monthly Downloads](https://static.pepy.tech/personalized-badge/myeia?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month)](https://pepy.tech/project/myeia)
Expand Down Expand Up @@ -114,7 +114,7 @@ df = eia.get_series_via_route(
route="natural-gas/pri/fut",
series=["RNGC1", "RNGC1"],
frequency="daily",
facet="seriesId",
facet="series",
)

df.head()
Expand All @@ -140,8 +140,8 @@ You can define a start and end date for your query.
```python
df = eia.get_series(
series_id="NG.RNGC1.D",
start="2021-01-01",
end="2021-01-31",
start_date="2021-01-01",
end_date="2021-01-31",
)

df.head()
Expand All @@ -167,8 +167,8 @@ df = eia.get_series_via_route(
route="natural-gas/pri/fut",
series=["RNGC1", "RNGC2"],
frequency="daily",
start="2021-01-01",
end="2021-01-31",
start_date="2021-01-01",
end_date="2021-01-31",
)

df.head()
Expand Down
24 changes: 6 additions & 18 deletions myeia/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class API:
def get_series(
self,
series_id: str,
new_name: Union[str, None] = None,
start_date: Union[str, None] = None,
end_date: Union[str, None] = None,
) -> pd.DataFrame:
Expand All @@ -82,7 +81,6 @@ def get_series(
Args:
series_id (str): The series ID.
new_name (str, optional): A name you want to give the value column.
start_date (str, optional): The start date of the series.
end_date (str, optional): The end date of the series.
Expand All @@ -97,11 +95,7 @@ def get_series(

url = f"{self.base_url}{api_endpoint}"
base_df = get_json_response(url, headers=self.header)

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

df = base_df[["period", "value"]] # Keep only the date and value columns
df.rename(columns={df.columns[0]: "Date", df.columns[1]: series_description}, inplace=True)
Expand All @@ -117,7 +111,6 @@ def get_series_via_route(
series: Union[str, List[str]],
frequency: str,
facet: Union[str, None] = None,
new_name: Union[str, None] = None,
start_date: Union[str, None] = None,
end_date: Union[str, None] = None,
) -> pd.DataFrame:
Expand All @@ -129,7 +122,6 @@ def get_series_via_route(
series (str, list[str]): List of series ID's or a single series ID.
frequency (str): The frequency of the series.
facet (str, optional): The facet of the series. Defaults to "series".
new_name (str, optional): A name you want to give the value column.
start_date (str, optional): The start date of the series.
end_date (str, optional): The end date of the series.
Expand All @@ -138,12 +130,15 @@ def get_series_via_route(
Examples:
>>> eia = API()
>>> eia.get_series_via_route("natural-gas/pri/fut", "RNGC1", "daily", "series")
>>> eia.get_series_via_route("natural-gas/pri/fut", "RNGC1", "daily")
>>> eia.get_series_via_route("natural-gas/pri/fut", ["RNGC1", "RNGC2"], "daily", "series")
"""
start_date, end_date = get_date_range(start_date, end_date)

list_of_series = [series] if isinstance(series, str) else series

if not facet:
facet = "series"

data = []
sort_args = "&sort[0][column]=period&sort[0][direction]=desc"
Expand All @@ -157,20 +152,13 @@ def get_series_via_route(
if base_df.empty:
raise ValueError(f"Error getting data for series: {series}. Please check your request.")

if not facet:
facet = "series"

if not new_name:
new_name = ""

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 = new_name if new_name != "" else df[df.columns[2]][0]
df.rename(columns={df.columns[1]: name}, inplace=True)
df.rename(columns={df.columns[1]: df[df.columns[2]][0]}, inplace=True)
df = df.iloc[:, :2] # Keep only the date and value columns
df.rename(columns={df.columns[0]: "Date"}, inplace=True)
df = format_time_series_data(df)
Expand Down
2 changes: 1 addition & 1 deletion myeia/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.4"
__version__ = "0.3.5"

0 comments on commit 970d6e8

Please sign in to comment.