mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-30 05:03:54 +00:00
fix(famafrench): select the 'Not Reqd' tables when all_data_items_required=False (#7823)
get_international_portfolio filtered the frames and their metadata with different conditions when all_data_items_required is False. The frames kept the tables without "Not Reqd" in the description, which are the "Required" tables, and the metadata kept descriptions ending with "Not Reqd", which the ex-dividend files never do because their descriptions end with " - Ex-Dividends". So with dividends=True the call returned the "All 4 Data Items Required" returns under the "Not Reqd" description, and with dividends=False the metadata list was empty and transform_data raised IndexError. The documented example obb.famafrench.country_portfolio_returns(frequency="annual", measure="local", country="japan", dividends=False, all_data_items_required=False) fails with "[Unexpected Error] -> IndexError -> list index out of range". Filter both lists on "Not Reqd" in the description. Co-authored-by: dlee-bq <dlee@brightquery.com> Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
This commit is contained in:
@@ -754,15 +754,9 @@ def get_international_portfolio(
|
||||
|
||||
if all_data_items_required is False and measure != "ratios":
|
||||
dfs = [
|
||||
df
|
||||
for df, meta in zip(dfs, dfs_meta)
|
||||
if "Not Reqd" not in meta["description"]
|
||||
]
|
||||
dfs_meta = [
|
||||
meta
|
||||
for meta in dfs_meta
|
||||
if meta.get("description", "").endswith("Not Reqd")
|
||||
df for df, meta in zip(dfs, dfs_meta) if "Not Reqd" in meta["description"]
|
||||
]
|
||||
dfs_meta = [meta for meta in dfs_meta if "Not Reqd" in meta["description"]]
|
||||
elif all_data_items_required is True:
|
||||
dfs = [
|
||||
df for df, meta in zip(dfs, dfs_meta) if "Required" in meta["description"]
|
||||
|
||||
@@ -5,6 +5,7 @@ import io
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
from pandas import DataFrame
|
||||
|
||||
from openbb_famafrench.utils import helpers
|
||||
|
||||
@@ -646,6 +647,40 @@ def test_get_international_portfolio_invalid_measure(monkeypatch):
|
||||
helpers.get_international_portfolio(country="japan", measure="bogus")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("suffix", ["", " - Ex-Dividends"])
|
||||
def test_get_international_portfolio_all_data_items_not_required(monkeypatch, suffix):
|
||||
"""all_data_items_required=False returns the 'Not Reqd' table with its metadata."""
|
||||
tables = [
|
||||
("Value-Weight Local Returns All 4 Data Items Not Reqd", 1.0),
|
||||
("Value-Weight Local Returns All 4 Data Items Required", 2.0),
|
||||
]
|
||||
dataframes = [DataFrame({"mkt": [value]}) for _, value in tables]
|
||||
metadata = [
|
||||
{"frequency": "annual", "description": description + suffix}
|
||||
for description, _ in tables
|
||||
]
|
||||
monkeypatch.setattr(helpers, "get_international_portfolio_data", lambda *a, **k: "")
|
||||
monkeypatch.setattr(helpers, "read_dat_file", lambda data: [])
|
||||
monkeypatch.setattr(
|
||||
helpers,
|
||||
"process_international_portfolio_data",
|
||||
lambda *a, **k: (dataframes, metadata),
|
||||
)
|
||||
|
||||
dfs, dfs_meta = helpers.get_international_portfolio(
|
||||
country="japan",
|
||||
measure="local",
|
||||
frequency="annual",
|
||||
dividends=not suffix,
|
||||
all_data_items_required=False,
|
||||
)
|
||||
|
||||
assert [df["mkt"].iloc[0] for df in dfs] == [1.0]
|
||||
assert [meta["description"] for meta in dfs_meta] == [
|
||||
"Value-Weight Local Returns All 4 Data Items Not Reqd" + suffix
|
||||
]
|
||||
|
||||
|
||||
def test_get_international_portfolio_ratios_monthly(monkeypatch):
|
||||
"""The 'ratios' measure with monthly frequency raises ValueError."""
|
||||
monkeypatch.setattr(helpers, "get_international_portfolio_data", lambda *a, **k: "")
|
||||
|
||||
Reference in New Issue
Block a user