{"record":{"id":"3577a14b086d506b","repo":"jax-ml/jax","slug":"fweights-must-be-integer","errorCode":null,"errorMessage":"fweights must be integer.","messagePattern":"fweights must be integer\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9216,"sourceCode":"    if not rowvar and y_arr.shape[0] != 1:\n      y_arr = y_arr.T\n    X = concatenate((X, y_arr), axis=0)\n  if X.shape[1] == 0:\n    cov_shape = () if X.shape[0] == 1 else (X.shape[0], X.shape[0])\n    return array_creation.full(cov_shape, np.nan, dtype=X.dtype)\n\n  if ddof is None:\n    ddof = 1 if bias == 0 else 0\n\n  w: Array | None = None\n  if fweights is not None:\n    fweights = util.ensure_arraylike(\"cov\", fweights)\n    if np.ndim(fweights) > 1:\n      raise RuntimeError(\"cannot handle multidimensional fweights\")\n    if np.shape(fweights)[0] != X.shape[1]:\n      raise RuntimeError(\"incompatible numbers of samples and fweights\")\n    if not issubdtype(fweights.dtype, np.integer):\n      raise TypeError(\"fweights must be integer.\")\n    # Ensure positive fweights; note that numpy raises an error on negative fweights.\n    w = abs(fweights)\n  if aweights is not None:\n    aweights = util.ensure_arraylike(\"cov\", aweights)\n    if np.ndim(aweights) > 1:\n      raise RuntimeError(\"cannot handle multidimensional aweights\")\n    if np.shape(aweights)[0] != X.shape[1]:\n      raise RuntimeError(\"incompatible numbers of samples and aweights\")\n    # Ensure positive aweights: note that numpy raises an error for negative aweights.\n    aweights = abs(aweights)\n    w = asarray(aweights if w is None else w * aweights)\n\n  if dtype is not None:\n    X = X.astype(dtype)\n    w = w.astype(dtype) if w is not None else w\n\n  avg, w_sum = reductions.average(X, axis=1, weights=w, returned=True)\n  w_sum = w_sum[0]","sourceCodeStart":9198,"sourceCodeEnd":9234,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9198-L9234","documentation":"Frequency weights in jnp.cov represent integer replication counts, so their dtype must be a subclass of np.integer. If fweights is float or another type, TypeError('fweights must be integer.') is raised, matching NumPy.","triggerScenarios":"jnp.cov(m, fweights=np.array([1.0, 2.0, 1.0])) — float weights; or weights produced by count/normalization math that yields floats.","commonSituations":"Normalized or fractional weights (e.g. inverse-frequency weights) passed as fweights when they belong in aweights; reading weights from float-typed files.","solutions":["Cast to integer: fweights=w.astype(int)","Use aweights for fractional/float weights instead","Generate counts with integer ops (//, sum of ints) upstream"],"exampleFix":"// before\njnp.cov(m, fweights=jnp.array([1.0, 2.0]))  # TypeError\n// after\njnp.cov(m, fweights=jnp.array([1, 2]))\n// or for fractional weights:\njnp.cov(m, aweights=jnp.array([0.5, 1.5]))","handlingStrategy":"type-guard","validationCode":"if fweights is not None:\n    fweights = jnp.asarray(fweights)\n    if not jnp.issubdtype(fweights.dtype, jnp.integer):\n        fweights = fweights.astype(jnp.int32)  # or move to aweights\njnp.cov(m, fweights=fweights)","typeGuard":"def integer_weights(w) -> bool:\n    return jnp.issubdtype(jnp.asarray(w).dtype, jnp.integer)","tryCatchPattern":null,"preventionTips":["fweights = integer counts only; aweights for floats","Cast counts with .astype(int)","Avoid normalization math on fweights"],"tags":["jax","covariance","weights","dtype-validation"],"backgroundTag":"dtype-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}