Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: stats.zscore returns all NaN if given masked array with first element masked #19039

@drammock

Description

@drammock

Describe your issue.

passing a masked array to scipy.stats.zscore yields results that are all nan when the first element of the array is masked.

Reproducing Code Example

import numpy as np
from scipy.stats import zscore

rng = np.random.default_rng(seed=8675309)  # tried with 3 different seeds
x = rng.standard_normal(10)
mask = np.zeros_like(x)
y = np.ma.masked_array(x, mask)
y.mask[1] = True
print(zscore(y))
# [-0.6327236439032247 -- -0.6005585026611974 0.03365730448338995
#  -0.9428312016417735 1.775001047420241 1.189629067553608
#  -0.5510199319620768 -1.2407231008627846 0.9695689615738183]
y.mask[1] = False
y.mask[0] = True  # still happens if additional elements are masked besides element [0]
print(zscore(y))
# [nan nan nan nan nan nan nan nan nan nan]

Error message

n/a

SciPy/NumPy/Python version and system information

1.11.1 1.25.2 sys.version_info(major=3, minor=11, micro=4, releaselevel='final', serial=0)
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /opt/mambaforge/envs/mwe/include
    lib directory: /opt/mambaforge/envs/mwe/lib
    name: blas
    openblas configuration: unknown
    pc file directory: /opt/mambaforge/envs/mwe/lib/pkgconfig
    version: 3.9.0
  lapack:
    detection method: pkgconfig
    found: true
    include directory: /opt/mambaforge/envs/mwe/include
    lib directory: /opt/mambaforge/envs/mwe/lib
    name: lapack
    openblas configuration: unknown
    pc file directory: /opt/mambaforge/envs/mwe/lib/pkgconfig
    version: 3.9.0
  pybind11:
    detection method: pkgconfig
    include directory: /opt/mambaforge/envs/mwe/include
    name: pybind11
    version: 2.10.4
Compilers:
  c:
    commands: /home/conda/feedstock_root/build_artifacts/scipy-split_1687995550978/_build_env/bin/x86_64-conda-linux-gnu-cc
    linker: ld.bfd
    name: gcc
    version: 12.3.0
  c++:
    commands: /home/conda/feedstock_root/build_artifacts/scipy-split_1687995550978/_build_env/bin/x86_64-conda-linux-gnu-c++
    linker: ld.bfd
    name: gcc
    version: 12.3.0
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 0.29.35
  fortran:
    commands: /home/conda/feedstock_root/build_artifacts/scipy-split_1687995550978/_build_env/bin/x86_64-conda-linux-gnu-gfortran
    linker: ld.bfd
    name: gcc
    version: 12.3.0
  pythran:
    include directory: ../../../_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold/lib/python3.11/site-packages/pythran
    version: 0.13.1
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  cross-compiled: false
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/mambaforge/envs/mwe/bin/python
  version: '3.11'

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.stats

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions