{"record":{"id":"0c5206c5eaf0a56a","repo":"HKUDS/Vibe-Trading","slug":"amount-must-be-a-finite-number-got-self-amount-r","errorCode":null,"errorMessage":"amount must be a finite number, got {self.amount!r}; a missing value must be fixed at the source, not carried as NaN","messagePattern":"amount must be a finite number, got (.+?); a missing value must be fixed at the source, not carried as NaN","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/entities/cashflow.py","lineNumber":163,"sourceCode":"        \"\"\"Normalize every field and enforce the sign convention.\n\n        Raises:\n            ValueError: If the date is unsupported, the amount is not finite,\n                the currency or kind is blank, or the amount's sign contradicts\n                a canonical kind in ``KIND_DIRECTION``.\n        \"\"\"\n        object.__setattr__(self, \"date\", normalize_date(self.date))\n        object.__setattr__(self, \"kind\", normalize_kind(self.kind))\n        object.__setattr__(self, \"currency\", normalize_currency(self.currency))\n\n        try:\n            amount = float(self.amount)\n        except (TypeError, ValueError) as exc:\n            raise ValueError(\n                f\"amount must be numeric, got {self.amount!r}\"\n            ) from exc\n        if not math.isfinite(amount):\n            raise ValueError(\n                f\"amount must be a finite number, got {self.amount!r}; a missing \"\n                \"value must be fixed at the source, not carried as NaN\"\n            )\n        object.__setattr__(self, \"amount\", amount)\n\n        required_sign = KIND_DIRECTION.get(self.kind)\n        if required_sign is not None and amount != 0.0:\n            if (amount > 0) != (required_sign > 0):\n                direction = \"positive (cash in)\" if required_sign > 0 else \"negative (cash out)\"\n                raise ValueError(\n                    f\"kind={self.kind!r} must have a {direction} amount under the \"\n                    f\"holder-perspective sign convention, got {amount!r}. Flip the \"\n                    \"sign, or use a distinct kind if this flow is genuinely \"\n                    \"two-directional (e.g. 'recallable_distribution').\"\n                )\n\n        if not isinstance(self.metadata, Mapping):\n            raise ValueError(","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/entities/cashflow.py#L145-L181","documentation":"After float() coercion succeeds, CashFlow rejects amounts that are NaN or the infinities, because financial aggregation would silently propagate garbage. The error message states the library's policy: missing values must be fixed at the source, never carried as NaN.","triggerScenarios":"CashFlow(amount=float('nan')), amount=float('inf'), or an amount parsed from the string 'NaN'/'inf' (float() accepts these).","commonSituations":"Pandas DataFrames with missing data converted via .to_dict('records') (NaN fills nulls); CSV cells containing 'NaN', 'n/a', or 'inf'; computations that divide by zero producing inf before entity creation.","solutions":["Filter or repair NaN/inf rows before constructing CashFlow (df = df.dropna(subset=['amount']) or replace with a corrected source value)","If missingness is legitimate (e.g. an unreported period), exclude that flow rather than encoding it as NaN","Add an ingest-time assert math.isfinite(x) to fail fast with row context"],"exampleFix":"# before\nrows = df.to_dict('records')\nflows = [CashFlow(**r) for r in rows]  # NaN amount -> ValueError\n# after\nimport math\nrows = df.to_dict('records')\nflows = [CashFlow(**r) for r in rows if math.isfinite(float(r['amount']))]","handlingStrategy":"validation","validationCode":"import math\nclean_rows = [r for r in rows if math.isfinite(float(r['amount']))]","typeGuard":"def is_finite_number(v) -> bool:\n    return isinstance(v, (int, float)) and math.isfinite(v)","tryCatchPattern":"try:\n    CashFlow(date=d, kind=k, amount=a, currency=c)\nexcept ValueError as e:\n    if 'finite number' in str(e):\n        handle_missing_value(row)  # drop, backfill from source, or alert","preventionTips":["dropna/subset on amount before entity creation","Never encode missing data as NaN — exclude the row or fix upstream","Assert finiteness in loader unit tests"],"tags":["python","cashflow","nan","validation"],"backgroundTag":"nan-in-numeric-data","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}