Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Changed return type of pearsonr to namedtuple #13801

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions scipy/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4030,6 +4030,9 @@ def __init__(self, msg=None):
"correlation coefficent may be inaccurate.")
self.args = (msg,)


PearsonrResult = namedtuple('PearsonrResult', ('correlation', 'pvalue'))


def pearsonr(x, y):
r"""
Expand Down Expand Up @@ -4058,10 +4061,10 @@ def pearsonr(x, y):

Returns
-------
r : float
Pearson's correlation coefficient.
p-value : float
Two-tailed p-value.
correlation : float
Pearson's correlation coefficient.
pvalue : float
Two-tailed p-value.

Warns
-----
Expand Down Expand Up @@ -4212,7 +4215,7 @@ def pearsonr(x, y):
ab = n/2 - 1
prob = 2*special.btdtr(ab, ab, 0.5*(1 - abs(np.float64(r))))

return r, prob
return PearsonrResult(r, prob)


def fisher_exact(table, alternative='two-sided'):
Expand Down
5 changes: 5 additions & 0 deletions scipy/stats/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ def test_len1(self):
x = [1]
y = [2]
assert_raises(ValueError, stats.pearsonr, x, y)

def test_pearsonr_result_attributes(self):
res = stats.pearsonr(X, X)
attributes = ('correlation', 'pvalue')
check_named_results(res, attributes)


class TestFisherExact:
Expand Down