{"record":{"id":"526ccbc654ded056","repo":"pandas-dev/pandas","slug":"wrong-dtype-data-dtype","errorCode":null,"errorMessage":"Wrong dtype: {data.dtype}","messagePattern":"Wrong dtype: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/period.py","lineNumber":1485,"sourceCode":"\n    Parameters\n    ----------\n    data : Union[Series[datetime64[ns]], DatetimeIndex, ndarray[datetime64ns]]\n    freq : Optional[Union[str, Tick]]\n        Must match the `freq` on the `data` if `data` is a DatetimeIndex\n        or Series.\n    tz : Optional[tzinfo]\n\n    Returns\n    -------\n    ordinals : ndarray[int64]\n    freq : Tick\n        The frequency extracted from the Series or DatetimeIndex if that's\n        used.\n\n    \"\"\"\n    if not isinstance(data.dtype, np.dtype) or data.dtype.kind != \"M\":\n        raise ValueError(f\"Wrong dtype: {data.dtype}\")\n\n    if freq is None:\n        if isinstance(data, ABCIndex):\n            data, freq = data._values, data.freq\n        elif isinstance(data, ABCSeries):\n            # freq is always None for DatetimeArray inside a Series, so we\n            #  fall back to the inferred freq.\n            inferred_freq = data._values._inferred_freq_str\n            if inferred_freq is not None:\n                warnings.warn(\n                    \"Constructing PeriodArray from a Series of datetime64 data \"\n                    \"will stop inferring the frequency in a future version. \"\n                    \"Pass `freq` explicitly instead.\",\n                    Pandas4Warning,\n                    stacklevel=find_stack_level(),\n                )\n                freq = inferred_freq\n            data = data._values","sourceCodeStart":1467,"sourceCodeEnd":1503,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/period.py#L1467-L1503","documentation":"Raised by dt64arr_to_periodarr when the input data's dtype is not a datetime64 kind ('M'). The conversion from datetime64 to period ordinals only operates on M-dtyped arrays; integer/object/float inputs are rejected and the caller must convert to datetime64 first.","triggerScenarios":"dt64arr_to_periodarr(int_array, freq), period_array(numpy_int_array) routed through the datetime64 path, or passing a DatetimeIndex-without-datetime64-dtype edge case.","commonSituations":"Loading dates as object strings and forgetting to parse. Passing integer epoch values without converting to datetime64. Custom array subclasses whose dtype kind is not M.","solutions":["Convert to datetime64 first: data = pd.to_datetime(data).to_numpy().","Use pd.DatetimeIndex(data) to ensure M dtype before conversion.","If data is ordinals, use PeriodArray directly instead of dt64arr_to_periodarr."],"exampleFix":"# before\nimport numpy as np\ndt64arr_to_periodarr(np.array([18262,18263], dtype='int64'), 'D')\n# after\nfrom pandas import to_datetime\ndt = to_datetime(['2020-01-01','2020-01-02']).to_numpy()\ndt64arr_to_periodarr(dt, 'D')","handlingStrategy":"validation","validationCode":"import numpy as np\nimport pandas as pd\n\ndef ensure_datetime64(data):\n    if not (isinstance(data.dtype, np.dtype) and data.dtype.kind == 'M'):\n        data = pd.to_datetime(data).to_numpy()\n    return data","typeGuard":"import numpy as np\n\ndef is_datetime64(data) -> bool:\n    return isinstance(getattr(data, 'dtype', None), np.dtype) and data.dtype.kind == 'M'","tryCatchPattern":"try:\n    ordinals, freq = dt64arr_to_periodarr(data, freq)\nexcept ValueError:\n    ordinals, freq = dt64arr_to_periodarr(pd.to_datetime(data).to_numpy(), freq)","preventionTips":["Always pd.to_datetime() date-like inputs at load time.","Check dtype.kind == 'M' before datetime-to-period conversion.","Do not feed integer epochs into dt64arr_to_periodarr."],"tags":["period","datetime","dtype","conversion","pandas-arrays"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}