Correlation Matrix With Exclusion Report

Quick Overview

Write a pandas function that returns the Pearson correlation matrix over the int, float and bool columns of a mixed-type DataFrame, plus a sorted list of the columns it left out. It tests dtype selection rules, column ordering, and clear reporting of what a correlation heatmap does not cover.

Correlation Matrix With Exclusion Report

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `numeric_corr_with_exclusions(df)` taking a mixed-dtype DataFrame. Return a tuple `(corr_df, excluded)`: the Pearson correlation matrix over float, int, and bool columns, and a sorted `list[str]` of every other column. Requirements: - Bool columns are included in `corr_df`, not `excluded`. - `corr_df` is square, `float64`, and indexed and columned by the retained columns in original DataFrame order. - `excluded` holds all remaining column names (object or string, datetime, category, and so on), sorted. Example: ```python df = pd.DataFrame({ "age": [25, 35, 45], # int64 "score": [1.0, 2.0, 3.0], # float64 "active": [False, True, True], # bool "name": ["a", "b", "c"], # object "joined": pd.to_datetime(["2024-01-01", "2024-02-01", "2024-03-01"]), # datetime64 }) corr_df, excluded = numeric_corr_with_exclusions(df) list(corr_df.columns) # ['age', 'score', 'active'] (original order) corr_df.dtypes.unique() # [float64] excluded # ['joined', 'name'] (sorted) corr_df.round(3) # age score active # age 1.000 1.000 0.866 # score 1.000 1.000 0.866 # active 0.866 0.866 1.000 ``` `score` is a linear function of `age`, so their correlation is 1. Read as 0, 1, 1, `active` has correlation $\sqrt{3}/2 \approx 0.866$ with both of them. ```hint Which dtypes count Check how pandas classifies bool and timedelta columns as numeric before you trust a one-word dtype filter. ``` ### Constraints & Assumptions - Column names are unique strings, so `excluded` can be sorted as a `list[str]`. - The DataFrame can hold any mix of numpy and pandas extension dtypes. ### Clarifying Questions to Ask - Do nullable `Int64`, `Float64` and `boolean` columns count as int, float and bool? - How should missing values inside retained columns be handled: pairwise, as `DataFrame.corr` does by default, or by dropping incomplete rows first? - Should a bool column stored as `object` because it contains missing values be retained or excluded? ### What a Strong Answer Covers - A retention rule that keeps int, float and bool columns, and leaves out object, string, category, datetime and timedelta columns. - `corr_df` as a square `float64` matrix with index and columns in original column order. - `excluded` derived from the same rule, so every column lands in exactly one of the two outputs, and returned sorted. - Behavior on edge inputs: no retained columns, and constant or all-NaN retained columns. ### Follow-up Questions - How would you handle a retained column that is entirely NaN or constant? - Why prefer explicit dtype selection over relying on `df.corr(numeric_only=True)` alone? - How would you measure association for the excluded categorical columns?

Overview: Write a pandas function that returns the Pearson correlation matrix over the int, float and bool columns of a mixed-type DataFrame, plus a sorted list of the columns it left out. It tests dtype selection rules, column ordering, and clear reporting of what a correlation heatmap does not cover.

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

Write numeric_corr_with_exclusions(df) taking a mixed-dtype DataFrame. Return a tuple (corr_df, excluded): the Pearson correlation matrix over float, int, and bool columns, and a sorted list[str] of every other column.

Requirements:

  • Bool columns are included in corr_df , not excluded .
  • corr_df is square, float64 , and indexed and columned by the retained columns in original DataFrame order.
  • excluded holds all remaining column names (object or string, datetime, category, and so on), sorted.

Example:

df = pd.DataFrame({
    "age": [25, 35, 45],                  # int64
    "score": [1.0, 2.0, 3.0],             # float64
    "active": [False, True, True],        # bool
    "name": ["a", "b", "c"],              # object
    "joined": pd.to_datetime(["2024-01-01", "2024-02-01", "2024-03-01"]),  # datetime64
})

corr_df, excluded = numeric_corr_with_exclusions(df)

list(corr_df.columns)    # ['age', 'score', 'active']  (original order)
corr_df.dtypes.unique()  # [float64]
excluded                 # ['joined', 'name']  (sorted)

corr_df.round(3)
#           age  score  active
# age     1.000  1.000   0.866
# score   1.000  1.000   0.866
# active  0.866  0.866   1.000

score is a linear function of age, so their correlation is 1. Read as 0, 1, 1, active has correlation 3/2≈0.866\sqrt{3}/2 \approx 0.866 with both of them.

Constraints & Assumptions

  • Column names are unique strings, so excluded can be sorted as a list[str] .
  • The DataFrame can hold any mix of numpy and pandas extension dtypes.

Clarifying Questions to Ask Guidance

  • Do nullable Int64 , Float64 and boolean columns count as int, float and bool?
  • How should missing values inside retained columns be handled: pairwise, as DataFrame.corr does by default, or by dropping incomplete rows first?
  • Should a bool column stored as object because it contains missing values be retained or excluded?

What a Strong Answer Covers Guidance

  • A retention rule that keeps int, float and bool columns, and leaves out object, string, category, datetime and timedelta columns.
  • corr_df as a square float64 matrix with index and columns in original column order.
  • excluded derived from the same rule, so every column lands in exactly one of the two outputs, and returned sorted.
  • Behavior on edge inputs: no retained columns, and constant or all-NaN retained columns.

Follow-up Questions Guidance

  • How would you handle a retained column that is entirely NaN or constant?
  • Why prefer explicit dtype selection over relying on df.corr(numeric_only=True) alone?
  • How would you measure association for the excluded categorical columns?