pandas-dev/pandas · error · TypeError

Incorrect dtype

Error message

Incorrect dtype

What it means

Raised by PeriodArray.__init__ when values is an ABCSeries but its underlying _values is not itself a PeriodArray. PeriodArray accepts a Series only as a thin pass-through when that Series already wraps period data; any other Series (datetime, int, object) is rejected with this terse TypeError. The expectation is the caller convert the data first.

Solutions

  1. Convert the Series to period dtype first: s.astype('period[M]').
  2. Use the factory pd.period_array(s, freq='M') or pd.PeriodIndex(s, freq='M').
  3. Pass the raw values (list of Period scalars or int ordinals with explicit dtype) instead of the Series.

Example fix

# before
pd.PeriodArray(pd.Series(['2023-01', '2023-02']))  # raises

# after
pd.PeriodArray(pd.Series(['2023-01', '2023-02']).astype('period[M]'))
Defensive patterns

Strategy: validation

Validate before calling

import pandas as pd

def period_array_from_series(series, freq=None):
    if not isinstance(series.dtype, pd.PeriodDtype):
        series = series.astype(f'period[{freq}]')
    return pd.PeriodArray(series)

Type guard

import pandas as pd

def is_period_series(series) -> bool:
    return isinstance(series.dtype, pd.PeriodDtype)

Try / catch

try:
    pd.PeriodArray(series)
except TypeError as e:
    if 'Incorrect dtype' in str(e):
        pd.PeriodArray(series.astype('period[M]'))
    else:
        raise

Prevention

When it happens

Trigger: PeriodArray(pd.Series(['2023-01', '2023-02'])) — object-dtype Series. PeriodArray(pd.Series([2023, 2024])) — int Series. PeriodArray(datetime_series) without conversion.

Common situations: Assuming PeriodArray will parse string periods from a Series; passing a column from a CSV (object dtype) directly; skipping pd.period_array / astype('period[...]').

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/73c400730efef40b. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/period.py:235

        _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 3b7651241d)