Pairwise Pearson correlations with p-values and multiple-comparison correction
Project description
pcorr
Compute all pairwise Pearson/Spearman correlations between numeric columns in a pandas.DataFrame — like pandas.DataFrame.corr(), but with p-values for each pair and multiple-comparison correction in one call.
Installation
pip install pcorr
pip install "pcorr[full]"
Usage
import pandas as pd
from pcorr import corr_pairwise, corr_table, show_table
df = pd.read_csv("data.csv")
# Long (tidy) format: one row per pair
table = corr_pairwise(df, method="fdr_bh", alpha=0.05)
print(table)
# var1 var2 n r p_corrected p_value significant
# 0 x y 200 0.967 0.0000 0.0000 True
# 1 x z 200 -0.894 0.0000 0.0000 True
# ...
# Two square tables (r and p) rendered as lower-triangle output
tables = corr_table(df, method="bonferroni")
tables["r"] # coefficients
tables["p"] # p-values (raw when method="none", otherwise corrected)
# Convenience display (renders nicely in Jupyter, prints in console)
show_table(df, method="bonferroni")
API
corr_pairwise(df, ...)— tidy table: one row per column pair.corr_table(df, ...)— two square tables (coefficients and p-values) in a lower-triangle style.show_table(df, ...)— displays/printscorr_table(...).corr_matrices(df, ...)— alias forcorr_table(...)(compatibility).
Output format
corr_pairwise
Always returns:
var1,var2— column namesn— number of valid observations in the pair (after pairwise NaN deletion)r— correlation coefficientsignificant—p < alphafor the p-value used for significance
P-value columns depend on method:
method="none"returnsp_value(raw p-value)- any correction (e.g.
bonferroni,fdr_bh,holm) returnsp_correctedandp_value(raw)
Raw p-values example:
from pcorr import corr_pairwise
out = corr_pairwise(df, method="none")
corr_table / corr_matrices
Returns a dict with two DataFrames:
tables["r"]— coefficients (lower triangle filled), diagonal"1", upper triangle blanktables["p"]— p-values in the same layout, diagonal"—"- raw p-values when
method="none" - corrected p-values for any correction method
- raw p-values when
Parameters
| parameter | meaning |
|---|---|
columns |
which columns to use (default: all numeric) |
corr |
"pearson" or "spearman" |
method |
bonferroni, fdr_bh, holm, sidak, none, ... |
alpha |
threshold for significant |
min_n |
minimum valid observations per pair (pairwise NaN deletion) |
round_to |
rounding for r/p columns (None disables rounding) |
Notes
- Pairwise NaN deletion: each pair has its own
n. - Constant columns and pairs with
n < min_nare handled without crashing. corr_pairwiseis sorted by the p-value used for ranking (corrected when applicable).
P-value correction methods
method="none"andmethod="bonferroni"work without statsmodels.- Other methods use
statsmodels.stats.multitest.multipletestsand require statsmodels (install via thefullextra).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pcorr-0.6.0.tar.gz.
File metadata
- Download URL: pcorr-0.6.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a8956d2aa8c8515016e71190182cd6b96a0b42d2ef7fd2ad8bc6bdf2becb41d
|
|
| MD5 |
5e04639033b31bc9292f9351c2074b0b
|
|
| BLAKE2b-256 |
f246c97936878de01b5aa809e027462a409905e0c7d54a988150acf32dcf931a
|
File details
Details for the file pcorr-0.6.0-py3-none-any.whl.
File metadata
- Download URL: pcorr-0.6.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
163271d1d7cebc5bd9f9f0d2f55ca50ebeed753a98d5f37d044255f5fe3f4dea
|
|
| MD5 |
f61869af8eb8e6063e7449dd90f7c04f
|
|
| BLAKE2b-256 |
a6576ab8425ee92b35ed3756e7d899880f850b3f6cbc05798fe9af1e562d7495
|