{"record":{"id":"e247a56b1072b74e","repo":"pandas-dev/pandas","slug":"passing-perioddtype-data-is-invalid-use-data-to","errorCode":null,"errorMessage":"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead","messagePattern":"Passing PeriodDtype data is invalid\\. Use `data\\.to_timestamp\\(\\)` instead","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":2905,"sourceCode":"    if not hasattr(data, \"dtype\"):\n        # e.g. collections.deque\n        return data, copy\n\n    if is_float_dtype(data.dtype):\n        # pre-2.0 we treated these as wall-times, inconsistent with ints\n        # GH#23675, GH#45573 deprecated to treat symmetrically with integer dtypes.\n        # Note: data.astype(np.int64) fails ARM tests, see\n        # https://github.com/pandas-dev/pandas/issues/49468.\n        data = data.astype(DT64NS_DTYPE).view(\"i8\")\n        copy = False\n\n    elif lib.is_np_dtype(data.dtype, \"m\") or is_bool_dtype(data.dtype):\n        # GH#29794 enforcing deprecation introduced in GH#23539\n        raise TypeError(f\"dtype {data.dtype} cannot be converted to datetime64[ns]\")\n    elif isinstance(data.dtype, PeriodDtype):\n        # Note: without explicitly raising here, PeriodIndex\n        #  test_setops.test_join_does_not_recur fails\n        raise TypeError(\n            \"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead\"\n        )\n\n    elif isinstance(data.dtype, ExtensionDtype) and not isinstance(\n        data.dtype, DatetimeTZDtype\n    ):\n        # TODO: We have no tests for these\n        data = np.array(data, dtype=np.object_)\n        copy = False\n\n    return data, copy\n\n\n# -------------------------------------------------------------------\n# Validation and Inference\n\n\ndef _maybe_infer_tz(tz: tzinfo | None, inferred_tz: tzinfo | None) -> tzinfo | None:","sourceCodeStart":2887,"sourceCodeEnd":2923,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L2887-L2923","documentation":"Raised by maybe_convert_dtype when the input data has a PeriodDtype. Period objects are a distinct temporal type (a time span anchored to a frequency) and cannot be reinterpreted as datetime64[ns] instants without an explicit conversion choice. The message points to the supported API: PeriodIndex.to_timestamp().","triggerScenarios":"Passing a Series with dtype `period[D]` or a PeriodIndex into `pd.DatetimeIndex(...)`, `pd.to_datetime(period_series)`, or any datetime constructor that routes through maybe_convert_dtype. Assigning a Period column into a datetime-typed slot.","commonSituations":"Switching modeling granularity from periods to timestamps (e.g. monthly periods -> month-start timestamps). Storing periods then handing them to a library that expects datetimes. Bug where the wrong column is referenced.","solutions":["Convert explicitly with the recommended API: `period_obj.to_timestamp()` (or `.dt.to_timestamp()` on a Series).","Pass `freq` if you want a specific anchor: `period_index.to_timestamp(freq='D')`.","If you wanted the period end: `period_index.to_timestamp(how='end')`.","Re-check whether the destination really wants datetime64 — if downstream accepts Period, pass the Period through unchanged."],"exampleFix":"// before\ndti = pd.DatetimeIndex(period_index)\n\n// after\ndti = period_index.to_timestamp()","handlingStrategy":"type-guard","validationCode":"import pandas as pd\n\ndef period_to_datetime(obj):\n    if isinstance(getattr(obj, 'dtype', None), pd.PeriodDtype):\n        return obj.dt.to_timestamp()\n    return obj","typeGuard":"def is_period(obj) -> bool:\n    import pandas as pd\n    return isinstance(getattr(obj, 'dtype', None), pd.PeriodDtype)","tryCatchPattern":"try:\n    dti = pd.DatetimeIndex(period_index)\nexcept TypeError as e:\n    if 'PeriodDtype data is invalid' in str(e):\n        dti = period_index.to_timestamp()\n    else:\n        raise","preventionTips":["Use to_timestamp() to move from Period to datetime — never DatetimeIndex(period_data).","Check dtype with isinstance(..., pd.PeriodDtype) before datetime conversion."],"tags":["datetime","period","dtype","construction","to-timestamp"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}