Find numeric column pairs whose Pearson and Spearman correlations disagree in sign

Quick Overview

Write a pandas function that finds every pair of numeric columns whose Pearson and Spearman correlations have opposite signs and returns them in a fixed, sorted schema. It tests why linear and rank correlation can disagree, careful pair ordering, NaN handling, and exact dtypes in the empty case.

Find numeric column pairs whose Pearson and Spearman correlations disagree in sign

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `correlation_sign_disagreement(df)`, which takes a pandas DataFrame and returns a `pd.DataFrame` listing every unordered pair of distinct numeric columns whose Pearson and Spearman correlations have opposite signs (`pearson * spearman < 0`). The result must meet these requirements: - Ignore non-numeric columns. Each pair appears once, with `col_a` the lexicographically smaller name. - Columns exactly `['col_a', 'col_b', 'pearson', 'spearman']` with dtypes `object, object, float64, float64`. - Sort by `abs(pearson - spearman)` descending, then `col_a`, then `col_b` ascending, then `reset_index(drop=True)`. - Do not include the absolute difference as a column. - If no pair disagrees, return an empty DataFrame with those four columns and the exact dtypes. Example, with the columns in the order `b, a, c, label` and `label` holding text: ```python df = pd.DataFrame({ 'b': [1, 2, 3, 4, 5], 'a': [4, 3, 2, 21, 0], 'c': [1, 2, 3, -21, 5], 'label': ['p', 'q', 'r', 's', 't'], }) correlation_sign_disagreement(df) ``` Expected result, with values rounded to four decimals: ```text col_a col_b pearson spearman 0 b c -0.2212 0.4000 1 a b 0.1857 -0.4000 ``` In `a`, most values fall as `b` rises, so Spearman is negative, but the single value 21 pulls Pearson positive. In `c` the value -21 does the reverse. The pair `(a, c)` has Pearson about -0.9993 and Spearman exactly -1, the same sign, so it is dropped. `(b, c)` comes first because its absolute difference (0.6212) is larger than that of `(a, b)` (0.5857), and the pair of columns `b` and `a` is reported with `col_a = 'a'` even though `b` comes first in the DataFrame. ```hint Pair order Check which name ends up in `col_a` when the DataFrame lists the larger name first. ``` ```hint Schema in every branch Look at the dtypes of the frame you return when no pair qualifies, as well as when some do. ``` ### Constraints & Assumptions - The input is a pandas DataFrame with n rows and k columns of mixed dtypes. Column names are strings, compared with Python's default string ordering. - Only integer and float columns count as numeric (`select_dtypes(include='number')`), so text and boolean columns are skipped. - Spearman uses average ranks for ties, the default in pandas and SciPy. ### What a Strong Answer Covers - Why Pearson and Spearman can disagree in sign, and what kind of data usually causes it. - One row per unordered pair, with the names ordered lexicographically regardless of column order. - What the strict `< 0` test does with correlations that are exactly zero or NaN. - Sorting on a helper key that never appears in the output, with the stated tie-breaks. - The same columns and dtypes in the empty result as in a non-empty one. ### Follow-up Questions - How would you handle pairs with NaN correlations from constant or all-missing columns? - How would you rank which disagreeing pairs are most driven by outliers? - A pair with Pearson 0.01 and Spearman -0.02 passes the test. How would you keep near-zero noise like that out of the report?

Overview: Write a pandas function that finds every pair of numeric columns whose Pearson and Spearman correlations have opposite signs and returns them in a fixed, sorted schema. It tests why linear and rank correlation can disagree, careful pair ordering, NaN handling, and exact dtypes in the empty case.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Write correlation_sign_disagreement(df), which takes a pandas DataFrame and returns a pd.DataFrame listing every unordered pair of distinct numeric columns whose Pearson and Spearman correlations have opposite signs (pearson * spearman < 0).

The result must meet these requirements:

  • Ignore non-numeric columns. Each pair appears once, with col_a the lexicographically smaller name.
  • Columns exactly ['col_a', 'col_b', 'pearson', 'spearman'] with dtypes object, object, float64, float64 .
  • Sort by abs(pearson - spearman) descending, then col_a , then col_b ascending, then reset_index(drop=True) .
  • Do not include the absolute difference as a column.
  • If no pair disagrees, return an empty DataFrame with those four columns and the exact dtypes.

Example, with the columns in the order b, a, c, label and label holding text:

df = pd.DataFrame({
    'b': [1, 2, 3, 4, 5],
    'a': [4, 3, 2, 21, 0],
    'c': [1, 2, 3, -21, 5],
    'label': ['p', 'q', 'r', 's', 't'],
})
correlation_sign_disagreement(df)

Expected result, with values rounded to four decimals:

  col_a col_b  pearson  spearman
0     b     c  -0.2212    0.4000
1     a     b   0.1857   -0.4000

In a, most values fall as b rises, so Spearman is negative, but the single value 21 pulls Pearson positive. In c the value -21 does the reverse. The pair (a, c) has Pearson about -0.9993 and Spearman exactly -1, the same sign, so it is dropped. (b, c) comes first because its absolute difference (0.6212) is larger than that of (a, b) (0.5857), and the pair of columns b and a is reported with col_a = 'a' even though b comes first in the DataFrame.

Constraints & Assumptions

  • The input is a pandas DataFrame with n rows and k columns of mixed dtypes. Column names are strings, compared with Python's default string ordering.
  • Only integer and float columns count as numeric ( select_dtypes(include='number') ), so text and boolean columns are skipped.
  • Spearman uses average ranks for ties, the default in pandas and SciPy.

What a Strong Answer Covers Guidance

  • Why Pearson and Spearman can disagree in sign, and what kind of data usually causes it.
  • One row per unordered pair, with the names ordered lexicographically regardless of column order.
  • What the strict < 0 test does with correlations that are exactly zero or NaN.
  • Sorting on a helper key that never appears in the output, with the stated tie-breaks.
  • The same columns and dtypes in the empty result as in a non-empty one.

Follow-up Questions Guidance

  • How would you handle pairs with NaN correlations from constant or all-missing columns?
  • How would you rank which disagreeing pairs are most driven by outliers?
  • A pair with Pearson 0.01 and Spearman -0.02 passes the test. How would you keep near-zero noise like that out of the report?