{"record":{"id":"a5be5bba2881968e","repo":"pandas-dev/pandas","slug":"specified-freq-and-dtype-are-different","errorCode":null,"errorMessage":"specified freq and dtype are different","messagePattern":"specified freq and dtype are different","errorType":"exception","errorClass":"IncompatibleFrequency","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":1452,"sourceCode":"    ----------\n    dtype : dtype\n    dtype2 : PeriodDtype or None\n        Dtype derived from a `freq` passed to the caller.\n\n    Returns\n    -------\n    PeriodDtype or None\n\n    Raises\n    ------\n    ValueError : non-period dtype\n    IncompatibleFrequency : mismatch between dtype and freq\n    \"\"\"\n    if dtype is not None:\n        if not isinstance(dtype, PeriodDtype):\n            raise ValueError(\"dtype must be PeriodDtype\")\n        if dtype2 is not None and dtype != dtype2:\n            raise IncompatibleFrequency(\"specified freq and dtype are different\")\n\n    elif dtype2 is not None:\n        if not isinstance(dtype2, PeriodDtype):\n            raise ValueError(\"dtype must be PeriodDtype\")\n        dtype = dtype2\n\n    return dtype\n\n\ndef dt64arr_to_periodarr(\n    data, freq, tz=None\n) -> tuple[npt.NDArray[np.int64], BaseOffset]:\n    \"\"\"\n    Convert a datetime-like array to values Period ordinals.\n\n    Parameters\n    ----------\n    data : Union[Series[datetime64[ns]], DatetimeIndex, ndarray[datetime64ns]]","sourceCodeStart":1434,"sourceCodeEnd":1470,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L1434-L1470","documentation":"Raised by validate_dtype_freq when both an explicit dtype and an inferred freq-derived dtype2 are supplied but they are not equal. The constructor refuses to silently pick one over the other; the caller must reconcile them.","triggerScenarios":"Calling a period constructor with freq='D' and dtype=PeriodDtype('M') simultaneously. APIs that accept both freq and dtype and let the user contradict themselves.","commonSituations":"Config files where freq and dtype come from different sources. Refactors that pass through both args without deduping. Copy-paste from examples with mismatched units.","solutions":["Drop one: pass either freq or dtype, not contradictory both.","Align them: dtype=PeriodDtype(freq) computed from the same freq value.","Validate equality before calling: if dtype != PeriodDtype(freq): raise."],"exampleFix":"# before\nvalidate_dtype_freq(pd.PeriodDtype('M'), pd.PeriodDtype('D'))\n# after\nfreq = 'D'\nvalidate_dtype_freq(pd.PeriodDtype(freq), pd.PeriodDtype(freq))\n# or just\nvalidate_dtype_freq(None, pd.PeriodDtype('D'))","handlingStrategy":"validation","validationCode":"from pandas.core.dtypes.dtypes import PeriodDtype\n\ndef reconcile(dtype, freq):\n    if dtype is not None and freq is not None and dtype != PeriodDtype(freq):\n        raise ValueError(f'dtype {dtype} conflicts with freq {freq}')\n    return dtype or (PeriodDtype(freq) if freq else None)","typeGuard":"from pandas.core.dtypes.dtypes import PeriodDtype\n\ndef dtype_freq_consistent(dtype, freq) -> bool:\n    return dtype is None or freq is None or dtype == PeriodDtype(freq)","tryCatchPattern":"from pandas.errors import IncompatibleFrequency\ntry:\n    dt = validate_dtype_freq(dtype, dtype2)\nexcept IncompatibleFrequency:\n    dt = validate_dtype_freq(None, dtype2)","preventionTips":["Pass only one of freq/dtype to period constructors.","Derive dtype from freq programmatically to avoid drift.","Assert consistency at config-load time."],"tags":["period","freq","dtype","validation","pandas-arrays"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}