{"record":{"id":"70ccad7aae54da03","repo":"pandas-dev/pandas","slug":"cannot-multiply-stringarray-by-bools-explicitly-c","errorCode":null,"errorMessage":"Cannot multiply StringArray by bools. Explicitly cast to integers instead.","messagePattern":"Cannot multiply StringArray by bools\\. Explicitly cast to integers instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":1264,"sourceCode":"                    stacklevel=find_stack_level(),\n                )\n            if len(other) != len(self):\n                # prevent improper broadcasting when other is 2D\n                raise ValueError(\n                    f\"Lengths of operands do not match: {len(self)} != {len(other)}\"\n                )\n\n            # for array-likes, first filter out NAs before converting to numpy\n            if not is_array_like_deprecate_non_pandas(other):\n                other = np.asarray(other)\n            other = other[valid]\n\n        other_dtype = getattr(other, \"dtype\", None)\n        if op.__name__.strip(\"_\") in [\"mul\", \"rmul\"] and (\n            lib.is_bool(other) or lib.is_np_dtype(other_dtype, \"b\")\n        ):\n            # GH#62595\n            raise TypeError(\n                \"Cannot multiply StringArray by bools. \"\n                \"Explicitly cast to integers instead.\"\n            )\n\n        if op.__name__ in ops.ARITHMETIC_BINOPS:\n            result = np.empty_like(self._ndarray, dtype=\"object\")\n            result[mask] = self.dtype.na_value\n            result[valid] = op(self._ndarray[valid], other)\n            if not lib.is_string_array(result, skipna=True):\n                return result\n            return self._from_backing_data(result)\n        else:\n            # logical\n            result = np.zeros(len(self._ndarray), dtype=\"bool\")\n            result[valid] = op(self._ndarray[valid], other)\n            res_arr = BooleanArray(result, mask)\n            if self.dtype.na_value is np.nan:\n                if op == operator.ne:","sourceCodeStart":1246,"sourceCodeEnd":1282,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_.py#L1246-L1282","documentation":"StringArray refuses multiplication by bool or bool-dtype arrays (GH#62595) because multiplying strings by booleans is ambiguous (repeat vs. mask). pandas requires an explicit integer cast so the intent (string repetition count) is unambiguous.","triggerScenarios":"s * True, s * [True, False], s * bool_series, or s * np.array([1,0], dtype=bool) where s is string-dtyped; op.__name__ in mul/rmul and lib.is_bool(other) or other has np dtype 'b'.","commonSituations":"Using a boolean mask as a multiplier instead of as an indexer; piping a condition into a multiplication instead of np.where; refactoring an int column to bool and breaking string repetition.","solutions":["Cast the bool operand to int explicitly: s * other.astype(int).","If repeating by 0/1 pattern was the goal, use s.where(other, '') or s.repeat(other.astype(int)).","Replace bool with an integer count array conveying repetition counts."],"exampleFix":"// before\ns = pd.Series(['ab','cd'], dtype='string')\ns * np.array([True, False])  # TypeError\n// after\ns * np.array([True, False]).astype(int)  # 'ab', ''","handlingStrategy":"validation","validationCode":"def safe_mul(s, other):\n    if pd.api.types.is_bool_dtype(pd.Series(other).dtype):\n        other = pd.Series(other).astype(int)\n    return s * other","typeGuard":"def is_non_bool_multiplier(other) -> bool:\n    import numpy as np\n    arr = np.asarray(other)\n    return arr.dtype.kind != 'b'","tryCatchPattern":"try:\n    s * other\nexcept TypeError as e:\n    if 'Cannot multiply StringArray by bools' in str(e):\n        s * np.asarray(other).astype(int)\n    else:\n        raise","preventionTips":["Cast bool arrays to int before multiplying strings.","Use boolean masks as indexers, not multipliers.","Keep repetition counts as integers."],"tags":["pandas","string-array","arithmetic","boolean","multiplication"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}