Stock market data infrastructure
Load financial statements into pandas
Turn the periods response into a pandas table while keeping currency, period type, cash-flow basis, and source attribution intact.
Documentation revised
Create a metadata-preserving table
Install httpx and pandas in your client environment. The example requests FY rows explicitly and reindexes display columns so a sparse response remains inspectable. The JSON response envelope is periods, not a bare list of statements.
import os
import httpx
import pandas as pd
response = httpx.get(
"https://stockmarketapi.ai/api/v1/companies/AAPL/financials",
params={"period_type": "FY", "limit": 5, "exchange": "NASDAQ"},
headers={"X-API-Key": os.environ["STOCKMARKET_API_KEY"]},
timeout=30,
)
response.raise_for_status()
payload = response.json()
frame = pd.DataFrame(payload["periods"])
if not frame.empty:
columns = ["period_end", "period_type", "currency", "revenue",
"primary_source", "cash_flow_basis"]
print(frame.reindex(columns=columns).to_string(index=False))
Keep missing values and source maps
Do not run fillna(0) on statement values. Preserve primary_source and the nested sources dictionary in your stored raw response even if the display table shows only selected columns. Check currency before joining companies, and keep fiscal_year with period_end.
Change frequency deliberately
For quarterly ingestion, inspect cash_flow_basis and request cash_flow=standalone when you need de-cumulated cash flows. For period=ttm, inspect ttm_quarters rather than assuming the aggregate represents four usable quarters. Do not mix FY and quarterly rows in an unqualified sum.