pandas-dev/pandas · error · TypeError
Incorrect dtype
Error message
Incorrect dtype
What it means
Raised by PeriodArray.__init__ when values is a pandas Series whose underlying _values is not itself a PeriodArray. The constructor will only adopt a Series if its values are already period-typed; otherwise the Series does not carry valid period ordinals and is rejected as incorrectly typed.
Source
Thrown at pandas/core/arrays/period.py:234
_field_ops + _object_ops + _bool_ops + _deprecated_ops
)
_datetimelike_methods: list[str] = ["strftime", "to_timestamp", "asfreq"]
_dtype: PeriodDtype
# --------------------------------------------------------------------
# Constructors
def __init__(self, values, dtype: Dtype | None = None, copy: bool = False) -> None:
if dtype is not None:
dtype = pandas_dtype(dtype)
if not isinstance(dtype, PeriodDtype):
raise ValueError(f"Invalid dtype {dtype} for PeriodArray")
if isinstance(values, ABCSeries):
values = values._values
if not isinstance(values, type(self)):
raise TypeError("Incorrect dtype")
elif isinstance(values, ABCPeriodIndex):
values = values._values
if isinstance(values, type(self)):
if dtype is not None and dtype != values.dtype:
raise raise_on_incompatible(values, dtype.freq)
values, dtype = values._ndarray, values.dtype
if not copy:
values = np.asarray(values, dtype="int64")
else:
values = np.array(values, dtype="int64", copy=copy)
if dtype is None:
raise ValueError("dtype is not specified and cannot be inferred")
dtype = cast("PeriodDtype", dtype)
NDArrayBacked.__init__(self, values, dtype)
View on GitHub (pinned to 71959b8cb9)
Solutions
- Convert the Series to period dtype first: s.astype('period[D]').
- Use pd.period_array(s.tolist(), dtype=pd.PeriodDtype('D')) to build from arbitrary values.
- Ensure the Series was created from a PeriodIndex or period-typed data.
Example fix
# before
s = pd.Series(['2020-01-01','2020-01-02'])
pa = pd.arrays.PeriodArray(s)
# after
pa = s.astype('period[D]').array
# or
pa = pd.period_array(s, dtype=pd.PeriodDtype('D')) Defensive patterns
Strategy: validation
Validate before calling
import pandas as pd
def period_array_from_series(s: pd.Series, freq: str = 'D'):
if not isinstance(s._values, pd.arrays.PeriodArray):
s = s.astype(f'period[{freq}]')
return s.array Type guard
import pandas as pd
def is_period_series(s: pd.Series) -> bool:
return isinstance(s.dtype, pd.PeriodDtype) Try / catch
try:
pa = pd.arrays.PeriodArray(s)
except TypeError:
pa = pd.period_array(s, dtype=pd.PeriodDtype('D')) Prevention
- Never pass a non-period Series to PeriodArray(); convert with .astype first.
- Use pd.period_array(data, dtype=...) for arbitrary input.
- Check s.dtype is PeriodDtype before constructing from a Series.
When it happens
Trigger: Passing pd.arrays.PeriodArray(some_series) where some_series.dtype is not period[M]/period[D]/etc. Building a PeriodArray from a Series of strings or ints without going through _from_sequence.
Common situations: Users wrapping an arbitrary Series into PeriodArray directly. Series that came from .astype('object') or arithmetic that lost period dtype. Confusing PeriodArray(Series) with pd.Series.astype('period[...]').
Related errors
- Invalid dtype {dtype} for PeriodArray
- dtype is not specified and cannot be inferred
- PeriodArray does not allow floating point in construction
- Period dtypes are not supported, use a PeriodIndex instead
- Not enough parameters to construct Period range
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/73c400730efef40b.
Report an issue: GitHub.