Complete Footnote extraction code (10-K, 10-Q)
What you added a few weeks ago for current period BS, IS, CF statements has been REALLY helpful. I'm going to show you a fragment of code below that in a fairly simple script builds a dataframe that can be sent to Excel and with a pivot on fy_idx, boom - you have 10 years plus latest quarter of comparable data analysis. Easy from there to build valuation models and so on.
However, for more granular analysis, footnotes data is invaluable. Very often there is a parent-child link from a primary statement element to a footnote which then further decomposes. I would like to be able to get that data but currently it's either beyond me or impossible. You already identify the footnotes, but they aren't populated.
My request: 1) add the same current period method to footnotes that you did for the primary statements. 2) add the parent concept to all rows in the dataframe so it's easier to tie them together. I know you have the data buried somewhere in the filing but it's really hard to work with. Just a parent concept tag on each row would simplify matters.
Now, as promised and maybe of help to others, here's what I'm doing with your current methods. This is the core routine - there's some housekeeping I'm not going to clutter this with:
def process_tickers(tickers: list[str]): """ Process each ticker through loading, validation, and writing. Args: tickers: Collection of ticker symbols to process.
Note: Assume filing below is a Filing class instance from edgartools """ configs = get_config()
# GAAP lookup: xbrl_tag -> std_tag
with open(configs.gaap_taxonomy_file, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
gaap_map = {row["xbrl_tag"]: row["std_tag"] for row in reader}
# Custom lookup: (cik, xbrl_tag) -> std_tag
with open(configs.custom_taxonomy_file, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
custom_map = {(row["cik"], row["xbrl_tag"]): row["std_tag"] for row in reader}
for ticker in tickers:
dfs_to_combine = []
filings_k, filing_q = get_edgar_data(ticker=ticker)
for yr_idx, filing in enumerate(chain(filings_k, [filing_q])):
xbrl_filing = filing.xbrl()
current = xbrl_filing.current_period
bs_curr_df = current.balance_sheet().get_dataframe()
is_curr_df_temp = current.income_statement().get_dataframe()
# this revenue section not used at the moment
#rev_rows = (
# is_curr_df_temp[is_curr_df_temp["concept"].str.endswith("_Revenues", na=False)]
# .to_dict(orient="records")
#)
#line_items = RevenueDeduplicator.deduplicate_statement_items(rev_rows)
# Simple filter to keep only 1 maximum revenue row (TBD may not work for every company)
is_concept = is_curr_df_temp["concept"].astype("string")
mask = is_concept.str.endswith("_Revenues", na=False)
vals = pd.to_numeric(is_curr_df_temp.loc[mask, "value"], errors="coerce")
if not vals.empty:
keep_mask = ~mask
keep_mask.loc[vals.idxmax()] = True
is_curr_df = is_curr_df_temp.loc[keep_mask]
else:
is_curr_df = is_curr_df_temp
is_curr_df = is_curr_df.drop(columns=["dimension_label", "is_dimension"], errors="ignore")
cf_curr_df = current.cashflow_statement().get_dataframe()
eq_curr_df = current.statement_of_equity().get_dataframe()
notes = current.notes() # just the headings, no data
notes = filing.xbrl().footnotes
# calc trees is a dict w keys 'http://company web site/role/name of statement (includes footnotes)
# this is a chore to work with
# calc_trees = xbrl_filing.calculation_trees # all the parent child links
is_curr_df = is_curr_df.assign(stmt="IS")
bs_curr_df = bs_curr_df.assign(stmt="BS")
cf_curr_df = cf_curr_df.assign(stmt="CF")
eq_curr_df = eq_curr_df.assign(stmt="EQ")
# --- Add filing metadata ----
fiscal_year = xbrl_filing.entity_info['fiscal_year']
fiscal_period = xbrl_filing.entity_info['fiscal_period']
period = xbrl_filing.entity_info['reporting_end_date']
entity_name = xbrl_filing.entity_info['entity_name']
cik = xbrl_filing.entity_info['identifier']
curr_df = (
pd.concat([is_curr_df, bs_curr_df, cf_curr_df, eq_curr_df], ignore_index=True)
.assign(
fy=fiscal_year,
fp=fiscal_period,
period=period,
fy_idx=yr_idx,
ticker=ticker,
cik=cik,
name=entity_name,
)
)
# ------- standard tag mapping --------
curr_df["concept"] = curr_df["concept"].astype("string")
curr_df[["version", "xbrl_tag"]] = curr_df["concept"].str.split("_", n=1, expand=True)
curr_df["version"] = curr_df["version"].astype("string")
curr_df["xbrl_tag"] = curr_df["xbrl_tag"].astype("string")
curr_df["cik"] = curr_df["cik"].astype("string")
curr_df["std_tag"] = pd.NA
mask_gaap = curr_df["version"].eq("us-gaap")
curr_df.loc[mask_gaap, "std_tag"] = (
curr_df.loc[mask_gaap, "xbrl_tag"].map(gaap_map)
)
mask_custom = curr_df["version"].eq("ticker")
custom_keys = list(zip(curr_df.loc[mask_custom, "cik"], curr_df.loc[mask_custom, "xbrl_tag"]))
curr_df.loc[mask_custom, "std_tag"] = (
pd.Series(custom_keys, index=curr_df.index[mask_custom]).map(custom_map)
)
curr_df["mapped"] = curr_df["std_tag"].notna()
# ---- Final assembly ----
dfs_to_combine.append(curr_df)
full_company_df = pd.concat(dfs_to_combine, ignore_index=True)
This is great use of the current_period property. I am starting to take interest into more complex workflows like that. It might not might actually fit inside the library but it's good that we can add the atomic functionality that can support this.
Thanks a lot