pandas-dev/pandas · error · ValueError

dtype is not specified and cannot be inferred

Error message

dtype is not specified and cannot be inferred

What it means

Raised by PeriodArray.__init__ when, after coercion, dtype is still None. This happens when raw integer ordinals are passed directly without a PeriodDtype — ordinals alone carry no frequency, so the period dtype cannot be inferred and the constructor refuses to guess.

Source

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

        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)

    # error: Signature of "_simple_new" incompatible with supertype "NDArrayBacked"
    @classmethod
    def _simple_new(  # type: ignore[override]
        cls,
        values: npt.NDArray[np.int64],
        dtype: PeriodDtype,
    ) -> Self:
        # alias for PeriodArray.__init__
        assertion_msg = "Should be numpy array of type i8"
        assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg
        return cls(values, dtype=dtype)

    @classmethod
    def _from_sequence(
        cls,

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Always supply a PeriodDtype: PeriodArray(ordinals, dtype=pd.PeriodDtype('D')).
  2. Build via period_array(...) which infers dtype from Period/string values.
  3. If constructing from ordinals, use PeriodArray._simple_new(ordinals, dtype=...) (internal).

Example fix

# before
pa = pd.arrays.PeriodArray(np.array([18262, 18263], dtype='int64'))
# after
pa = pd.arrays.PeriodArray(np.array([18262, 18263], dtype='int64'), dtype=pd.PeriodDtype('D'))
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
import pandas as pd

def period_from_ordinals(ordinals, freq='D'):
    arr = np.asarray(ordinals, dtype='int64')
    return pd.arrays.PeriodArray(arr, dtype=pd.PeriodDtype(freq))

Type guard

import pandas as pd

def has_freq(dtype) -> bool:
    return dtype is not None and hasattr(dtype, 'freq')

Try / catch

try:
    pa = pd.arrays.PeriodArray(values, dtype=dtype)
except ValueError:
    pa = pd.arrays.PeriodArray(values, dtype=pd.PeriodDtype('D'))

Prevention

When it happens

Trigger: Calling PeriodArray(np.array([1,2,3], dtype='int64')) with no dtype. Passing a list/ndarray of ints that look like ordinals but no freq. Internal paths that bypass _from_sequence and reach __init__ with bare ordinals.

Common situations: Treating an int array as period ordinals without specifying freq. Migrating code that previously used an internal _simple_new without dtype. Loading ordinals from a parquet/feather file and forgetting to attach freq.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/4e0f02b2dbfe1ecb. Report an issue: GitHub.