{"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":1547,"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":1529,"sourceCodeEnd":1565,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/period.py#L1529-L1565","documentation":"Raised (with pragma: no cover, so it is a defensive guard) when freq is None and neither start nor end is a Period object, so the frequency cannot be inferred. _get_ordinal_range needs at least one Period endpoint or an explicit freq.","triggerScenarios":"Calling period_range with start/end as plain strings or Timestamps and no freq= argument; mixing string endpoints with periods= but omitting freq.","commonSituations":"Assuming period_range will infer a freq from strings the way date_range infers from datetime data; refactor that dropped the freq kwarg; copy-paste from date_range usage.","solutions":["Always pass an explicit freq= to period_range unless one endpoint is already a Period.","Convert one endpoint to pd.Period(value, freq) so the freq is carried.","Use date_range instead if you do not actually need period semantics."],"exampleFix":"# before\npd.period_range('2020-01-01', '2020-01-10')\n# after\npd.period_range('2020-01-01', '2020-01-10', freq='D')","handlingStrategy":"validation","validationCode":"from pandas import Period\n\ndef can_infer_freq(start, end, freq) -> bool:\n    return freq is not None or isinstance(start, Period) or isinstance(end, Period)","typeGuard":"def has_period_endpoint_or_freq(start, end, freq) -> bool:\n    return freq is not None or isinstance(start, pd.Period) or isinstance(end, pd.Period)","tryCatchPattern":"try:\n    pr = pd.period_range(start, end, freq=freq)\nexcept ValueError as e:\n    if 'Could not infer freq' in str(e):\n        pr = pd.period_range(start, end, freq='D')  # explicit default\n    else:\n        raise","preventionTips":["Always pass an explicit freq= to period_range.","If you only have strings, prefer date_range + .to_period(freq).","Make one endpoint a pd.Period(value, freq) to carry freq implicitly."],"tags":["pandas","period","freq","range"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}