votes up 1

spearmanr only handles 1-D or 2-D arrays

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

    if axis is not None and axis > 1:
        raise ValueError("spearmanr only handles 1-D or 2-D arrays, "
                         "supplied axis argument {}, please use only "
                         "values 0, 1 or None for axis".format(axis))

    a, axisout = _chk_asarray(a, axis)
    if a.ndim > 2:
        raise ValueError("spearmanr only handles 1-D or 2-D arrays")

    if b is None:
        if a.ndim < 2:
            raise ValueError("`spearmanr` needs at least 2 "
                             "variables to compare")
    else:
        # Concatenate a and b, so that we now only have to handle the case
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 2 votes down

This is caused when the argument a is beyond 2D. i.e The shape of a >2D

Code to reproduce the exception:

from scipy import stats
stats.spearmanr([[[1,2,3,4,5]]])

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-a2beb1881914> in <module>()  1 from scipy import stats ----> 2 stats.spearmanr([[[1,2,3,4,5]]]) 
/usr/local/lib/python3.7/dist-packages/scipy/stats/stats.py in spearmanr(a, b, axis, nan_policy)  3821 a, axisout = _chk_asarray(a, axis)  3822 if a.ndim > 2: -> 3823 raise ValueError("spearmanr only handles 1-D or 2-D arrays")  3824   3825 if b is None: 
ValueError: spearmanr only handles 1-D or 2-D arrays

Fixed:

from scipy import stats
stats.spearmanr([[1,2,3,4,5]])

SpearmanrResult(correlation=nan, pvalue=nan)

Nov 25, 2021 kellemnegasi answer
kellemnegasi 18.3k

Add a possible fix

Please authorize to post fix