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

Add ability to ignore invalid ids #23

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
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
23 changes: 21 additions & 2 deletions bls/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import collections
import datetime
import logging
import warnings


import os
Expand Down Expand Up @@ -134,6 +135,7 @@ def get_series(
startyear: Optional[Union[str, int, float]] = None,
endyear: Optional[Union[str, int, float]] = None,
key: Optional[str] = None,
errors: str = "raise"
) -> pd.DataFrame:
"""
Retrieve one or more series from BLS. Note that only ten years may be
Expand All @@ -144,11 +146,28 @@ def get_series(
years before the endyear
:endyear: The last year for which to retrieve data. Defaults to ten years
after the startyear, if given, or else the current year
:errors: {"ignore", "raise"}, default "raise"
If "ignore", suppress error and only existing series are returned.
:returns: a pandas DataFrame object with each series as a column and each
monthly observation as a row. If only one series is requested, a pandas
Series object is returned instead of a DataFrame.
"""
results = get_json_series(series, startyear, endyear, key)
df = pd.DataFrame({result["seriesID"]: parse_series(result) for result in results})
# Parse out invalid seriesID(s)
invalid_ids = [res["seriesID"] for res in results if not res["data"]]
if errors == "raise" and invalid_ids:
raise ValueError(
"No data received for series {}! Are your parameters correct?"
.format(invalid_ids)
)
if invalid_ids:
warnings.warn(
"No data received for series {}! Are your parameters correct?"
.format(invalid_ids)
)
df = pd.DataFrame({
result["seriesID"]: parse_series(result)
for result in results if result["seriesID"] not in invalid_ids
})
df = df.applymap(float)
return df[series].sort_index()
return df[df.columns].sort_index()
22 changes: 19 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ def nokey():


def test_monthly_value():
assert bls.get_series("LNS14000000", startyear=1948, endyear=1948)["1948-01"] == 3.4
assert (
bls.get_series("LNS14000000", startyear=1948, endyear=1948)["1948-01"].iloc[0][
0
]
== 3.4
)


def test_monthly_value_multiple():
Expand All @@ -27,14 +32,18 @@ def test_monthly_value_multiple():

def test_quarterly_value():
assert (
bls.get_series("CIU2020000000000A", startyear=2001, endyear=2001)["2001-Q1"]
bls.get_series("CIU2020000000000A", startyear=2001, endyear=2001)[
"2001-Q1"
].iloc[0][0]
== 3.8
)


def test_annual_value():
assert (
bls.get_series("TUU10100AA01000007", startyear=2009, endyear=2009)["2009"]
bls.get_series("TUU10100AA01000007", startyear=2009, endyear=2009)["2009"].iloc[
0
][0]
== 148720
)

Expand Down Expand Up @@ -72,3 +81,10 @@ def test_error_no_key_too_many_years(nokey):
def test_error_no_data():
with pytest.raises(ValueError):
bls.get_series("LNS14000000", startyear=1900, endyear=1900)


def test_warning_and_not_valuerror_when_errors_set_to_ignore():
with pytest.warns(UserWarning):
bls.get_series(
["LNS14000000", "INVALID_SERIESID"], endyear=2018, errors="ignore"
)