pandas-dev/pandas · error · TypeError
Passing PeriodDtype data is invalid. Use `data.to_timestamp(
Error message
Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead
What it means
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.
Source
Thrown at pandas/core/arrays/datetimes.py:2907
if not hasattr(data, "dtype"):
# e.g. collections.deque
return data, copy
if is_float_dtype(data.dtype):
# pre-2.0 we treated these as wall-times, inconsistent with ints
# GH#23675, GH#45573 deprecated to treat symmetrically with integer dtypes.
# Note: data.astype(np.int64) fails ARM tests, see
# https://github.com/pandas-dev/pandas/issues/49468.
data = data.astype(DT64NS_DTYPE).view("i8")
copy = False
elif lib.is_np_dtype(data.dtype, "m") or is_bool_dtype(data.dtype):
# GH#29794 enforcing deprecation introduced in GH#23539
raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]")
elif isinstance(data.dtype, PeriodDtype):
# Note: without explicitly raising here, PeriodIndex
# test_setops.test_join_does_not_recur fails
raise TypeError(
"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead"
)
elif isinstance(data.dtype, ExtensionDtype) and not isinstance(
data.dtype, DatetimeTZDtype
):
# TODO: We have no tests for these
data = np.array(data, dtype=np.object_)
copy = False
return data, copy
# -------------------------------------------------------------------
# Validation and Inference
def _maybe_infer_tz(tz: tzinfo | None, inferred_tz: tzinfo | None) -> tzinfo | None:View on GitHub (pinned to 71959b8cb9)
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.
Example fix
# before pd.DatetimeIndex(period_index) # after period_index.to_timestamp()
Defensive patterns
Strategy: validation
Validate before calling
def to_datetime_safe(data):
if isinstance(getattr(data, 'dtype', None), pd.PeriodDtype):
return data.to_timestamp()
return pd.DatetimeIndex(data) Type guard
def is_period(data) -> bool:
return isinstance(getattr(data, 'dtype', None), pd.PeriodDtype) Prevention
- Use .to_timestamp() to move from Period to Datetime.
- Track dtype categories in your schema.
- Don't re-wrap period data through DatetimeIndex.
When it happens
Trigger: pd.DatetimeIndex(period_index) or pd.to_datetime(period_series); passing a Series with dtype period[...] into a datetime constructor.
Common situations: Pipeline that converts PeriodIndex to DatetimeIndex by re-wrapping instead of using the canonical method.
Related errors
- You must pass a freq argument as current index has none.
- dtype {data.dtype} cannot be converted to datetime64[ns]
- Passing in 'datetime64' dtype with no precision is not allow
- Unexpected value for 'dtype': '{dtype}'. Must be 'datetime64
- cannot supply both a tz and a dtype with a tz
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/e247a56b1072b74e.
Report an issue: GitHub.