Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(journal): treat numpy int serials as Excel days in Tonghuashun

santhrealb11ccc7c8b85
diff --git a/agent/src/tools/trade_journal_parsers.py b/agent/src/tools/trade_journal_parsers.py
index 420bea103..1c77ddb52 100644
--- a/agent/src/tools/trade_journal_parsers.py
+++ b/agent/src/tools/trade_journal_parsers.py
@@ -240,7 +240,8 @@ def _ths_datetime(val: Any) -> str:
"""Normalize 成交时间; Excel serial floats become ISO datetime."""
if val is None or (isinstance(val, float) and pd.isna(val)):
return ""
- if isinstance(val, (int, float)) and not isinstance(val, bool):
+ # iterrows yields numpy integer/float scalars; bare pd.to_datetime(int) is ns-epoch.
+ if pd.api.types.is_number(val) and not isinstance(val, (bool,)):
ts = pd.to_datetime(float(val), unit="D", origin="1899-12-30", errors="coerce")
if pd.notna(ts):
return ts.strftime("%Y-%m-%d %H:%M:%S")
diff --git a/agent/tests/test_ths_excel_serial_dates.py b/agent/tests/test_ths_excel_serial_dates.py
index 5de6cddc5..59ab8e67f 100644
--- a/agent/tests/test_ths_excel_serial_dates.py
+++ b/agent/tests/test_ths_excel_serial_dates.py
@@ -26,6 +26,25 @@ def test_parse_tonghuashun_excel_serial_datetime() -> None:
assert rec[0].datetime == "2024-01-30 09:00:00"
+def test_parse_tonghuashun_excel_serial_int64_datetime() -> None:
+ """iterrows yields np.int64 for int64 columns; must not treat as ns-epoch."""
+ df = pd.DataFrame({
+ "成交时间": pd.Series([45321], dtype="int64"),
+ "证券代码": ["600519"],
+ "证券名称": ["茅台"],
+ "操作": ["买入"],
+ "成交数量": ["100"],
+ "成交价格": ["100"],
+ "成交金额": ["10000"],
+ "手续费": ["1"],
+ "印花税": ["0"],
+ "过户费": ["0"],
+ })
+ rec = parse_tonghuashun(df)
+ assert len(rec) == 1
+ assert rec[0].datetime == "2024-01-30 00:00:00"
+
+
def test_parse_tonghuashun_string_datetime_still_ok() -> None:
df = pd.DataFrame([{
"成交时间": "2024-01-01 10:00:00",