{"record":{"id":"537d6df941da430f","repo":"pandas-dev/pandas","slug":"could-not-infer-freq-from-start-end","errorCode":null,"errorMessage":"Could not infer freq from start/end","messagePattern":"Could not infer freq from start/end","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":1544,"sourceCode":"        start = Period(start, freq)\n    if end is not None:\n        end = Period(end, freq)\n\n    is_start_per = isinstance(start, Period)\n    is_end_per = isinstance(end, Period)\n\n    if is_start_per and is_end_per and start.freq != end.freq:\n        raise ValueError(\"start and end must have same freq\")\n    if start is NaT or end is NaT:\n        raise ValueError(\"start and end must not be NaT\")\n\n    if freq is None:\n        if is_start_per:\n            freq = start.freq\n        elif is_end_per:\n            freq = end.freq\n        else:  # pragma: no cover\n            raise ValueError(\"Could not infer freq from start/end\")\n        mult = freq.n\n\n    if periods is not None:\n        periods = periods * mult\n        if start is None:\n            data = np.arange(\n                end.ordinal - periods + mult, end.ordinal + 1, mult, dtype=np.int64\n            )\n        else:\n            data = np.arange(\n                start.ordinal, start.ordinal + periods, mult, dtype=np.int64\n            )\n    else:\n        data = np.arange(start.ordinal, end.ordinal + 1, mult, dtype=np.int64)\n\n    return data, freq\n\n","sourceCodeStart":1526,"sourceCodeEnd":1562,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L1526-L1562","documentation":"Defensive branch in _get_ordinal_range (marked pragma: no cover) that fires when freq is None and neither start nor end ended up as a Period object, so there is no frequency to inherit. In normal flow at least one bound becomes a Period via Period(start, freq); reaching this line means the inputs defeated that construction path. It is essentially an internal-invariant failure rather than an expected user error.","triggerScenarios":"Reaching this requires freq=None plus both start and end being non-Period after construction (e.g. subclassing/monkeypatching Period, or a future refactor). Practically unreachable through the public period_range API for well-formed inputs.","commonSituations":"Custom subclasses overriding Period behavior; using private _get_ordinal_range directly with exotic objects; version regressions where the Period coercion path changes.","solutions":["Always pass an explicit freq= to period_range / _get_ordinal_range so freq inference is not required.","If calling internals directly, ensure at least one bound is a real Period before invoking.","Report as a pandas bug if hit through the public API, since the branch is marked no-cover."],"exampleFix":"// before\npd.core.arrays.period._get_ordinal_range(start, end, periods, freq=None)\n// after\npd.core.arrays.period._get_ordinal_range(start, end, periods, freq='D')","handlingStrategy":"validation","validationCode":"import pandas as pd\nfrom pandas import Period\n\ndef safe_get_ordinal_range(start, end, periods, freq):\n    if freq is None and not isinstance(start, Period) and not isinstance(end, Period):\n        raise ValueError('Pass an explicit freq; could not infer from start/end')\n    return pd.core.arrays.period._get_ordinal_range(start, end, periods, freq)","typeGuard":"def has_period_endpoint(start, end) -> bool:\n    return isinstance(start, pd.Period) or isinstance(end, pd.Period)","tryCatchPattern":"try:\n    data, freq = _get_ordinal_range(start, end, periods, freq)\nexcept ValueError as e:\n    if 'Could not infer freq' in str(e):\n        freq = 'D'\n        data, freq = _get_ordinal_range(start, end, periods, freq)\n    else:\n        raise","preventionTips":["Never call _get_ordinal_range without an explicit freq.","Treat this branch as a bug indicator if reached via public API.","Ensure at least one bound is a Period when freq is None."],"tags":["pandas","period","period-range","internal","unreachable"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}