mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-30 05:03:54 +00:00
fix(federal_reserve): Dallas TMOS and TSSOS read the renamed sheet (#7848)
* fix(federal_reserve): Dallas TMOS and TSSOS read the renamed sheet The Dallas Fed renamed the survey workbooks' sheet from Index to Sheet1. The sheet is picked by name from either, then the only sheet, as dallas_bcs already does. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * refactor(federal_reserve): one sheet-selection helper for the Dallas survey workbooks dallas_tmos, dallas_tssos and dallas_bcs each carried a copy of the same sheet-picking block. It is now survey_sheet() in utils/dallas_survey.py: the first published name present, else the only sheet, else an error that names the sheets found. dallas_bcs used to fall back to its old sheet name and fail inside pandas; it now raises that same error. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: dlee-bq <dlee@brightquery.com>
This commit is contained in:
@@ -82,25 +82,18 @@ class FederalReserveDallasBankingFetcher(
|
||||
**kwargs: Any,
|
||||
) -> list[FederalReserveDallasBankingData]:
|
||||
"""Decode every index, then pivot indicator-horizon to wide rows."""
|
||||
from io import BytesIO
|
||||
|
||||
from pandas import ExcelFile
|
||||
|
||||
from openbb_federal_reserve.utils.dallas_survey import (
|
||||
BANKING_INDICATORS,
|
||||
parse_keyed_survey,
|
||||
survey_sheet,
|
||||
)
|
||||
from openbb_federal_reserve.utils.workbook import pivot_wide
|
||||
|
||||
# The workbook's sheet was renamed upstream: old name, new name, then the only sheet.
|
||||
sheets = ExcelFile(BytesIO(data[0]["_raw"])).sheet_names
|
||||
sheet = next(
|
||||
(s for s in ("CSV_BCS_Index_Results", "BCS_Index_Results") if s in sheets),
|
||||
sheets[0] if len(sheets) == 1 else "CSV_BCS_Index_Results",
|
||||
)
|
||||
records = parse_keyed_survey(
|
||||
data[0]["_raw"],
|
||||
sheet,
|
||||
survey_sheet(
|
||||
data[0]["_raw"], ("CSV_BCS_Index_Results", "BCS_Index_Results")
|
||||
),
|
||||
BANKING_INDICATORS,
|
||||
start_date=query.start_date,
|
||||
end_date=query.end_date,
|
||||
|
||||
@@ -85,12 +85,13 @@ class FederalReserveDallasManufacturingFetcher(
|
||||
from openbb_federal_reserve.utils.dallas_survey import (
|
||||
MANUFACTURING_INDICATORS,
|
||||
parse_survey,
|
||||
survey_sheet,
|
||||
)
|
||||
from openbb_federal_reserve.utils.workbook import pivot_wide
|
||||
|
||||
records = parse_survey(
|
||||
data[0]["_raw"],
|
||||
"Index",
|
||||
survey_sheet(data[0]["_raw"], ("Index", "Sheet1")),
|
||||
MANUFACTURING_INDICATORS,
|
||||
date_format="%b-%y",
|
||||
start_date=query.start_date,
|
||||
|
||||
@@ -85,12 +85,13 @@ class FederalReserveDallasServiceSectorFetcher(
|
||||
from openbb_federal_reserve.utils.dallas_survey import (
|
||||
SERVICE_INDICATORS,
|
||||
parse_survey,
|
||||
survey_sheet,
|
||||
)
|
||||
from openbb_federal_reserve.utils.workbook import pivot_wide
|
||||
|
||||
records = parse_survey(
|
||||
data[0]["_raw"],
|
||||
"Index",
|
||||
survey_sheet(data[0]["_raw"], ("Index", "Sheet1")),
|
||||
SERVICE_INDICATORS,
|
||||
date_format="%b-%y",
|
||||
start_date=query.start_date,
|
||||
|
||||
@@ -256,6 +256,48 @@ def _read_dated(
|
||||
return frame
|
||||
|
||||
|
||||
def survey_sheet(content: bytes, names: tuple[str, ...]) -> str:
|
||||
"""Return the workbook's sheet among ``names``, else its only sheet.
|
||||
|
||||
The Dallas Fed has renamed the data sheet of its survey workbooks.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
content : bytes
|
||||
The workbook.
|
||||
names : tuple[str, ...]
|
||||
The names the sheet has been published under, in order of preference.
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
The name of the sheet to read.
|
||||
|
||||
Raises
|
||||
------
|
||||
OpenBBError
|
||||
When no name matches in a workbook of several sheets.
|
||||
"""
|
||||
from io import BytesIO
|
||||
|
||||
from openbb_core.app.model.abstract.error import OpenBBError
|
||||
from pandas import ExcelFile
|
||||
|
||||
sheets = ExcelFile(BytesIO(content)).sheet_names
|
||||
|
||||
for name in names:
|
||||
if name in sheets:
|
||||
return name
|
||||
|
||||
if len(sheets) == 1:
|
||||
return str(sheets[0])
|
||||
|
||||
raise OpenBBError(
|
||||
f"The Dallas Fed workbook has no sheet named {' or '.join(names)};"
|
||||
f" it has {', '.join(map(str, sheets))}."
|
||||
)
|
||||
|
||||
|
||||
def parse_survey(
|
||||
content: bytes,
|
||||
sheet: str,
|
||||
|
||||
@@ -762,3 +762,56 @@ class TestLeadingIndex:
|
||||
query = FederalReserveDallasLeadingIndexFetcher.transform_query({})
|
||||
with pytest.raises(EmptyDataError):
|
||||
FederalReserveDallasLeadingIndexFetcher.extract_data(query, None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fetcher, header",
|
||||
[
|
||||
(FederalReserveDallasManufacturingFetcher, ["date", "Prod", "Fprod"]),
|
||||
(FederalReserveDallasServiceSectorFetcher, ["date ", "rev", "frev"]),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("sheet", ["Index", "Sheet1"])
|
||||
def test_survey_sheet_named_index_or_sheet1(fetcher, header, sheet):
|
||||
"""The survey workbook is read whether its sheet is ``Index`` or ``Sheet1``."""
|
||||
raw = _workbook(sheet, [header, ["Jun-04", 38, 50]])
|
||||
rows = fetcher.transform_data(fetcher.transform_query({}), [{"_raw": raw}])
|
||||
assert [r.date for r in rows] == [date(2004, 6, 1)]
|
||||
|
||||
|
||||
class TestSurveySheet:
|
||||
"""Cover the sheet selection shared by the Dallas survey workbooks."""
|
||||
|
||||
@staticmethod
|
||||
def _sheets(*names: str) -> bytes:
|
||||
from openpyxl import Workbook
|
||||
|
||||
workbook = Workbook()
|
||||
workbook.active.title = names[0]
|
||||
for name in names[1:]:
|
||||
workbook.create_sheet(name)
|
||||
buffer = io.BytesIO()
|
||||
workbook.save(buffer)
|
||||
return buffer.getvalue()
|
||||
|
||||
def test_first_published_name_wins(self):
|
||||
"""A listed name is used, in order, whatever else the workbook holds."""
|
||||
from openbb_federal_reserve.utils.dallas_survey import survey_sheet
|
||||
|
||||
content = self._sheets("Notes", "Sheet1", "Index")
|
||||
assert survey_sheet(content, ("Index", "Sheet1")) == "Index"
|
||||
|
||||
def test_only_sheet_is_used_when_renamed_again(self):
|
||||
"""A workbook with one sheet is read whatever the sheet is called."""
|
||||
from openbb_federal_reserve.utils.dallas_survey import survey_sheet
|
||||
|
||||
assert survey_sheet(self._sheets("Data"), ("Index", "Sheet1")) == "Data"
|
||||
|
||||
def test_no_match_among_several_sheets_names_them(self):
|
||||
"""The error names the sheets it found."""
|
||||
from openbb_core.app.model.abstract.error import OpenBBError
|
||||
|
||||
from openbb_federal_reserve.utils.dallas_survey import survey_sheet
|
||||
|
||||
with pytest.raises(OpenBBError, match="Index or Sheet1; it has Notes, Data"):
|
||||
survey_sheet(self._sheets("Notes", "Data"), ("Index", "Sheet1"))
|
||||
|
||||
Reference in New Issue
Block a user