pandas-dev/pandas · error · TypeError

Not supported to convert PeriodArray to array with…

Error message

Not supported to convert PeriodArray to array with different 'freq' ({self.freqstr} vs {type.freq})

What it means

Raised by PeriodArray.__arrow_array__ when the target pyarrow type is an ArrowPeriodType whose freq string differs from the PeriodArray's freqstr. Arrow period values carry their frequency as part of the type, so a cross-freq conversion would silently change semantics; pandas rejects it. Both freqs are shown for diagnosis.

Solutions

  1. Match the freqs: convert the PeriodArray first with .asfreq(target_freq) before arrow export.
  2. Drop the explicit type argument and let __arrow_array__ infer the correct ArrowPeriodType.
  3. Export as int64 ordinals: pa.array(period_arr, type=pa.int64()) (the integer path is allowed).

Example fix

# before
pa.array(monthly_idx, type=ArrowPeriodType('D'))  # raises

# after
pa.array(monthly_idx.asfreq('D'), type=ArrowPeriodType('D'))
Defensive patterns

Strategy: validation

Validate before calling

import pandas as pd

def to_arrow_period(period_idx, target_type=None):
    if target_type is not None and hasattr(target_type, 'freq'):
        if period_idx.freqstr != target_type.freq:
            period_idx = period_idx.asfreq(target_type.freq)
    import pyarrow as pa
    return pa.array(period_idx, type=target_type)

Type guard

def arrow_freq_matches(period_idx, target_type) -> bool:
    return target_type is None or not hasattr(target_type, 'freq') or period_idx.freqstr == target_type.freq

Try / catch

try:
    pa.array(period_idx, type=target_type)
except TypeError as e:
    if "different 'freq'" in str(e):
        pa.array(period_idx.asfreq(target_type.freq), type=target_type)
    else:
        raise

Prevention

When it happens

Trigger: pa.array(monthly_period_idx, type=ArrowPeriodType('D')) — monthly data into a daily-typed arrow array. pyarrow.Table.from_pandas(df) where the schema pins a different period freq than the source. Casting an Arrow-backed period array to a mismatched period type.

Common situations: Schema evolution where period freq changed; concatenating arrow tables with period columns of different freqs; user-supplied pyarrow schema that doesn't match the data.

Related errors


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

Appendix: source

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

        # This will raise TypeError for non-object dtypes
        return np.array(list(self), dtype=object)

    def __arrow_array__(self, type=None):
        """
        Convert myself into a pyarrow Array.
        """
        import pyarrow

        from pandas.core.arrays.arrow.extension_types import ArrowPeriodType

        if type is not None:
            if pyarrow.types.is_integer(type):
                return pyarrow.array(self._ndarray, mask=self.isna(), type=type)
            elif isinstance(type, ArrowPeriodType):
                # ensure we have the same freq
                if self.freqstr != type.freq:
                    raise TypeError(
                        "Not supported to convert PeriodArray to array with different "
                        f"'freq' ({self.freqstr} vs {type.freq})"
                    )
            else:
                raise TypeError(
                    f"Not supported to convert PeriodArray to '{type}' type"
                )

        period_type = ArrowPeriodType(self.freqstr)
        storage_array = pyarrow.array(self._ndarray, mask=self.isna(), type="int64")
        return pyarrow.ExtensionArray.from_storage(period_type, storage_array)

    # --------------------------------------------------------------------
    # Vectorized analogues of Period properties

    year = _field_accessor(
        "year",
        """

View on GitHub (pinned to 3b7651241d)