Double-listing: layout-agnostic invariants, whole-dump sweep, bulk corrections
Yahoo changed the shape of its event feed between downloads: a 2026-08
re-download of CVIX/JLPSX shows it no longer returns capitalGain events at
all (the dividend stream still carries the year-end rows), while stale
tickers now return no events at all. File-specific remove ops therefore
break silently on the next re-download, so the double-listing fix is now
expressed as layout-agnostic invariants applied at bundle assembly
(idempotent, hold for full and incremental builds):
dedup: [[date, amount]] keep at most one copy of a same-date/
same-amount cross-file pair (the capitalGain copy when both present)
drop_capg_copy: [date] the capitalGain row on that date is the
spurious copy of the dividend row
- data.py: apply_invariants() at bundle assembly + pure dedupe_event_rows()
shared with verify_official and tests (8 new test cases, 22 passing)
- JLPSX/CVSIX corrections rewritten with the invariants (2019-08-08 now
keeps the dividend amount per the verified same-date pattern)
- scripts/scan_double_listing.py: whole-dump sweep -> reports/double_listing/
6,421 symbols scanned: 1,218 with same-amount pairs (6,589), 1,631 with
differing-amount pairs (12,366, reported only - not distinguishable from
legitimate same-day div+capg without per-fund official data), 83 with
repeated within-file rows (ingest keep-last already collapses them)
- scripts/apply_dedup_corrections.py: bulk 'dedup' corrections for the
1,218 same-amount symbols (1,212 new files; the 6 verified funds keep
their explicit, official-verified corrections)
- scripts/check_corrections.py: integrity check for every correction op
against the actual (frozen) files - caught a mis-filed CVSIX entry
This commit is contained in:
@@ -16,6 +16,7 @@ import time
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
|
||||
import data as D # noqa: E402
|
||||
import pandas as pd # noqa: E402
|
||||
|
||||
PASS, FAIL = 0, 0
|
||||
|
||||
@@ -136,6 +137,58 @@ def main() -> int:
|
||||
check("forced full rebuild", list(b.adj.columns) == ["aaa", "bbb", "ddd"])
|
||||
finally:
|
||||
shutil.rmtree(tmp, ignore_errors=True)
|
||||
|
||||
# --- cross-file correction invariants (Yahoo double-listing) ---
|
||||
tmp = pathlib.Path(tempfile.mkdtemp(prefix="fd-"))
|
||||
try:
|
||||
root = tmp / "data"; cache = tmp / "cache"; corrdir = tmp / "corr"
|
||||
root.mkdir(parents=True); cache.mkdir(); corrdir.mkdir()
|
||||
(root / "eee-dividend.csv").write_text(
|
||||
"Date,Dividends\n2020-01-02,1.5\n2020-12-15,4.0\n")
|
||||
(root / "eee-capitalGain.csv").write_text(
|
||||
"Date,Capital Gains\n2020-12-15,4.0\n2021-12-14,2.5\n")
|
||||
(root / "fff-dividend.csv").write_text(
|
||||
"Date,Dividends\n2020-12-15,1.0\n")
|
||||
(root / "fff-capitalGain.csv").write_text(
|
||||
"Date,Capital Gains\n2020-12-15,0.9\n")
|
||||
# eee: same-amount pair (dedup -> keep capg) ; fff: differing amounts
|
||||
# (drop_capg_copy -> keep the dividend row)
|
||||
(corrdir / "EEE.json").write_text(json.dumps({"dedup": [["2020-12-15", 4.0]]}))
|
||||
(corrdir / "FFF.json").write_text(json.dumps({"drop_capg_copy": ["2020-12-15"]}))
|
||||
real_corr = D.CORRECTIONS_DIR
|
||||
D.CORRECTIONS_DIR = corrdir
|
||||
try:
|
||||
# pure list-level function, both layouts
|
||||
d, c = D.dedupe_event_rows({"dedup": [["2020-12-15", 4.0]]},
|
||||
[("2020-12-15", 4.0)], [("2020-12-15", 4.0)])
|
||||
check("dedup: both copies -> keep capg", d == [] and c == [("2020-12-15", 4.0)])
|
||||
d, c = D.dedupe_event_rows({"dedup": [["2020-12-15", 4.0]]},
|
||||
[("2020-12-15", 4.0)], [])
|
||||
check("dedup: lone div copy (new layout) -> keep", d == [("2020-12-15", 4.0)] and c == [])
|
||||
d, c = D.dedupe_event_rows({"drop_capg_copy": ["2020-12-15"]},
|
||||
[("2020-12-15", 1.0)], [("2020-12-15", 0.9)])
|
||||
check("drop_capg_copy: old layout", d == [("2020-12-15", 1.0)] and c == [])
|
||||
d, c = D.dedupe_event_rows({"drop_capg_copy": ["2020-12-15"]},
|
||||
[("2020-12-15", 1.0)], [])
|
||||
check("drop_capg_copy: new layout (no capg) no-op", d == [("2020-12-15", 1.0)] and c == [])
|
||||
# bundle-level application
|
||||
b = D.load_bundle(root=root, cache=cache)
|
||||
ts = pd.Timestamp("2020-12-15")
|
||||
check("bundle: eee div copy dropped, capg kept",
|
||||
b.div.at[ts, "eee"] == 0.0 and b.capg.at[ts, "eee"] == 4.0)
|
||||
check("bundle: eee non-pair rows untouched",
|
||||
b.div["eee"].loc[pd.Timestamp("2020-01-02")] == 1.5
|
||||
and b.capg["eee"].loc[pd.Timestamp("2021-12-14")] == 2.5)
|
||||
check("bundle: fff capg copy dropped, div kept",
|
||||
b.div.at[ts, "fff"] == 1.0 and b.capg.at[ts, "fff"] == 0.0)
|
||||
# idempotent: reloading does not double-apply
|
||||
b2 = D.load_bundle(root=root, cache=cache)
|
||||
check("invariants idempotent on reload",
|
||||
b2.div["eee"][ts] == 0.0 and b2.capg["eee"][ts] == 4.0)
|
||||
finally:
|
||||
D.CORRECTIONS_DIR = real_corr
|
||||
finally:
|
||||
shutil.rmtree(tmp, ignore_errors=True)
|
||||
print(f"\n{PASS} passed, {FAIL} failed")
|
||||
return 1 if FAIL else 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user