{"record":{"id":"73c400730efef40b","repo":"pandas-dev/pandas","slug":"incorrect-dtype","errorCode":null,"errorMessage":"Incorrect dtype","messagePattern":"Incorrect dtype","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":234,"sourceCode":"        _field_ops + _object_ops + _bool_ops + _deprecated_ops\n    )\n    _datetimelike_methods: list[str] = [\"strftime\", \"to_timestamp\", \"asfreq\"]\n\n    _dtype: PeriodDtype\n\n    # --------------------------------------------------------------------\n    # Constructors\n\n    def __init__(self, values, dtype: Dtype | None = None, copy: bool = False) -> None:\n        if dtype is not None:\n            dtype = pandas_dtype(dtype)\n            if not isinstance(dtype, PeriodDtype):\n                raise ValueError(f\"Invalid dtype {dtype} for PeriodArray\")\n\n        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","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L216-L252","documentation":"Raised by PeriodArray.__init__ when values is a pandas Series whose underlying _values is not itself a PeriodArray. The constructor will only adopt a Series if its values are already period-typed; otherwise the Series does not carry valid period ordinals and is rejected as incorrectly typed.","triggerScenarios":"Passing pd.arrays.PeriodArray(some_series) where some_series.dtype is not period[M]/period[D]/etc. Building a PeriodArray from a Series of strings or ints without going through _from_sequence.","commonSituations":"Users wrapping an arbitrary Series into PeriodArray directly. Series that came from .astype('object') or arithmetic that lost period dtype. Confusing PeriodArray(Series) with pd.Series.astype('period[...]').","solutions":["Convert the Series to period dtype first: s.astype('period[D]').","Use pd.period_array(s.tolist(), dtype=pd.PeriodDtype('D')) to build from arbitrary values.","Ensure the Series was created from a PeriodIndex or period-typed data."],"exampleFix":"# before\ns = pd.Series(['2020-01-01','2020-01-02'])\npa = pd.arrays.PeriodArray(s)\n# after\npa = s.astype('period[D]').array\n# or\npa = pd.period_array(s, dtype=pd.PeriodDtype('D'))","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef period_array_from_series(s: pd.Series, freq: str = 'D'):\n    if not isinstance(s._values, pd.arrays.PeriodArray):\n        s = s.astype(f'period[{freq}]')\n    return s.array","typeGuard":"import pandas as pd\n\ndef is_period_series(s: pd.Series) -> bool:\n    return isinstance(s.dtype, pd.PeriodDtype)","tryCatchPattern":"try:\n    pa = pd.arrays.PeriodArray(s)\nexcept TypeError:\n    pa = pd.period_array(s, dtype=pd.PeriodDtype('D'))","preventionTips":["Never pass a non-period Series to PeriodArray(); convert with .astype first.","Use pd.period_array(data, dtype=...) for arbitrary input.","Check s.dtype is PeriodDtype before constructing from a Series."],"tags":["period","series","dtype","constructor","pandas-arrays"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}