{"record":{"id":"a164ea4e49e48524","repo":"pandas-dev/pandas","slug":"the-sort-keyword-in-type-self-name-factor","errorCode":null,"errorMessage":"The 'sort' keyword in {type(self).__name__}.factorize is not supported. To factorize with sort, call pd.factorize(obj, sort=True) instead.","messagePattern":"The 'sort' keyword in (.+?)\\.factorize is not supported\\. To factorize with sort, call pd\\.factorize\\(obj, sort=True\\) instead\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimelike.py","lineNumber":2367,"sourceCode":"\n        return nanops.nanall(self._ndarray, axis=axis, skipna=skipna, mask=self.isna())\n\n    # --------------------------------------------------------------\n    # ExtensionArray Interface\n\n    def _values_for_json(self) -> np.ndarray:\n        # Small performance bump vs the base class which calls np.asarray(self)\n        if isinstance(self.dtype, np.dtype):\n            return self._ndarray\n        return super()._values_for_json()\n\n    def factorize(\n        self,\n        use_na_sentinel: bool = True,\n        sort: bool = False,\n    ):\n        if sort:\n            raise NotImplementedError(\n                f\"The 'sort' keyword in {type(self).__name__}.factorize is not \"\n                \"supported. To factorize with sort, call pd.factorize(obj, sort=True) \"\n                \"instead.\"\n            )\n        return super().factorize(use_na_sentinel=use_na_sentinel)\n\n    def interpolate(\n        self,\n        *,\n        method: InterpolateOptions,\n        axis: int,\n        index: Index,\n        limit,\n        limit_direction,\n        limit_area,\n        copy: bool,\n        **kwargs,\n    ) -> Self:","sourceCodeStart":2349,"sourceCodeEnd":2385,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimelike.py#L2349-L2385","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Replace array.factorize(sort=True) with pd.factorize(array, sort=True).","If you need the array-level method, call it without sort and sort the codes/uniques yourself via np.argsort on the uniques."],"exampleFix":"# before\nidx.array.factorize(sort=True)\n\n# after\ncodes, uniques = pd.factorize(idx, sort=True)","handlingStrategy":"validation","validationCode":"def safe_factorize(obj, sort=False):\n    return pd.factorize(obj, sort=sort)  # never call obj.array.factorize(sort=...)","typeGuard":null,"tryCatchPattern":"try:\n    arr.factorize(sort=True)\nexcept NotImplementedError as e:\n    if 'is not supported' in str(e):\n        pd.factorize(arr, sort=True)\n    else: raise","preventionTips":["Always go through pd.factorize for sort=True across ExtensionArrays.","Lint for '.array.factorize(' / '.factorize(sort=True)' patterns in code review."],"tags":["factorize","datetime","extension-array","not-implemented"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}