{"record":{"id":"9067609a76259a6f","repo":"pandas-dev/pandas","slug":"dtype-self-dtype-does-not-support-operation-906760","errorCode":null,"errorMessage":"dtype '{self.dtype}' does not support operation '{how}'","messagePattern":"dtype '(.+?)' does not support operation '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/base.py","lineNumber":3076,"sourceCode":"        op = WrappedCythonOp(how=how, kind=kind, has_dropped_na=has_dropped_na)\n\n        initial: Any = 0\n        # GH#43682\n        if isinstance(self.dtype, StringDtype):\n            # StringArray\n            if op.how in [\n                \"prod\",\n                \"mean\",\n                \"median\",\n                \"cumsum\",\n                \"cumprod\",\n                \"std\",\n                \"sem\",\n                \"var\",\n                \"skew\",\n                \"kurt\",\n            ]:\n                raise TypeError(\n                    f\"dtype '{self.dtype}' does not support operation '{how}'\"\n                )\n            if op.how not in [\"any\", \"all\"]:\n                # Fail early to avoid conversion to object\n                op._get_cython_function(op.kind, op.how, np.dtype(object), False)\n\n            arr = self\n            if op.how == \"sum\":\n                initial = \"\"\n                # https://github.com/pandas-dev/pandas/issues/60229\n                # All NA should result in the empty string.\n                assert \"skipna\" in kwargs\n                if kwargs[\"skipna\"] and min_count == 0:\n                    arr = arr.fillna(\"\")\n            npvalues = arr.to_numpy(object, na_value=np.nan)\n        else:\n            raise NotImplementedError(\n                f\"function is not implemented for this dtype: {self.dtype}\"","sourceCodeStart":3058,"sourceCodeEnd":3094,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/base.py#L3058-L3094","documentation":"In _grouped_reduce, when the array's dtype is a StringDtype, the code explicitly rejects arithmetic/statistical groupby operations that are meaningless on strings (prod, mean, median, cumsum, cumprod, std, sem, var, skew, kurt) by raising TypeError. Only any/all/sum/first/last (and count) are permitted on string columns. This fails early to avoid a silent object-dtype conversion.","triggerScenarios":"Calling groupby aggregations like df.groupby('g').mean(), .prod(), .std(), .median() on a DataFrame that contains a StringArray/'str' column (the new pyarrow-backed or object-backed string dtype). The groupby engine routes the string block into ExtensionArray._grouped_reduce which raises TypeError naming the unsupported operation.","commonSituations":"Migrating to pandas' string dtype (pd.StringDtype / 'str') where a whole-frame groupby aggregation now hits a string column that previously sat as object and silently produced NaN. Running df.groupby(...).agg(['mean','std',...]) across all columns including string ones.","solutions":["Aggregate only numeric columns: df.groupby('g')[numeric_cols].mean().","Use an agg spec that selects per-column reductions, excluding the string column from arithmetic aggregations.","If the column should be object dtype, cast it: df['col'] = df['col'].astype(object) (only if object semantics are truly wanted)."],"exampleFix":"// before\ndf.groupby('g').mean()  # TypeError on 'str' column\n\n// after\nnum = df.select_dtypes(include='number').columns\ndf.groupby('g')[list(num)].mean()","handlingStrategy":"validation","validationCode":"from pandas.core.arrays.string_ import StringDtype\nstring_ops = {'prod','mean','median','cumsum','cumprod','std','sem','var','skew','kurt'}\nif isinstance(arr.dtype, StringDtype) and how in string_ops:\n    raise TypeError(f\"dtype '{arr.dtype}' does not support '{how}'\")","typeGuard":"def is_string_dtype_obj(arr) -> bool:\n    from pandas.core.arrays.string_ import StringDtype\n    return isinstance(arr.dtype, StringDtype)","tryCatchPattern":"try:\n    out = df.groupby('g').mean()\nexcept TypeError:\n    num = df.select_dtypes(include='number').columns\n    out = df.groupby('g')[list(num)].mean()","preventionTips":["Select numeric columns before arithmetic groupby aggregations.","Use explicit agg specs that exclude string columns from arithmetic ops.","Be aware that pd.StringDtype/'str' is stricter than object — mean/std/etc. are rejected."],"tags":["extension-array","string-dtype","groupby","type-error","pandas"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}