{"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":2380,"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":2362,"sourceCodeEnd":2398,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimelike.py#L2362-L2398","documentation":"Raised by DatetimeLikeArrayMixin.factorize when sort=True is passed directly to the ExtensionArray.factorize method. Sorting the uniques of a datetime-like array requires reordering and is intentionally implemented only at the top-level pd.factorize(obj, sort=True); the array-level method refuses to silently do extra work or diverge in semantics.","triggerScenarios":"Calling obj.factorize(sort=True) on a DatetimeArray/TimedeltaArray/PeriodArray (or via the .values accessor), instead of the module-level pd.factorize.","commonSituations":"Refactoring code that called pd.factorize into the method form for chaining; assuming the method mirrors numpy/sklearn factorize signatures fully; typing stubs that list sort as a valid kwarg.","solutions":["Use pd.factorize(obj, sort=True) at the module level.","If you must use the method, call obj.factorize() then sort the resulting codes/uniques yourself.","Lint/guard callsites so sort=True is never passed to the array method."],"exampleFix":"// before\ncodes, uniques = idx.factorize(sort=True)  # NotImplementedError\n\n// after\ncodes, uniques = pd.factorize(idx, sort=True)","handlingStrategy":"validation","validationCode":"def factorize_safe(obj, sort=False):\n    if sort:\n        return pd.factorize(obj, sort=True)\n    return obj.factorize() if hasattr(obj, \"factorize\") else pd.factorize(obj)","typeGuard":"def is_array_factorize_no_sort(method_name, kwargs) -> bool:\n    return method_name == \"factorize\" and kwargs.get(\"sort\", False) is True","tryCatchPattern":"try:\n    codes, uniques = obj.factorize(sort=True)\nexcept NotImplementedError as e:\n    if \"sort\" in str(e) and \"pd.factorize\" in str(e):\n        codes, uniques = pd.factorize(obj, sort=True)\n    else:\n        raise","preventionTips":["Standardize on pd.factorize(...) at module level across the codebase.","Lint for .factorize(sort=True) calls on Index/Array objects.","Educate reviewers that the method form intentionally omits sort."],"tags":["pandas","factorize","datetimearray","api-misuse"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}