pandas-dev/pandas · error · NotImplementedError

The 'sort' keyword in {type(self).__name__}.factorize is not

Error message

The 'sort' keyword in {type(self).__name__}.factorize is not supported. To factorize with sort, call pd.factorize(obj, sort=True) instead.

What it means

Raised by DatetimeLikeArrayMixin.factorize (overridden) when sort=True is passed to the ExtensionArray.factorize method directly. Sorting requires the global codes to be remapped, which the array-local factorize cannot do, so pandas routes it through the top-level pd.factorize that knows how to post-process the uniques. This is a NotImplementedError, not a data error.

Source

Thrown at pandas/core/arrays/datetimelike.py:2367

        return nanops.nanall(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())

    # --------------------------------------------------------------
    # ExtensionArray Interface

    def _values_for_json(self) -> np.ndarray:
        # Small performance bump vs the base class which calls np.asarray(self)
        if isinstance(self.dtype, np.dtype):
            return self._ndarray
        return super()._values_for_json()

    def factorize(
        self,
        use_na_sentinel: bool = True,
        sort: bool = False,
    ):
        if sort:
            raise NotImplementedError(
                f"The 'sort' keyword in {type(self).__name__}.factorize is not "
                "supported. To factorize with sort, call pd.factorize(obj, sort=True) "
                "instead."
            )
        return super().factorize(use_na_sentinel=use_na_sentinel)

    def interpolate(
        self,
        *,
        method: InterpolateOptions,
        axis: int,
        index: Index,
        limit,
        limit_direction,
        limit_area,
        copy: bool,
        **kwargs,
    ) -> Self:

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Replace array.factorize(sort=True) with pd.factorize(array, sort=True).
  2. If you need the array-level method, call it without sort and sort the codes/uniques yourself via np.argsort on the uniques.

Example fix

# before
idx.array.factorize(sort=True)

# after
codes, uniques = pd.factorize(idx, sort=True)
Defensive patterns

Strategy: validation

Validate before calling

def safe_factorize(obj, sort=False):
    return pd.factorize(obj, sort=sort)  # never call obj.array.factorize(sort=...)

Try / catch

try:
    arr.factorize(sort=True)
except NotImplementedError as e:
    if 'is not supported' in str(e):
        pd.factorize(arr, sort=True)
    else: raise

Prevention

When it happens

Trigger: Calling obj.factorize(sort=True) on a DatetimeArray, TimedeltaArray, or PeriodArray (e.g. idx.array.factorize(sort=True), series.array.factorize(sort=True)). Also via library code that forwards sort to the EA-level factorize.

Common situations: Copy-pasting a pd.factorize call into an .array.factorize call while keeping sort=True. Building generic pipelines that call factorize on arbitrary ExtensionArrays with sort.

Related errors


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