How pandas .corr() handles non-numeric columns and missing values in a correlation matrix, and why it matters

Quick Overview

Asks how pandas computes a correlation matrix when a DataFrame has text or categorical columns and missing values, and why those defaults can mislead an analysis. Tests practical pandas knowledge, how missing data changes the sample behind each correlation, and when the resulting matrix stops being valid.

How pandas .corr() handles non-numeric columns and missing values in a correlation matrix, and why it matters

Role: Data Scientist

Category: Statistics & Math

Difficulty: medium

Interview Round: Other

You call `df.corr()` on a DataFrame that mixes numeric, text and categorical columns, and several numeric columns have missing values on different rows. When you compute a correlation matrix in `pandas`, how are non-numeric columns and missing values handled, and why does that matter? ```hint Count the rows For each cell of the matrix, ask which rows actually went into it when different columns are missing on different rows. ``` ```hint Read the signature Look at the `numeric_only` and `min_periods` parameters of `DataFrame.corr`, and check which pandas version you are running when you read their defaults. ``` ### What a Strong Answer Covers - What `.corr()` does with non-numeric columns, including how the default differs between pandas versions and how to control it explicitly. - How missing values are handled and what that means for the sample behind each cell. - What the missing-data handling does to the reliability of individual cells and to the validity of the matrix as a whole, and how the missingness pattern affects bias. - Practical safeguards: checking missingness and overlap counts, using `min_periods`, and choosing deletion, imputation or categorical encoding on purpose. ### Follow-up Questions - How would you get the number of rows behind each cell of the correlation matrix? - Why can a matrix built with pairwise deletion fail to be positive semi-definite, and what breaks downstream when it does? - How does mean imputation change the correlations you compute? - How would you measure association between a categorical column and a numeric one, or between two categorical columns?

Overview: Asks how pandas computes a correlation matrix when a DataFrame has text or categorical columns and missing values, and why those defaults can mislead an analysis. Tests practical pandas knowledge, how missing data changes the sample behind each correlation, and when the resulting matrix stops being valid.

PracHub logo
General
Nov 24, 2026
mediumData ScientistOtherStatistics & Math
0
0

You call df.corr() on a DataFrame that mixes numeric, text and categorical columns, and several numeric columns have missing values on different rows. When you compute a correlation matrix in pandas, how are non-numeric columns and missing values handled, and why does that matter?

What a Strong Answer Covers Guidance

  • What .corr() does with non-numeric columns, including how the default differs between pandas versions and how to control it explicitly.
  • How missing values are handled and what that means for the sample behind each cell.
  • What the missing-data handling does to the reliability of individual cells and to the validity of the matrix as a whole, and how the missingness pattern affects bias.
  • Practical safeguards: checking missingness and overlap counts, using min_periods , and choosing deletion, imputation or categorical encoding on purpose.

Follow-up Questions Guidance

  • How would you get the number of rows behind each cell of the correlation matrix?
  • Why can a matrix built with pairwise deletion fail to be positive semi-definite, and what breaks downstream when it does?
  • How does mean imputation change the correlations you compute?
  • How would you measure association between a categorical column and a numeric one, or between two categorical columns?