{"record":{"id":"36c4bcc7dd4e3cdc","repo":"matplotlib/matplotlib","slug":"col-must-be-an-int-or-sequence-of-ints","errorCode":null,"errorMessage":"col must be an int or sequence of ints.","messagePattern":"col must be an int or sequence of ints\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/matplotlib/table.py","lineNumber":508,"sourceCode":"            ypos += heights[row]\n\n        # set cell positions\n        for (row, col), cell in self._cells.items():\n            cell.set_x(lefts[col])\n            cell.set_y(bottoms[row])\n\n    def auto_set_column_width(self, col):\n        \"\"\"\n        Automatically set the widths of given columns to optimal sizes.\n\n        Parameters\n        ----------\n        col : int or sequence of ints\n            The indices of the columns to auto-scale.\n        \"\"\"\n        col1d = np.atleast_1d(col)\n        if not np.issubdtype(col1d.dtype, np.integer):\n            raise TypeError(\"col must be an int or sequence of ints.\")\n        for cell in col1d:\n            self._autoColumns.append(cell)\n\n        self.stale = True\n\n    def _auto_set_column_width(self, col, renderer):\n        \"\"\"Automatically set width for column.\"\"\"\n        cells = [cell for key, cell in self._cells.items() if key[1] == col]\n        max_width = max((cell.get_required_width(renderer) for cell in cells),\n                        default=0)\n        for cell in cells:\n            cell.set_width(max_width)\n\n    def auto_set_font_size(self, value=True):\n        \"\"\"Automatically set font size.\"\"\"\n        self._autoFontsize = value\n        self.stale = True\n","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/matplotlib/matplotlib/blob/b379c1b69e012b142c0f496a52bcb30513802d72/lib/matplotlib/table.py#L490-L526","documentation":"Table.auto_set_column_width wraps its argument with np.atleast_1d and requires the resulting dtype to be integer (table.py:508). A single float, a list of floats, string digits, or a float numpy array raises TypeError — even values like 1.0 that look like valid column indices.","triggerScenarios":"table.auto_set_column_width(0.0); auto_set_column_width(np.array([0.0, 1.0])); auto_set_column_width(['0', '1']); column indices computed from divisions or read from JSON as floats.","commonSituations":"Indices derived from arithmetic (i / 2, np.linspace) or config/JSON parsing that yields floats; pandas .get_loc results assumed to be ints on categorical indexes (they can be slices/booleans).","solutions":["Pass ints: table.auto_set_column_width(0) or table.auto_set_column_width([0, 2])","Coerce untrusted input first: table.auto_set_column_width([int(c) for c in cols])","Generate candidate columns with np.arange(n), never np.linspace"],"exampleFix":"# before: float indices from a config file\nauto_cols = [0.0, 2.0]\ntable.auto_set_column_width(auto_cols)  # TypeError: col must be an int or sequence of ints\n\n# after: coerce to Python ints\ntable.auto_set_column_width([int(c) for c in auto_cols])","handlingStrategy":"type-guard","validationCode":"import numpy as np\n\ndef as_int_columns(col):\n    a = np.atleast_1d(col)\n    if not np.issubdtype(a.dtype, np.integer):\n        if np.issubdtype(a.dtype, np.floating) and np.all(a == a.astype(int)):\n            a = a.astype(int)\n        else:\n            raise TypeError(f'column indices must be ints, got dtype {a.dtype}')\n    return a.tolist()\n\ntable.auto_set_column_width(as_int_columns(cfg['auto_cols']))","typeGuard":"import numpy as np\n\ndef is_int_index_list(col) -> bool:\n    try:\n        a = np.atleast_1d(col)\n    except Exception:\n        return False\n    return np.issubdtype(a.dtype, np.integer)","tryCatchPattern":"try:\n    table.auto_set_column_width(col)\nexcept TypeError as e:\n    raise TypeError(f'auto_set_column_width needs int indices, got {col!r}') from e","preventionTips":["Int-coerce config values at load: json ints stay ints, yaml floats get int(v)","Use range(n)/np.arange(n) when generating candidate columns","Never derive column indices from division or linspace results"],"tags":["matplotlib","table","typeerror","column-width","dtype"],"backgroundTag":"invalid-argument-type","analyzedSha":"b379c1b69e012b142c0f496a52bcb30513802d72","analyzedAt":"2026-08-21T23:31:55.468Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}