"""Consolidated analysis-ready panel: one row per jurisdiction × fiscal year, headline indicators as columns with short names, values drawn only from questionnaire generations where the question is the same. The mapping below is the curated crosswalk; `panel_dictionary` publishes it.""" from __future__ import annotations from dataclasses import dataclass, field import pandas as pd G16, G18, G20 = "ISORA 2016", "ISORA 2018", "ISORA 2020+" GENERATIONS = (G16, G18, G20) PCT, RATIO, COUNT, LCU, PERSONS = "percent", "ratio", "count", "local currency units", "persons" @dataclass(frozen=True) class PanelColumn: name: str unit: str description: str codes: dict[str, str] = field(default_factory=dict) # generation -> indicator code topic: str = "" def _col(name, unit, description, topic, g16=None, g18=None, g20=None) -> PanelColumn: codes = {g: c for g, c in ((G16, g16), (G18, g18), (G20, g20)) if c} return PanelColumn(name, unit, description, codes, topic) PANEL_COLUMNS: tuple[PanelColumn, ...] = ( # --- Revenue and resources _col( "net_revenue_lcu", LCU, "Total net revenue collected by the tax administration (incl. SSC and non-tax revenue where collected)", "revenue", "80040_3", "80040_3", "80040_3", ), _col( "gdp_lcu", LCU, "Gross domestic product (as reported to ISORA)", "revenue", None, None, "398_001", ), _col( "government_revenue_lcu", LCU, "Total government revenue (as reported to ISORA)", "revenue", None, None, "398_005", ), _col( "population", PERSONS, "Total population (as reported to ISORA)", "revenue", None, None, "398_003", ), _col( "labor_force", PERSONS, "Labor force (as reported to ISORA)", "revenue", None, None, "398_004", ), _col( "revenue_to_gdp_pct", PCT, "Net revenue collected by the tax administration as % of GDP", "revenue", "10790", "337_001", "337_001", ), _col( "tax_incl_ssc_to_gdp_pct", PCT, "Tax collected including social security contributions as % of GDP", "revenue", None, "337_002", "337_002", ), _col( "tax_excl_ssc_to_gdp_pct", PCT, "Tax collected excluding social security contributions as % of GDP", "revenue", None, "337_003", "337_003", ), _col( "revenue_to_government_revenue_pct", PCT, "Net revenue collected as % of total government revenue", "revenue", None, None, "337_168", ), _col( "pit_share_of_revenue_pct", PCT, "Personal income tax as % of total revenue collected", "revenue", "80080_4", "337_005", "337_005", ), _col( "cit_share_of_revenue_pct", PCT, "Corporate income tax as % of total revenue collected", "revenue", "80090_4", "337_006", "337_006", ), _col( "vat_share_of_revenue_pct", PCT, "VAT as % of total revenue collected", "revenue", "80130_4", "337_007", "337_007", ), _col( "ssc_share_of_revenue_pct", PCT, "Social security contributions as % of total revenue collected", "revenue", "80240_4", "337_008", "337_008", ), _col( "other_taxes_share_of_revenue_pct", PCT, "Other taxes as % of total revenue collected", "revenue", None, "337_009", "337_009", ), _col( "nontax_share_of_revenue_pct", PCT, "Non-tax revenue as % of total revenue collected", "revenue", "80250_4", "337_004", "337_004", ), _col( "cost_of_collection_pct", PCT, "Recurrent (operating) cost of collection: operating expenditure as % of net revenue collected", "resources", None, "337_012", "337_012", ), _col( "operating_expenditure_lcu", LCU, "Operating (recurrent) expenditure of the tax administration", "resources", "89410", "91710_543", "337_176", ), _col( "salary_expenditure_lcu", LCU, "Salary expenditure of the tax administration", "resources", "89440", "91730_543", "337_177", ), _col( "ict_expenditure_lcu", LCU, "ICT operating expenditure of the tax administration", "resources", "83280_204", "91740_543", "337_178", ), _col( "capital_expenditure_lcu", LCU, "Capital expenditure of the tax administration", "resources", None, "91710_544", "337_179", ), _col( "salary_share_of_opex_pct", PCT, "Salary cost as % of operating (recurrent) expenditure", "resources", "10700", "337_013", "337_013", ), _col( "ict_share_of_opex_pct", PCT, "ICT operating cost as % of operating expenditure", "resources", "10710", "337_014", "337_014", ), _col( "capex_to_opex_pct", PCT, "Capital expenditure as % of operating expenditure", "resources", None, None, "337_015", ), _col( "total_fte", COUNT, "Total full-time equivalent staff of the tax administration", "staff", "83430_206", "83430_206", "337_180", ), _col("population_per_fte", RATIO, "Population per FTE", "staff", "10740", "337_010", "337_010"), _col( "labor_force_per_fte", RATIO, "Labor force per FTE", "staff", "10750", "337_011", "337_011" ), # --- Staff _col( "staff_audit_share_pct", PCT, "% of staff in audit, investigation and other verification", "staff", "83460_207", "94020_207", "337_017", ), _col( "staff_debt_collection_share_pct", PCT, "% of staff in enforced debt collection and related functions", "staff", "83470_207", "94030_207", "337_018", ), _col( "staff_hq_share_pct", PCT, "% of staff in headquarters", "staff", None, "337_022", "337_022" ), _col( "hiring_rate_pct", PCT, "Recruitments in FY as % of staff", "staff", "10020", "337_028", "337_028", ), _col( "attrition_rate_pct", PCT, "Departures in FY as % of staff", "staff", "10010", "337_029", "337_029", ), _col( "staff_female_pct", PCT, "% of staff who are female", "staff", "10200", "337_041", "337_041" ), _col( "executives_female_pct", PCT, "% of executives who are female", "staff", "10220", "337_042", "337_042", ), _col( "staff_bachelor_pct", PCT, "% of staff with a bachelor's degree (or equivalent)", "staff", "10080", "337_043", "337_043", ), _col( "staff_master_or_higher_pct", PCT, "% of staff with a master's degree or higher (or equivalent)", "staff", "10070", "337_044", "337_044", ), _col( "staff_under_35_pct", PCT, "% of staff younger than 35 (sum of <25 and 25-34 bands)", "staff", None, None, None, ), _col( "staff_55_or_older_pct", PCT, "% of staff aged 55 or older (sum of 55-64 and >64 bands)", "staff", None, None, None, ), # --- Segmentation, registration, filing _col( "lto_fte_share_pct", PCT, "FTEs in the large taxpayer office/program as % of total FTEs", "segmentation", "10760", "337_045", "337_045", ), _col( "lto_revenue_share_pct", PCT, "Net revenue administered by the large taxpayer office/program as % of total net revenue", "segmentation", "10550", None, "92280_28_1", ), _col( "lto_corporate_taxpayers_share_pct", PCT, "Corporate taxpayers managed by the LTO/program as % of active corporate taxpayers", "segmentation", "10240", "337_046", "337_046", ), _col( "active_pit_taxpayers_pct_labor_force", PCT, "Active PIT taxpayers as % of labor force", "registration", "10770", "337_059", "337_059", ), _col( "active_pit_taxpayers_pct_population", PCT, "Active PIT taxpayers as % of population", "registration", "10780", "337_058", "337_058", ), _col( "inactive_pit_register_pct", PCT, "Inactive taxpayers as % of PIT register", "registration", None, "337_053", "337_053", ), _col( "inactive_cit_register_pct", PCT, "Inactive taxpayers as % of CIT register", "registration", None, "337_054", "337_054", ), _col( "inactive_vat_register_pct", PCT, "Inactive taxpayers as % of VAT register", "registration", None, "337_055", "337_055", ), _col( "active_taxpayers_cit", COUNT, "Number of active CIT taxpayers", "registration", None, None, "95860_37", ), _col( "active_taxpayers_pit", COUNT, "Number of active PIT taxpayers", "registration", None, None, "95860_38", ), _col( "active_taxpayers_vat", COUNT, "Number of active VAT taxpayers", "registration", None, None, "95860_39", ), _col( "active_taxpayers_paye", COUNT, "Number of active PAYE (employer withholding) taxpayers", "registration", None, None, "95860_40", ), _col( "on_time_filing_cit_pct", PCT, "CIT returns filed on time as % of returns expected", "filing", "88140_37", "88140_37", "88140_37", ), _col( "on_time_filing_pit_pct", PCT, "PIT returns filed on time as % of returns expected", "filing", "88140_38", "88140_38", "88140_38", ), _col( "on_time_filing_vat_pct", PCT, "VAT returns filed on time as % of returns expected", "filing", None, "88140_39", "88140_39", ), _col( "on_time_filing_paye_pct", PCT, "PAYE returns filed on time as % of returns expected", "filing", "88140_40", "88140_40", "88140_40", ), _col( "efiling_cit_pct", PCT, "CIT returns filed electronically as % of returns received", "filing", None, None, "111_200", ), _col( "efiling_pit_pct", PCT, "PIT returns filed electronically as % of returns received", "filing", None, None, "111_201", ), _col( "efiling_vat_pct", PCT, "VAT returns filed electronically as % of returns received", "filing", None, None, "111_202", ), # --- Payment and arrears _col( "on_time_payment_cit_pct", PCT, "CIT payments received on time as % of payments due", "payment", "10430", "337_085", "337_085", ), _col( "on_time_payment_pit_pct", PCT, "PIT payments received on time as % of payments due", "payment", "10440", "337_084", "337_084", ), _col( "on_time_payment_vat_pct", PCT, "VAT payments received on time as % of payments due", "payment", "10460", "337_087", "337_087", ), _col( "on_time_payment_paye_pct", PCT, "PAYE payments received on time as % of payments due", "payment", "10450", "337_086", "337_086", ), _col( "epayment_by_number_pct", PCT, "Electronic payments as % of payments (by number)", "payment", None, "337_090", "337_090", ), _col( "epayment_by_value_pct", PCT, "Electronic payments as % of payments (by value)", "payment", None, "337_091", "337_091", ), _col( "arrears_to_revenue_pct", PCT, "Closing stock of arrears at year end as % of total revenue collected", "arrears", "10560", "337_092", "337_092", ), _col( "collectable_arrears_share_pct", PCT, "Collectable arrears as % of closing stock of arrears", "arrears", None, "337_093", "337_093", ), _col( "cit_arrears_pct_of_cit_collected", PCT, "CIT arrears as % of CIT collected", "arrears", None, "337_094", "337_094", ), _col( "pit_arrears_pct_of_pit_collected", PCT, "PIT arrears as % of PIT collected", "arrears", None, "337_095", "337_095", ), _col( "vat_arrears_pct_of_vat_collected", PCT, "VAT arrears as % of VAT collected", "arrears", None, "337_097", "337_097", ), _col( "arrears_growth_excl_noncollectable_pct", PCT, "Year-end arrears as % of previous year-end arrears (excluding non-collectable)", "arrears", None, None, "337_102", ), # --- Audit and disputes _col( "audit_assessments_to_collections_pct", PCT, "Additional assessments from all audits and verification actions as % of tax collections", "audit", None, None, "337_158", ), _col( "audit_hit_rate_pct", PCT, "Audits resulting in an adjustment as % of audits completed", "audit", None, None, "337_173", ), _col( "cit_assessments_pct_of_cit_collected", PCT, "CIT additional assessments as % of CIT collected", "audit", None, "337_121", "337_121", ), _col( "pit_assessments_pct_of_pit_collected", PCT, "PIT additional assessments as % of PIT collected", "audit", None, "337_122", "337_122", ), _col( "vat_assessments_pct_of_vat_collected", PCT, "VAT additional assessments as % of VAT collected", "audit", None, "337_124", "337_124", ), _col( "internal_review_cases_per_1000_taxpayers", RATIO, "Internal review (administrative review) cases initiated per 1 000 active PIT and CIT taxpayers", "disputes", "10650", "337_125", "337_125", ), _col( "independent_review_to_internal_review_ratio", RATIO, "Cases under independent review relative to internal review cases", "disputes", None, None, "337_126", ), _col( "appeals_won_by_administration_pct", PCT, "Cases resolved by higher appellate court in favour of the administration as % of cases resolved", "disputes", None, None, "337_127", ), ) # Columns computed as sums of published components (generation -> list of codes). COMPOSITE_COLUMNS: dict[str, dict[str, list[str]]] = { "staff_under_35_pct": { G16: ["10090", "10100"], G18: ["337_031", "337_032"], G20: ["337_031", "337_032"], }, "staff_55_or_older_pct": { G16: ["10130", "10140"], G18: ["337_035", "337_036"], G20: ["337_035", "337_036"], }, } # Ratios that cannot be zero for an operating tax administration. The ISORA 2016 derived # indicators in particular publish an exact 0 when one of the inputs was not reported. IMPLAUSIBLE_ZERO_COLUMNS: frozenset[str] = frozenset( {"revenue_to_gdp_pct", "population_per_fte", "labor_force_per_fte", "salary_share_of_opex_pct"} ) ATTRIBUTE_COLUMNS = [ "jurisdiction_code", "jurisdiction_name", "fiscal_year", "survey_round", "questionnaire_generation", "imf_region", "world_bank_region", "income_group_wb", "income_group_wb_classification_fy", "income_group_wb_current", "member_oecd", "member_eu", "member_iota", "member_ciat", "member_adb", ] def _values(obs: pd.DataFrame, generation: str, code: str, unit: str) -> pd.Series: sub = obs[ (obs["questionnaire_generation"] == generation) & (obs["indicator_code"] == code) & (obs["value_status"] == "value") ] col = "value_local_currency_units" if unit == LCU else "value_numeric" return sub.set_index(["jurisdiction_code", "fiscal_year"])[col].dropna() def build_panel( obs: pd.DataFrame, jurisdictions: pd.DataFrame, income_by_year: pd.Series ) -> tuple[pd.DataFrame, pd.DataFrame]: base = ( obs[ [ "jurisdiction_code", "jurisdiction_name", "fiscal_year", "survey_round", "questionnaire_generation", ] ] .drop_duplicates(["jurisdiction_code", "fiscal_year"]) .sort_values(["jurisdiction_code", "fiscal_year"]) .reset_index(drop=True) ) attrs = jurisdictions.set_index("jurisdiction_code") base["imf_region"] = base["jurisdiction_code"].map(attrs["imf_region"]) base["world_bank_region"] = base["jurisdiction_code"].map(attrs["world_bank_region_current"]) base["income_group_wb"] = income_by_year.reindex( pd.MultiIndex.from_frame(base[["jurisdiction_code", "fiscal_year"]]) ).to_numpy() base["income_group_wb_classification_fy"] = ["FY" + str(y + 2)[2:] for y in base["fiscal_year"]] base["income_group_wb_current"] = base["jurisdiction_code"].map( attrs["world_bank_income_group_current"] ) for m in ("member_oecd", "member_eu", "member_iota", "member_ciat", "member_adb"): base[m] = base["jurisdiction_code"].map(attrs[m]) base = base.set_index(["jurisdiction_code", "fiscal_year"]) dictionary_rows = [] labels = obs.drop_duplicates(["questionnaire_generation", "indicator_code"]).set_index( ["questionnaire_generation", "indicator_code"] )["indicator_label"] for column in PANEL_COLUMNS: series = [] sources = COMPOSITE_COLUMNS.get(column.name) for gen in GENERATIONS: if sources: codes = sources.get(gen, []) parts = [_values(obs, gen, c, column.unit) for c in codes] if not parts: continue combined = pd.concat(parts, axis=1).dropna().sum(axis=1) series.append(combined) code_txt = " + ".join(codes) label_txt = " + ".join(labels.get((gen, c), "") for c in codes) else: code = column.codes.get(gen) if not code: continue series.append(_values(obs, gen, code, column.unit)) code_txt, label_txt = code, labels.get((gen, code), "") dictionary_rows.append( { "column": column.name, "topic": column.topic, "unit": column.unit, "description": column.description, "questionnaire_generation": gen, "indicator_code": code_txt, "source_label": label_txt, "value_source": "value_local_currency_units" if column.unit == LCU else "value_numeric", "zeros_treated_as_missing": column.name in IMPLAUSIBLE_ZERO_COLUMNS, } ) if series: stacked = pd.concat(series) stacked = stacked[~stacked.index.duplicated(keep="last")] if column.name in IMPLAUSIBLE_ZERO_COLUMNS: stacked = stacked.where(stacked != 0) base[column.name] = stacked.reindex(base.index).to_numpy() else: base[column.name] = pd.Series(dtype="float64") panel = base.reset_index() counts = {c.name: int(panel[c.name].notna().sum()) for c in PANEL_COLUMNS} dictionary = pd.DataFrame(dictionary_rows) dictionary["n_non_null_in_panel"] = dictionary["column"].map(counts) return panel[ATTRIBUTE_COLUMNS + [c.name for c in PANEL_COLUMNS]], dictionary