{"record":{"id":"99d17d98c0f951de","repo":"pandas-dev/pandas","slug":"inferred-frequency-inferred-from-passed-values-d","errorCode":null,"errorMessage":"Inferred frequency {inferred} from passed values does not conform to passed frequency {freq.freqstr}","messagePattern":"Inferred frequency (.+?) from passed values does not conform to passed frequency (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":1880,"sourceCode":"                start=index[0],\n                end=None,\n                periods=len(index),\n                freq=freq,\n                unit=index.unit,\n            )\n            if not lib.array_equivalent_bytes(index.asi8, on_freq.asi8):\n                raise ValueError\n        except ValueError as err:\n            if \"non-fixed\" in str(err):\n                # non-fixed frequencies are not meaningful for timedelta64;\n                #  we retain that error message\n                raise err\n            # GH#11587 the main way this is reached is if the `np.array_equal`\n            #  check above is False.  This can also be reached if index[0]\n            #  is `NaT`, in which case the call to `cls._generate_range` will\n            #  raise a ValueError, which we re-raise with a more targeted\n            #  message.\n            raise ValueError(\n                f\"Inferred frequency {inferred} from passed values \"\n                f\"does not conform to passed frequency {freq.freqstr}\"\n            ) from err\n\n    @classmethod\n    def _generate_range(\n        cls, start, end, periods: int | None, freq, *args, **kwargs\n    ) -> Self:\n        raise AbstractMethodError(cls)\n\n    # --------------------------------------------------------------\n\n    @cache_readonly\n    def _creso(self) -> int:\n        return get_unit_from_dtype(self._ndarray.dtype)\n\n    @cache_readonly\n    def unit(self) -> TimeUnit:","sourceCodeStart":1862,"sourceCodeEnd":1898,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimelike.py#L1862-L1898","documentation":"Raised by _validate_frequency when constructing a DatetimeIndex/TimedeltaIndex with an explicit freq= argument whose step does not match what pandas infers from the supplied values (GH#11587). The class generates a range at the declared freq from index[0] and compares asi8 byte arrays; on mismatch (or when index[0] is NaT) it re-raises this targeted message.","triggerScenarios":"pd.DatetimeIndex(values, freq='D') where values are not actually daily-spaced; pd.date_range validation paths that pass both data and a freq; round-tripping data that was filtered/missing rows but retained a stale freq attribute.","commonSituations":"Hand-editing or subsetting a DatetimeIndex without dropping its freq; concatenating indexes whose spacing differs; loading data with gaps but asserting a business-day or fixed frequency; timezone edge cases for non-Tick offsets.","solutions":["Omit freq= and let pandas infer it, then optionally call .asfreq(desired) to enforce.","Clean the source values so they actually match the desired grid before passing freq=.","If the data is meant to be irregular, drop the freq argument entirely.","Set freq=None on an existing index via idx = idx._with_freq(None) before reuse if you only need the values."],"exampleFix":"// before\nidx = pd.DatetimeIndex(['2020-01-01','2020-01-03'], freq='D')  # ValueError\n\n// after\nidx = pd.DatetimeIndex(['2020-01-01','2020-01-03'])  # inferred freq=None\n# or, to enforce daily:\nidx = pd.DatetimeIndex(['2020-01-01','2020-01-02']).asfreq('D')","handlingStrategy":"validation","validationCode":"def make_index(values, freq=None):\n    if freq is None:\n        return pd.DatetimeIndex(values)\n    inferred = pd.DatetimeIndex(values).inferred_freq\n    if inferred is not None and inferred != pd.tseries.frequencies.to_offset(freq).freqstr:\n        return pd.DatetimeIndex(values)  # let pandas infer; do not force freq\n    return pd.DatetimeIndex(values, freq=freq)","typeGuard":"def freq_matches(values, freq) -> bool:\n    inferred = pd.DatetimeIndex(values).inferred_freq\n    return inferred is not None and inferred == pd.tseries.frequencies.to_offset(freq).freqstr","tryCatchPattern":"try:\n    idx = pd.DatetimeIndex(values, freq=freq)\nexcept ValueError as e:\n    if \"does not conform to passed frequency\" in str(e):\n        idx = pd.DatetimeIndex(values)  # fall back to inferred/no freq\n    else:\n        raise","preventionTips":["After subsetting or filtering a DatetimeIndex, drop its freq: idx = idx._with_freq(None).","Prefer .asfreq(desired) over passing freq= at construction when enforcement is the goal.","Validate inferred_freq equals the intended offset before passing freq=."],"tags":["pandas","datetimeindex","frequency","validation","gh11587"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}