Problem with Mann-Whitney U test in scipy I am trying to perform Mann-Whitney test on a real data set using scipy. I extracted an example where the problem occurs. It seems that the function tiecorrect returns negative value which is the source of problem in my opinion. Here is my case
import scipy.stats

x = [1.] * 163 + [2.] * 81 + [3.] * 40 + [4.] * 6 + [5.] * 2
y = [1.] * 1007 + [2.] * 362 + [3.] * 99 + [4.] * 27 + [5.] * 13 # real-world example

print scipy.stats.mannwhitneyu(x,y)

I have successfully performed the MW test on the same case using SPSS and R but I would like to figure out how to do with in scipy.
 A: There might be a bug in the package, but if you store the u and the prob output separately, you'll see the u value, although the prob is missing for some reason. 
u, prob=scipy.stats.mannwhitneyu(x,y)

u
Out[18]: 193405.5

prob
Out[19]: nan

You could then use the normal approximation of $U$ to get a p-value, though. For large samples,
$$z = \frac{U-m_U}{\sigma_U}$$
where $m_U = \frac{n_1n_2}{2}$ and $\sigma_U=\sqrt{\frac{n_1n_2(n_1+n_2+1)}{12}}$ has approximately a standard Normal distribution.
m_u = len(x)*len(y)/2

sigma_u = np.sqrt(len(x)*len(y)*(len(x)+len(y)+1)/12)

z = (u - m_u)/sigma_u

z
Out[23]: -3.2920646126227546

Then you can compute a p-value.
pval = 2*scipy.stats.norm.cdf(z)

pval
Out[27]: 0.00099454759456888472

Scipy might be trying to compute the direct null hypothesis distribution of $U$, but the Normal approximation should work fine given the number of observations that you have.
A: This thread is old, but for those like me who encounter this scipy bug and find themselves here - 
The issue is indeed the tiecorrect function in scipy.stats.mannwhitneyu(x,y)
If you do not need tie correcting, the scipy.stats.ranksums(x,y) test works fine. 
For your case: 
import scipy.stats

x = [1.] * 163 + [2.] * 81 + [3.] * 40 + [4.] * 6 + [5.] * 2
y = [1.] * 1007 + [2.] * 362 + [3.] * 99 + [4.] * 27 + [5.] * 13 # real-world example

print(scipy.stats.ranksums(x,y))
Out[6]: RanksumsResult(statistic=3.2920646126227546, pvalue=0.00099454759456888472)

