Issue 1:
Describe your issue.

I'm running a pre-post hypothesis test on a small dataset using the Wilcoxon signed rank test (scipy.stats.wilcoxon), and noticed when I validated the results the p-value did not match. It looks like it is not using the normal approximation even though ties exist.

Per the documentation the expected approach is:

"To derive the p-value, the exact distribution (mode == 'exact') can be used for sample sizes of up to 25. The default mode == 'auto' uses the exact distribution if there are at most 25 observations and no ties, otherwise a normal approximation is used (mode == 'approx')."

The example demonstrates this by running the out of the box wilcoxon then testing for ties and running the "mode = 'approx'" if they exist.

You will find the first result when running the regular test is pvalue=0.01220703125 and the second with logic built in to test for ties is pvalue=0.014944844395687155

In this dataset they exist in both the pre and post period.

Reproducing Code Example

dfgh = pd.DataFrame({'ID':[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,13,13],
'Period':['0 Month','3 Month','0 Month','3 Month','0 Month','3 Month',
'0 Month','3 Month','0 Month','3 Month','0 Month','3 Month',
'0 Month','3 Month','0 Month','3 Month','0 Month','3 Month',
'0 Month','3 Month','0 Month','3 Month','0 Month','3 Month',],
'Scale':['Something','Something','Something','Something','Something',
'Something','Something','Something','Something','Something',
'Something','Something','Something','Something','Something',
'Something','Something','Something','Something','Something',
'Something','Something','Something','Something',],
'Value':[18,26,23,4,24,3,27,26,9,0,40,3,17,10,33,9,6,7,8,1,9,4,26,9,]
})

dfhb = dfgh.query('Period == "0 Month"')
dfh3 = dfgh.query('Period == "3 Month"')

print('run standard wilcoxon:')
print(wilcoxon(dfhb.Value, dfh3.Value,alternative='two-sided', correction=True,))
print('\ntest for ties and selct normal approximation if the exist')
if (dfhb['Value'].nunique() < len(dfhb['Value'])) or (dfh3['Value'].nunique() < len(dfh3['Value'])):
print('ties identified - use approx:')
pval = wilcoxon(dfhb.Value, dfh3.Value, alternative='two-sided', correction=True,mode ='approx',)
else:
print('no ties')
plav = wilcoxon(dfhb.Value, dfh3.Value,alternative='two-sided', correction=True,mode ='exact',)
print(pval)
print('SciPy Version: ' + scipy.__version__)

Error message

There is no associated error.

SciPy/NumPy/Python version information

1.6.2

Issue 2:
My issue is about stats.wilcoxon(). stats.wilcoxon detects when d== 0, and appropriately warns that 'exact' will not work under such a circumstance. However, it reports that this is because of 'ties', and not because of the presence of 0s.

Exact calculations will also not be accurate when there are ties in rank, but the function does not appropriately detect such scenarios.

R produces appropriate warnings in both circumstances

wilcox.test(0:4)

Wilcoxon signed rank test with continuity correction

data: 0:4
V = 10, p-value = 0.1003
alternative hypothesis: true location is not equal to 0

Warning message:
In wilcox.test.default(0:4) : cannot compute exact p-value with zeroes
> wilcox.test(c(1,1:4))

Wilcoxon signed rank test with continuity correction

data: c(1, 1:4)
V = 15, p-value = 0.05791
alternative hypothesis: true location is not equal to 0

Warning message:
In wilcox.test.default(c(1, 1:4)) : cannot compute exact p-value with ties

Reproducing code example:

>>> from scipy import stats as stats
>>> x = [1, 1, 2, 3, 4]
>>> stats.wilcoxon(x, correction=True, mode='exact') ## should produce a warning similar to R and should fall back to approx
WilcoxonResult(statistic=0.0, pvalue=0.0625)
>>> stats.wilcoxon(x, correction=True, mode='approx')
/home/jrr/miniconda3/lib/python3.8/site-packages/scipy/stats/morestats.py:2981: UserWarning: Sample size too small for normal approximation.
warnings.warn("Sample size too small for normal approximation.")
WilcoxonResult(statistic=0.0, pvalue=0.05790726541729728)
>>> x = [0, 1, 2, 3, 4]
>>> stats.wilcoxon(x, correction=True) ## warns, but calls ties.
/home/jrr/miniconda3/lib/python3.8/site-packages/scipy/stats/morestats.py:2967: UserWarning: Exact p-value calculation does not work if there are ties. Switching to normal approximation.
warnings.warn("Exact p-value calculation does not work if there are "
WilcoxonResult(statistic=0.0, pvalue=0.10034824646229075)

Scipy/Numpy/Python version information:

import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)

1.6.1 1.19.2 sys.version_info(major=3, minor=8, micro=8, releaselevel='final', serial=0)