{"record":{"id":"4e0f02b2dbfe1ecb","repo":"pandas-dev/pandas","slug":"dtype-is-not-specified-and-cannot-be-inferred","errorCode":null,"errorMessage":"dtype is not specified and cannot be inferred","messagePattern":"dtype is not specified and cannot be inferred","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":250,"sourceCode":"        if isinstance(values, ABCSeries):\n            values = values._values\n            if not isinstance(values, type(self)):\n                raise TypeError(\"Incorrect dtype\")\n\n        elif isinstance(values, ABCPeriodIndex):\n            values = values._values\n\n        if isinstance(values, type(self)):\n            if dtype is not None and dtype != values.dtype:\n                raise raise_on_incompatible(values, dtype.freq)\n            values, dtype = values._ndarray, values.dtype\n\n        if not copy:\n            values = np.asarray(values, dtype=\"int64\")\n        else:\n            values = np.array(values, dtype=\"int64\", copy=copy)\n        if dtype is None:\n            raise ValueError(\"dtype is not specified and cannot be inferred\")\n        dtype = cast(\"PeriodDtype\", dtype)\n        NDArrayBacked.__init__(self, values, dtype)\n\n    # error: Signature of \"_simple_new\" incompatible with supertype \"NDArrayBacked\"\n    @classmethod\n    def _simple_new(  # type: ignore[override]\n        cls,\n        values: npt.NDArray[np.int64],\n        dtype: PeriodDtype,\n    ) -> Self:\n        # alias for PeriodArray.__init__\n        assertion_msg = \"Should be numpy array of type i8\"\n        assert isinstance(values, np.ndarray) and values.dtype == \"i8\", assertion_msg\n        return cls(values, dtype=dtype)\n\n    @classmethod\n    def _from_sequence(\n        cls,","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/period.py#L232-L268","documentation":"Raised by PeriodArray.__init__ when, after all input-handling branches, dtype is still None. PeriodArray needs a frequency to interpret the int64 ordinals; without a PeriodDtype from either the dtype argument or an incoming PeriodArray/PeriodIndex, there is no way to know the freq, so construction fails. The values have already been coerced to int64, but the freq is missing.","triggerScenarios":"PeriodArray(np.array([202301, 202302], dtype='int64')) — raw ints, no dtype. PeriodArray([1, 2, 3]) with no freq. Internal code that strips dtype and forgets to reattach it.","commonSituations":"Loading ordinals from storage without persisting the freq; hand-building a PeriodArray from integers; the Series/PeriodIndex branches were skipped because the input was a plain ndarray.","solutions":["Pass an explicit dtype: PeriodArray(ordinals, dtype='period[M]').","Supply Period scalars instead of raw ints so the freq is inferred: PeriodArray([pd.Period('2023-01', freq='M'), ...]).","Build with pd.period_range or pd.PeriodIndex which always carry a freq."],"exampleFix":"# before\npd.PeriodArray(np.array([202301, 202302], dtype='int64'))  # raises\n\n# after\npd.PeriodArray(np.array([202301, 202302], dtype='int64'), dtype='period[M]')","handlingStrategy":"validation","validationCode":"import numpy as np\nimport pandas as pd\n\ndef period_array_from_ordinals(ordinals, freq):\n    if freq is None:\n        raise ValueError('freq must be provided when constructing from raw ordinals')\n    ordinals = np.asarray(ordinals, dtype='int64')\n    return pd.PeriodArray(ordinals, dtype=f'period[{freq}]')","typeGuard":"def has_period_freq(dtype_or_none) -> bool:\n    from pandas.core.dtypes.dtypes import PeriodDtype\n    return dtype_or_none is not None and isinstance(dtype_or_none, PeriodDtype)","tryCatchPattern":"try:\n    pd.PeriodArray(values)\nexcept ValueError as e:\n    if 'cannot be inferred' in str(e):\n        pd.PeriodArray(values, dtype='period[M]')  # supply freq\n    else:\n        raise","preventionTips":["Always pass an explicit dtype='period[<freq>]' when constructing from int ordinals.","Prefer pd.PeriodIndex / pd.period_range which always carry a freq.","Persist the freq alongside ordinals when storing period data to disk."],"tags":["pandas","period","constructor","freq","dtype"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}