{"record":{"id":"0970f1ef6ce98b3b","repo":"QuantConnect/Lean","slug":"symbols-symbol-id-symbol-ticker-e","errorCode":null,"errorMessage":"{symbols}, {symbol.id}, {symbol}, {ticker}. {e}","messagePattern":"(.+?), (.+?), (.+?), (.+?)\\. (.+?)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/PandasDataFrameHistoryAlgorithm.py","lineNumber":136,"sourceCode":"            self.assert_history_count(f\"df.xs(str({symbol}))\", df.xs(str(symbol)), expected)\n            self.assert_history_count(f\"df.at[(str({symbol}),), '{column}']\", list(df.at[(str(symbol),), column]), expected)\n            self.assert_history_count(f\"df2.loc[str({symbol})]\", df2.loc[str(symbol)], len(df2.columns))\n            self.assert_history_count(f\"df3[str({symbol})]\", df3[str(symbol)], expected)\n            self.assert_history_count(f\"df3.get(str({symbol}))\", df3.get(str(symbol)), expected)\n\n            # str : Symbol.VALUE\n            if len(ticker) == 0:\n                return\n            self.assert_history_count(f\"df.loc[{ticker}]\", df.loc[ticker], expected)\n            self.assert_history_count(f\"df.xs({ticker})\", df.xs(ticker), expected)\n            self.assert_history_count(f\"df.at[(ticker,), '{column}']\", list(df.at[(ticker,), column]), expected)\n            self.assert_history_count(f\"df2.loc[{ticker}]\", df2.loc[ticker], len(df2.columns))\n            self.assert_history_count(f\"df3[{ticker}]\", df3[ticker], expected)\n            self.assert_history_count(f\"df3.get({ticker})\", df3.get(ticker), expected)\n\n        except Exception as e:\n            symbols = set(df.index.get_level_values(level='symbol'))\n            raise AssertionError(f\"{symbols}, {symbol.id}, {symbol}, {ticker}. {e}\")\n\n\n    def assert_history_count(self, method_call, trade_bar_history, expected):\n        if isinstance(trade_bar_history, list):\n            count = len(trade_bar_history)\n        else:\n            count = len(trade_bar_history.index)\n        if count != expected:\n            raise AssertionError(f\"{method_call} expected {expected}, but received {count}\")\n\n\nclass QuandlFuture(PythonQuandl):\n    '''Custom quandl data type for setting customized value column name. Value column is used for the primary trading calculations and charting.'''\n    def __init__(self):\n        self.value_column_name = \"Settle\"\n","sourceCodeStart":118,"sourceCodeEnd":152,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/PandasDataFrameHistoryAlgorithm.py#L118-L152","documentation":"Catch-all in assert_history_index: the helper performs a series of indexing access styles (df.loc, df.xs, df.at, df2, df3) inside a try block to validate that Lean-created dataframes are indexable by ticker, Symbol, and SID string forms. If any of those access styles raises, the except wraps the original exception with the symbol set, the Symbol id/object, the ticker, and the original error for diagnosis.","triggerScenarios":"Any of the df.loc[ticker], df.xs(ticker), df.at[(ticker,), column], df2.loc[ticker], df3[ticker], or df3.get(ticker) calls inside the try raises (e.g. KeyError because the key form is not in the index, ValueError from a non-unique index, or a TypeError from wrong key shape). The wrapper re-raises with full context. This commonly fires when the PandasMapper key remapping (ticker<->SID) cannot resolve a key form.","commonSituations":"The dataframe index does not contain the expected key form (e.g. str(Symbol.ID) vs the Symbol object vs the ticker), often after a change to how PandasData builds the multi-index or after a pandas version bump that changed indexing semantics. Custom data whose Symbol/ticker does not match the index labels. A duplicate/non-unique index key causing xs to fail.","solutions":["Read the original error ({e}) in the wrapped message — it identifies which access style and which key form failed.","Inspect the dataframe index labels (df.index) to confirm which key form (ticker, str(Symbol.ID), or Symbol) is actually present.","If the PandasMapper cannot map the ticker to a SID, ensure SymbolCache has the symbol registered (e.g. it was added via AddEquity/AddData) before indexing.","For non-unique index errors, deduplicate or use a more specific key (include the time level)."],"exampleFix":"# before\nexcept Exception as e:\n    symbols = set(df.index.get_level_values(level='symbol'))\n    raise AssertionError(f\"{symbols}, {symbol.id}, {symbol}, {ticker}. {e}\")\n\n# after (also record the index key forms actually present, to localize the failure)\nexcept Exception as e:\n    symbols = set(df.index.get_level_values(level='symbol'))\n    sample_keys = list(df.index.get_level_values(0)[:5])\n    raise AssertionError(\n        f\"History indexing failed: symbols={symbols}, \"\n        f\"symbol={symbol}, ticker={ticker}, err={e}, sample_keys={sample_keys}\")","handlingStrategy":"try-catch","validationCode":"# Confirm the key form is present before the multi-style indexing\nlabels = set(map(str, df.index.get_level_values(0)))\nif str(symbol.id) not in labels and ticker not in labels:\n    self.debug(f\"Neither SID {symbol.id} nor ticker {ticker} in index {sorted(labels)}\")\n    return","typeGuard":"def key_resolvable(df, symbol, ticker):\n    \"\"\"True when the index contains the symbol's SID string or ticker form.\"\"\"\n    labels = set(map(str, df.index.get_level_values(0)))\n    return str(symbol.id) in labels or ticker in labels","tryCatchPattern":"try:\n    # the series of df.loc / df.xs / df.at access styles\n    ...\nexcept KeyError as e:\n    # Indexing key not found — report the index forms available rather than re-raising opaquely\n    labels = sorted(set(map(str, df.index.get_level_values(0))))\n    self.debug(f\"Key not found for {ticker}/{symbol.id}; index has {labels}: {e}\")","preventionTips":["Ensure the symbol was added (AddEquity/AddData) so SymbolCache can map ticker to SID.","Index by str(symbol.id) or the Symbol object you obtained from add_*, not a freehand ticker.","Check index labels before trying multiple access styles."],"tags":["quantconnect","lean","history","pandas","indexing","symbol-cache","python"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}