{"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":235,"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":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/period.py#L217-L253","documentation":"Raised by PeriodArray.__init__ when values is an ABCSeries but its underlying _values is not itself a PeriodArray. PeriodArray accepts a Series only as a thin pass-through when that Series already wraps period data; any other Series (datetime, int, object) is rejected with this terse TypeError. The expectation is the caller convert the data first.","triggerScenarios":"PeriodArray(pd.Series(['2023-01', '2023-02'])) — object-dtype Series. PeriodArray(pd.Series([2023, 2024])) — int Series. PeriodArray(datetime_series) without conversion.","commonSituations":"Assuming PeriodArray will parse string periods from a Series; passing a column from a CSV (object dtype) directly; skipping pd.period_array / astype('period[...]').","solutions":["Convert the Series to period dtype first: s.astype('period[M]').","Use the factory pd.period_array(s, freq='M') or pd.PeriodIndex(s, freq='M').","Pass the raw values (list of Period scalars or int ordinals with explicit dtype) instead of the Series."],"exampleFix":"# before\npd.PeriodArray(pd.Series(['2023-01', '2023-02']))  # raises\n\n# after\npd.PeriodArray(pd.Series(['2023-01', '2023-02']).astype('period[M]'))","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef period_array_from_series(series, freq=None):\n    if not isinstance(series.dtype, pd.PeriodDtype):\n        series = series.astype(f'period[{freq}]')\n    return pd.PeriodArray(series)","typeGuard":"import pandas as pd\n\ndef is_period_series(series) -> bool:\n    return isinstance(series.dtype, pd.PeriodDtype)","tryCatchPattern":"try:\n    pd.PeriodArray(series)\nexcept TypeError as e:\n    if 'Incorrect dtype' in str(e):\n        pd.PeriodArray(series.astype('period[M]'))\n    else:\n        raise","preventionTips":["Convert Series to period dtype with .astype('period[...]') before passing to PeriodArray.","Prefer pd.period_array(series, freq=...) or pd.PeriodIndex(series, freq=...).","Check series.dtype is PeriodDtype in your data-prep layer."],"tags":["pandas","period","constructor","series","dtype"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}