Skip to content
Python

fix(sec_edgar): serve a statement one row per period (#1517, #1542)

Yijia-Xiao2717033ad829
diff --git a/tests/test_sec_edgar.py b/tests/test_sec_edgar.py
index b72d33b4c..c5e59db9e 100644
--- a/tests/test_sec_edgar.py
+++ b/tests/test_sec_edgar.py
@@ -97,9 +97,10 @@ def test_the_quarter_is_not_confused_with_the_year_to_date():
@pytest.mark.unit
-def test_a_line_the_filer_does_not_tag_is_named_unavailable():
+def test_a_line_the_filer_does_not_tag_is_named_as_such():
out = sec_edgar.get_balance_sheet("AAPL", "annual", "2024-11-15")
- assert "Stockholders Equity" in out and "unavailable" in out
+ untagged = [line for line in out.splitlines() if line.startswith("# Not tagged by this filer:")]
+ assert len(untagged) == 1 and "Stockholders Equity" in untagged[0]
assert "364980" in out # the rest of the statement still returns
@@ -158,8 +159,7 @@ def test_a_throttle_lets_the_next_vendor_try(monkeypatch):
def test_values_do_not_break_the_columns():
"""Figures run to the billions; a thousands separator would split the field."""
out = sec_edgar.get_balance_sheet("AAPL", "annual", "2024-11-15")
- body = [row for row in out.splitlines() if row.startswith("Total Assets")][0]
- assert body.count(",") == out.splitlines()[3].count(",")
+ assert _table(out)[1]["Total Assets"][-1] == "364980"
@pytest.mark.unit
@@ -167,15 +167,12 @@ def test_a_per_share_figure_keeps_its_own_unit():
"""Statements are reported in millions, but EPS is dollars per share: scaling
it the same way prints a real figure as zero."""
out = sec_edgar.get_income_statement("AAPL", "annual", "2024-11-15")
- row = [r for r in out.splitlines() if r.startswith("Diluted EPS")][0]
- assert "6.08" in row
- assert "USD/shares" in row or "per share" in row
+ assert _table(out)[1]["Diluted EPS (USD/shares)"] == ["6.08"]
@pytest.mark.unit
-def test_every_row_has_one_cell_per_period():
- """An untagged line still has to line up with the columns, or the table is
- misread by position."""
+def test_every_row_has_one_cell_per_column():
+ """Every period's row lines up with the header, or the table is misread by position."""
out = sec_edgar.get_balance_sheet("AAPL", "annual", "2024-11-15")
table = [r for r in out.splitlines() if r and not r.startswith("#")]
widths = {row.count(",") for row in table}
@@ -237,11 +234,18 @@ def test_capital_expenditure_is_found_under_either_tag_filers_use(monkeypatch):
monkeypatch.setattr(sec_edgar, "_fetch_json",
lambda url: TICKER_MAP if "company_tickers" in url else facts)
out = sec_edgar.get_cashflow("AAPL", "annual", "2025-03-01")
- assert [r for r in out.splitlines() if r.startswith("Capital Expenditure")] == ["Capital Expenditure,70"]
+ assert _table(out)[1]["Capital Expenditure"] == ["70"]
+
+
+def _table(out):
+ """(periods, {column: cells by period}) of a statement, one row per period."""
+ lines = [line for line in out.splitlines() if line and not line.startswith("#")]
+ header, rows = lines[0].split(","), [line.split(",") for line in lines[1:]]
+ return [row[0] for row in rows], {name: [row[i] for row in rows] for i, name in enumerate(header) if i}
def _columns(out):
- return [line for line in out.splitlines() if line.startswith(",")][0].split(",")[1:]
+ return _table(out)[0]
@pytest.mark.unit
@@ -279,7 +283,7 @@ def test_a_recast_outside_the_annual_report_still_counts_from_its_filing(monkeyp
def eps(date):
out = sec_edgar.get_income_statement("AAPL", "annual", date)
- return [line for line in out.splitlines() if line.startswith("Diluted EPS")][0].split(",")[1]
+ return _table(out)[1]["Diluted EPS (USD/shares)"][0]
assert eps("2019-01-01") == "16.97"
assert eps("2020-01-01") == "2.12"
@@ -301,7 +305,7 @@ def test_a_cash_flow_filed_year_to_date_is_served_with_its_span(monkeypatch):
out = sec_edgar.get_cashflow("AAPL", "quarterly", "2026-09-28")
assert _columns(out) == ["2025-12-27", "2026-03-28 (6 months)", "2026-06-27 (9 months)"]
- assert [r for r in out.splitlines() if r.startswith("Operating Cash Flow")] == ["Operating Cash Flow,30000,53000,81000"]
+ assert _table(out)[1]["Operating Cash Flow"] == ["30000", "53000", "81000"]
@pytest.mark.unit
@@ -318,9 +322,28 @@ def test_a_row_filed_only_year_to_date_keeps_its_figure_beside_a_row_filed_by_qu
out = sec_edgar.get_cashflow("AAPL", "quarterly", "2026-09-28")
assert _columns(out) == ["2026-06-30", "2026-06-30 (6 months)"]
- rows = {r.split(",")[0]: r.split(",")[1:] for r in out.splitlines()[3:]}
- assert rows["Operating Cash Flow"] == ["", "120"]
- assert rows["Capital Expenditure"] == ["10", ""]
+ columns = _table(out)[1]
+ assert columns["Operating Cash Flow"] == ["", "120"]
+ assert columns["Capital Expenditure"] == ["10", ""]
+
+
+def test_a_statement_serves_every_period_one_row_each(monkeypatch):
+ """A filer's whole history in a row per line item ran to 67 columns and was read
+ a column off (#1517); a row per period keeps each figure beside its date."""
+ quarters = [
+ _fact(f"{year}-{month:02d}-30", (year * 10 + month // 3) * 1_000_000, f"{year}-{month + 1:02d}-28",
+ form="10-Q", fp=f"Q{month // 3}", start=f"{year}-{month - 2:02d}-01")
+ for year in range(2015, 2026) for month in (3, 6, 9)
+ ]
+ facts = {"facts": {"us-gaap": {"Revenues": {"units": {"USD": quarters}}}}}
+ monkeypatch.setattr(sec_edgar, "_fetch_json",
+ lambda url: TICKER_MAP if "company_tickers" in url else facts)
+
+ periods, columns = _table(sec_edgar.get_income_statement("AAPL", "quarterly", "2026-01-15"))
+
+ assert periods == [f"{y}-{m:02d}-30" for y in range(2015, 2026) for m in (3, 6, 9)]
+ assert columns["Revenue"][0] == "20151" and columns["Revenue"][-1] == "20253"
@pytest.mark.unit
@@ -331,5 +354,4 @@ def test_a_figure_under_a_million_is_not_printed_as_zero(monkeypatch):
monkeypatch.setattr(sec_edgar, "_fetch_json",
lambda url: TICKER_MAP if "company_tickers" in url else facts)
- out = sec_edgar.get_income_statement("AAPL", "annual", "2026-03-01")
- assert [r for r in out.splitlines() if r.startswith("Revenue,")] == ["Revenue,-0.0250,0.303"]
+ assert _table(sec_edgar.get_income_statement("AAPL", "annual", "2026-03-01"))[1]["Revenue"] == ["-0.0250", "0.303"]
diff --git a/tradingagents/dataflows/vendors/sec_edgar.py b/tradingagents/dataflows/vendors/sec_edgar.py
index 255d501ce..7ed95fd78 100644
--- a/tradingagents/dataflows/vendors/sec_edgar.py
+++ b/tradingagents/dataflows/vendors/sec_edgar.py
@@ -217,24 +217,25 @@ def _statement(kind: str, ticker: str, freq: str, as_of_date: str, title: str) -
periods = sorted({(end, index) for spans_of in chosen.values() for end, index in spans_of.items()})
if not periods:
raise NoMarketDataError(ticker, ticker, f"no {freq} {title.lower()} filed by {as_of_date}")
-
+ tagged = {label: line for label, line in lines.items() if line[0]}
+ untagged = [label for label in lines if label not in tagged]
header = (
- f"# {title} for {ticker.upper()} ({freq}), USD in millions unless the row says otherwise\n"
- f"# SEC EDGAR facts filed on or before {as_of_date}, at the values filed then\n\n"
+ f"# {title} for {ticker.upper()} ({freq}), USD in millions unless the column says otherwise\n"
+ f"# SEC EDGAR facts filed on or before {as_of_date}, at the values filed then\n"
+ + (f"# Not tagged by this filer: {', '.join(untagged)}\n" if untagged else "")
+ + "\n"
)
- rows = [",".join([""] + [end + names[index] for end, index in periods])]
- for label, (values, unit) in lines.items():
- # Every row spans the same columns, or a reader lines the table up wrong.
- if not values:
- rows.append(",".join([label] + ["unavailable (not tagged by this filer)"] * len(periods)))
- continue
- name = label if unit == "USD" else f"{label} ({unit})"
- # Plain numbers: a thousands separator would split the CSV field.
+ # One row per period, so every figure sits beside its own date: across a
+ # filer's whole history a row per line item ran to dozens of columns and was
+ # read a column off (#1517).
+ rows = [",".join(["Period"] + [label if unit == "USD" else f"{label} ({unit})"
+ for label, (_, unit) in tagged.items()])]
+ for end, index in periods:
cells = []
- for end, index in periods:
+ for label, (values, unit) in tagged.items():
value = values.get((end, index)) if chosen[label].get(end) == index else None
cells.append("" if value is None else _millions(value) if unit == "USD" else f"{value:.2f}")
- rows.append(",".join([name] + cells))
+ rows.append(",".join([end + names[index]] + cells))
return header + "\n".join(rows) + "\n"