{"record":{"id":"3422698b39db0d85","repo":"pandas-dev/pandas","slug":"cannot-round-dtype-self-dtype-as-it-is-non-numer","errorCode":null,"errorMessage":"Cannot round dtype {self.dtype} as it is non-numeric","messagePattern":"Cannot round dtype (.+?) as it is non-numeric","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/base.py","lineNumber":2945,"sourceCode":"        Series.round : Round values of a Series.\n\n        Notes\n        -----\n        This is a non-performant default implementation.  Subclasses are\n        encouraged to override it to avoid the elementwise loop.\n\n        Examples\n        --------\n        >>> arr = pd.array([1.234, 5.678, pd.NA], dtype=\"Float64\")\n        >>> arr.round(1)\n        <FloatingArray>\n        [1.2, 5.7, <NA>]\n        Length: 3, dtype: Float64\n        \"\"\"\n        if self.dtype._is_boolean:\n            return self.copy()\n        if not self.dtype._is_numeric:\n            raise TypeError(f\"Cannot round dtype {self.dtype} as it is non-numeric\")\n        # Python's builtin round on complex emits DeprecationWarning (and\n        # raises TypeError in a future Python release); use np.round there.\n        round_fn = np.round if self.dtype.kind == \"c\" else round\n        rounded = [\n            round_fn(item, decimals) if not item_isna else item\n            for item, item_isna in zip(self, self.isna(), strict=True)\n        ]\n        return type(self)._from_sequence(rounded, dtype=self.dtype)\n\n    def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):\n        if any(\n            isinstance(other, (ABCSeries, ABCIndex, ABCDataFrame)) for other in inputs\n        ):\n            return NotImplemented\n\n        result = arraylike.maybe_dispatch_ufunc_to_dunder_op(\n            self, ufunc, method, *inputs, **kwargs\n        )","sourceCodeStart":2927,"sourceCodeEnd":2963,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/base.py#L2927-L2963","documentation":"ExtensionArray.round (base.py:2945) rejects dtypes that are neither boolean (returned as-is) nor numeric; it raises TypeError. Rounding only makes sense for numeric data, so non-numeric EAs (string, object, datetime) are refused.","triggerScenarios":"Calling s.round() or df.round() on a Series/column whose EA dtype._is_numeric is False and _is_boolean is False (e.g. string, categorical-of-strings, some custom EA).","commonSituations":"Running df.round() on a mixed DataFrame that includes string or datetime columns; calling round on a custom EA that did not mark itself numeric; data ingestion that left numeric-looking data as strings.","solutions":["Apply round only to numeric columns: df.select_dtypes(include='number').round().","Convert the column to numeric first: s.astype('Float64').round().","If your custom EA is numeric, ensure its dtype._is_numeric returns True.","Drop or exclude non-numeric columns from the round() call."],"exampleFix":"# before\ndf.round()  # raises if df has a 'string' column\n\n# after\ndf_num = df.select_dtypes(include=\"number\")\ndf[df_num.columns] = df_num.round()","handlingStrategy":"validation","validationCode":"def safe_round(s, decimals=0):\n    import pandas as pd\n    if not pd.api.types.is_numeric_dtype(s):\n        return s\n    return s.round(decimals)","typeGuard":"def is_roundable(dtype) -> bool:\n    import pandas as pd\n    return pd.api.types.is_numeric_dtype(dtype) or pd.api.types.is_bool_dtype(dtype)","tryCatchPattern":"try:\n    df.round()\nexcept TypeError as e:\n    if \"non-numeric\" in str(e):\n        num = df.select_dtypes(\"number\")\n        df[num.columns] = num.round()\n    else:\n        raise","preventionTips":["Round only numeric columns","Use select_dtypes(include='number')","Mark custom numeric EAs via dtype._is_numeric"],"tags":["extension-array","round","dtype-mismatch","numeric"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}