{"record":{"id":"ff6d7a3ca1969ba7","repo":"QuantConnect/Lean","slug":"method-call-expected-expected-but-received-c","errorCode":null,"errorMessage":"{method_call} expected {expected}, but received {count}","messagePattern":"(.+?) expected (.+?), but received (.+?)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/PandasDataFrameHistoryAlgorithm.py","lineNumber":145,"sourceCode":"            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":127,"sourceCodeEnd":152,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/PandasDataFrameHistoryAlgorithm.py#L127-L152","documentation":"Count-check helper assert_history_count that verifies a history slice contains exactly the expected number of elements. It computes count as len(list) for a list result or len(index) otherwise, then compares to expected. The assertion fires when the number of bars/rows returned for a given access method differs from the expected count.","triggerScenarios":"For a given access style (e.g. df.loc[str(symbol.id)]), the returned series/list has a count != expected. This happens when the history request returned fewer or more bars than anticipated — e.g. data missing for some dates, fill-forward changing row count, a different resolution producing a different bar count, or a date range that does not span the expected number of trading days.","commonSituations":"The expected count assumed a specific number of trading days but the data has gaps. Fill-forward (dataNormalization/fill-data-forward) added or removed rows. The backtest date range changed. A data source delivered partial data. The resolution requested (daily/hour/minute) yields a different count than the hard-coded expectation.","solutions":["Compare the actual data rows for that symbol against the expected window; fill-forward or data gaps are the usual cause.","Recompute 'expected' dynamically from the actual date range and resolution rather than hard-coding it.","Confirm the start/end dates and trading-day calendar match what the expectation assumed.","If data is legitimately partial, relax the assertion to a range or skip it."],"exampleFix":"# before\ndef 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# after (allow the count to be derived, and tolerate fill-forward variance)\ndef assert_history_count(self, method_call, trade_bar_history, expected, tolerance=0):\n    count = len(trade_bar_history) if isinstance(trade_bar_history, list) else len(trade_bar_history.index)\n    if abs(count - expected) > tolerance:\n        raise AssertionError(f\"{method_call} expected {expected} (+/-{tolerance}), but received {count}\")","handlingStrategy":"validation","validationCode":"# Compute expected from the actual range/resolution instead of hard-coding\ntrading_days = self.trading_calendar.get_trading_days(self.start_date, self.end_date)\nexpected = len(trading_days)\ncount = len(trade_bar_history) if isinstance(trade_bar_history, list) else len(trade_bar_history.index)\nif count != expected:\n    self.debug(f\"{method_call}: got {count}, expected {expected} (trading days)\")","typeGuard":"def count_matches(history, expected, tolerance=0):\n    \"\"\"True when the history element count is within tolerance of expected.\"\"\"\n    count = len(history) if isinstance(history, list) else len(history.index)\n    return abs(count - expected) <= tolerance","tryCatchPattern":null,"preventionTips":["Derive 'expected' from the date range and resolution rather than a hard-coded constant.","Account for fill-forward and trading-day calendar differences.","Allow a small tolerance for data gaps instead of an exact equality."],"tags":["quantconnect","lean","history","pandas","data-count","python"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}