pandas-dev/pandas · error · ValueError

Not enough parameters to construct Period range

Error message

Not enough parameters to construct Period range

What it means

Raised by PeriodArray._generate_range (used by period_range) when both start and end are None. A period range needs at least one bound to anchor it; specifying only periods (count) without start or end is insufficient. freq is optional only when start/end are datetime-like with inferable frequency.

Solutions

  1. Provide a start: pd.period_range(start='2023-01-01', periods=5, freq='D').
  2. Provide an end: pd.period_range(end='2023-12-31', periods=5, freq='D').
  3. If you need a list of N anonymous ordinals, construct PeriodArray directly with a dtype.

Example fix

# before
pd.period_range(periods=5, freq='D')  # raises

# after
pd.period_range(start='2023-01-01', periods=5, freq='D')
Defensive patterns

Strategy: validation

Validate before calling

import pandas as pd

def build_period_range(start=None, end=None, periods=None, freq=None):
    if start is None and end is None:
        raise ValueError('period_range requires at least one of start or end')
    return pd.period_range(start=start, end=end, periods=periods, freq=freq)

Type guard

def has_range_anchor(start, end) -> bool:
    return start is not None or end is not None

Try / catch

try:
    pd.period_range(periods=periods, freq=freq)
except ValueError as e:
    if 'Not enough parameters' in str(e):
        pd.period_range(start='2023-01-01', periods=periods, freq=freq)
    else:
        raise

Prevention

When it happens

Trigger: pd.period_range(periods=5, freq='D') — no start/end. pd.period_range(None, None, 5). Internal calls to _generate_range that drop both bounds.

Common situations: Building a generic 'N periods' range without a starting anchor; misreading the API and assuming periods alone suffices; refactoring that nulls out start/end conditionally.

Related errors


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

Appendix: source

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

        PeriodArray[freq]
        """
        if isinstance(freq, BaseOffset):
            freq = PeriodDtype(freq)._freqstr
        data, freq = dt64arr_to_periodarr(data, freq, tz)
        dtype = PeriodDtype(freq)
        return cls(data, dtype=dtype)

    @classmethod
    def _generate_range(cls, start, end, periods, freq):
        periods = dtl.validate_periods(periods)

        if freq is not None:
            freq = Period._maybe_convert_freq(freq)

        if start is not None or end is not None:
            subarr, freq = _get_ordinal_range(start, end, periods, freq)
        else:
            raise ValueError("Not enough parameters to construct Period range")

        return subarr, freq

    @classmethod
    def _from_fields(cls, *, fields: dict, freq) -> Self:
        subarr, freq = _range_from_fields(freq=freq, **fields)
        dtype = PeriodDtype(freq)
        return cls._simple_new(subarr, dtype=dtype)

    # -----------------------------------------------------------------
    # DatetimeLike Interface

    def _unbox_scalar(
        self,
        # error: Argument 1 of "_unbox_scalar" is incompatible with supertype
        # "pandas.core.arrays.datetimelike.DatetimeLikeArrayMixin"; supertype
        #  defines the argument type as "Period | Timestamp | Timedelta | NaTType"
        value: Period | NaTType,  # type: ignore[override]

View on GitHub (pinned to 3b7651241d)