{"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":249,"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":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L231-L267","documentation":"Raised by PeriodArray.__init__ when, after coercion, dtype is still None. This happens when raw integer ordinals are passed directly without a PeriodDtype — ordinals alone carry no frequency, so the period dtype cannot be inferred and the constructor refuses to guess.","triggerScenarios":"Calling PeriodArray(np.array([1,2,3], dtype='int64')) with no dtype. Passing a list/ndarray of ints that look like ordinals but no freq. Internal paths that bypass _from_sequence and reach __init__ with bare ordinals.","commonSituations":"Treating an int array as period ordinals without specifying freq. Migrating code that previously used an internal _simple_new without dtype. Loading ordinals from a parquet/feather file and forgetting to attach freq.","solutions":["Always supply a PeriodDtype: PeriodArray(ordinals, dtype=pd.PeriodDtype('D')).","Build via period_array(...) which infers dtype from Period/string values.","If constructing from ordinals, use PeriodArray._simple_new(ordinals, dtype=...) (internal)."],"exampleFix":"# before\npa = pd.arrays.PeriodArray(np.array([18262, 18263], dtype='int64'))\n# after\npa = pd.arrays.PeriodArray(np.array([18262, 18263], dtype='int64'), dtype=pd.PeriodDtype('D'))","handlingStrategy":"validation","validationCode":"import numpy as np\nimport pandas as pd\n\ndef period_from_ordinals(ordinals, freq='D'):\n    arr = np.asarray(ordinals, dtype='int64')\n    return pd.arrays.PeriodArray(arr, dtype=pd.PeriodDtype(freq))","typeGuard":"import pandas as pd\n\ndef has_freq(dtype) -> bool:\n    return dtype is not None and hasattr(dtype, 'freq')","tryCatchPattern":"try:\n    pa = pd.arrays.PeriodArray(values, dtype=dtype)\nexcept ValueError:\n    pa = pd.arrays.PeriodArray(values, dtype=pd.PeriodDtype('D'))","preventionTips":["Treat period ordinals as opaque; always pair them with a freq.","Persist freq metadata alongside ordinals when serializing.","Prefer pd.period_range / pd.period_array over raw PeriodArray(ordinals)."],"tags":["period","dtype","freq","constructor","pandas-arrays"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}