The above 5 lines can be replaced by:

    pval = np.where(z > 0, norm.sf(z), norm.cdf(z))