{"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":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":2907,"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":2889,"sourceCodeEnd":2925,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L2889-L2925","documentation":"Raised by maybe_convert_dtype when the data carries a PeriodDtype. Periods are discrete time spans and are not interchangeable with datetime64 instants; pandas directs the user to Period.to_timestamp(). TypeError.","triggerScenarios":"pd.DatetimeIndex(period_index) or pd.to_datetime(period_series); passing a Series with dtype period[...] into a datetime constructor.","commonSituations":"Pipeline that converts PeriodIndex to DatetimeIndex by re-wrapping instead of using the canonical method.","solutions":["Use period_index.to_timestamp() (or series.dt.to_timestamp()) to convert periods to datetimes.","Keep Period data as PeriodIndex when you need period semantics."],"exampleFix":"# before\npd.DatetimeIndex(period_index)\n# after\nperiod_index.to_timestamp()","handlingStrategy":"validation","validationCode":"def to_datetime_safe(data):\n    if isinstance(getattr(data, 'dtype', None), pd.PeriodDtype):\n        return data.to_timestamp()\n    return pd.DatetimeIndex(data)","typeGuard":"def is_period(data) -> bool:\n    return isinstance(getattr(data, 'dtype', None), pd.PeriodDtype)","tryCatchPattern":null,"preventionTips":["Use .to_timestamp() to move from Period to Datetime.","Track dtype categories in your schema.","Don't re-wrap period data through DatetimeIndex."],"tags":["datetime","period","dtype","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}