Report the pairwise sample size behind every Pearson correlation
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write `correlation_sample_sizes(df, min_n)` taking a DataFrame containing NaNs and an int `min_n`. Return a `pd.DataFrame` with one row per unordered pair of distinct numeric columns, exposing the pairwise sample size behind each Pearson correlation.
Requirements:
- The columns are exactly `['col_a', 'col_b', 'r', 'n_pairs', 'reliable']` with dtypes `object`, `object`, `float64`, `int64`, `bool`.
- `r` is the Pearson correlation of the pair (numeric columns only).
- `n_pairs` is the number of rows where both columns are non-null (the pairwise-complete count gated by `min_periods` in `.corr()`).
- `reliable` is `n_pairs >= min_n`.
- Sort by `n_pairs` ascending, then `col_a`, then `col_b` ascending, then `reset_index(drop=True)`.
Example with `min_n = 3`:
```python
df = pd.DataFrame({
"x": [1.0, 2.0, 3.0, 4.0, np.nan],
"y": [3.0, 4.0, np.nan, 8.0, 10.0],
"z": [np.nan, 1.0, np.nan, np.nan, 5.0],
"g": ["a", "b", "c", "d", "e"],
})
```
Expected output:
```text
col_a col_b r n_pairs reliable
0 x z NaN 1 False
1 y z 1.0000 2 False
2 x y 0.9897 3 True
```
`x` and `z` overlap only in row 1, so their correlation is undefined. `y` and `z` overlap in rows 1 and 4, and any two distinct points give a perfect correlation, which is why that pair is flagged as unreliable. `x` and `y` overlap in rows 0, 1 and 3. `g` is not numeric and takes no part.
```hint Which rows count
`.corr()` computes each cell from the rows where both columns are present. Count exactly those rows, which is a different number from the non-null count of either column alone.
```
```hint A reproducible pair order
Each unordered pair appears once. Fix which column goes in `col_a` before sorting, and check what `r` should be when a pair has fewer than two overlapping rows.
```
### Constraints & Assumptions
- Numeric means int or float columns, including the nullable `Int64` and `Float64` dtypes. Bool, timedelta, datetime, category and object columns are ignored.
- Column names are unique strings.
- Within a pair, `col_a` is the column that appears earlier in the DataFrame.
- With k numeric columns the output has $k(k-1)/2$ rows. If there are fewer than two numeric columns it is empty but keeps the five columns and their dtypes.
### What a Strong Answer Covers
- `n_pairs` computed from the rows where both columns are non-null, the same rows `.corr()` uses for that cell.
- Pairs with 0 or 1 overlapping rows, or a constant column, kept in the output with `r` as NaN.
- The exact columns, dtypes and sort order, including the empty case.
- A stated rule for which columns count as numeric and which column of a pair is `col_a`.
- The cost in terms of the number of numeric columns and rows.
### Follow-up Questions
- What `r` value should you report when `n_pairs` is 0 or 1?
- How would you scale this to hundreds of columns without an O(k²) Python loop?
- How would you judge reliability with a confidence interval for `r` instead of a fixed `min_n`?
Overview: Write a pandas function that lists every pair of numeric columns with its Pearson correlation, the number of rows where both values are present, and a reliability flag. It tests pairwise-complete NaN handling, exact output dtypes and sort order, and judging how much data supports a correlation.